Spaces:
Sleeping
Sleeping
Download backend/app/main.py from Creatiste/taste-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/backend/app/main.py
- Command line
-
hf download hf://spaces/Creatiste/taste-engine/backend/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/backend/app/main.py
3.26 kB
| """FastAPI application for taste-engine.""" | |
| import os | |
| from contextlib import asynccontextmanager | |
| from pathlib import Path | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| from backend.app import deps | |
| from backend.app.routers import products, ratings, stats, scan, images, discover | |
| STATIC_DIR = Path(__file__).resolve().parents[2] / "frontend" / "dist" | |
| async def lifespan(app: FastAPI): | |
| deps.init() | |
| yield | |
| app = FastAPI(title="Taste Engine", version="0.2.0", lifespan=lifespan) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["http://localhost:5173", "http://localhost:3000"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| app.include_router(products.router) | |
| app.include_router(ratings.router) | |
| app.include_router(stats.router) | |
| app.include_router(scan.router) | |
| app.include_router(images.router) | |
| app.include_router(discover.router) | |
| def health(): | |
| import os | |
| store = deps.get_store() | |
| total = store.conn.execute("SELECT COUNT(*) FROM products").fetchone()[0] | |
| cols = [r[1] for r in store.conn.execute("PRAGMA table_info(products)").fetchall()] | |
| embeds = 0 | |
| if "taste_embedding" in cols: | |
| embeds = store.conn.execute( | |
| "SELECT COUNT(*) FROM products WHERE taste_embedding IS NOT NULL" | |
| ).fetchone()[0] | |
| return { | |
| "status": "ok", | |
| "db_path": str(store.db_path), | |
| "total_products": total, | |
| "with_taste_embedding": embeds, | |
| "columns": cols, | |
| "data_dir": os.environ.get("TASTE_DATA_DIR", "not set"), | |
| } | |
| def pull_db(): | |
| """Pull latest DB from dataset repo and replace the local one.""" | |
| from huggingface_hub import hf_hub_download | |
| import shutil | |
| store = deps.get_store() | |
| db_path = store.db_path | |
| # Download from dataset repo | |
| downloaded = hf_hub_download( | |
| repo_id="Creatiste/taste-data", | |
| filename="taste.db", | |
| repo_type="dataset", | |
| local_dir="/tmp/hf-pull", | |
| ) | |
| # Close current connection, replace file, reinit | |
| store.conn.close() | |
| shutil.copy2(downloaded, str(db_path)) | |
| # Reinit store with new DB | |
| deps.init() | |
| new_store = deps.get_store() | |
| total = new_store.conn.execute("SELECT COUNT(*) FROM products").fetchone()[0] | |
| return {"ok": True, "total_products": total, "source": downloaded} | |
| # Serve built frontend in production (when dist exists) | |
| if STATIC_DIR.is_dir(): | |
| app.mount("/assets", StaticFiles(directory=STATIC_DIR / "assets"), name="static-assets") | |
| async def spa_root(): | |
| return FileResponse(STATIC_DIR / "index.html") | |
| async def spa_catchall(path: str): | |
| """Serve static files or fall back to index.html for SPA routing.""" | |
| file = STATIC_DIR / path | |
| if file.is_file(): | |
| return FileResponse(file) | |
| return FileResponse(STATIC_DIR / "index.html") | |
| def run(): | |
| import uvicorn | |
| port = int(os.environ.get("PORT", 8000)) | |
| uvicorn.run("backend.app.main:app", host="0.0.0.0", port=port, reload=True) | |
| if __name__ == "__main__": | |
| run() | |