Spaces:
Running
Running
| from fastapi import FastAPI, Query | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import JSONResponse | |
| from pathlib import Path | |
| from hf_model_utils import get_model_structure | |
| app = FastAPI() | |
| # Allow React dev server (port 5173 by default) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=[ | |
| "http://localhost:5173", # optional, keep for local dev | |
| ], | |
| allow_credentials=False, # safer, enable only if using cookies/sessions | |
| allow_methods=["GET", "POST"], # restrict to what your API actually uses | |
| allow_headers=["Content-Type", "Authorization"], # only required headers | |
| ) | |
| def greet_json(): | |
| return {"Hello": "World!"} | |
| def get_model_info( | |
| name: str = Query(..., description="Name of the model on Hugging Face"), | |
| model_type: str | None = Query(None, description="Optional model type identifier") | |
| ): | |
| """ | |
| Get model structure info for a given Hugging Face model. | |
| Example: /api/model?name=bert-base-uncased | |
| """ | |
| try: | |
| info = get_model_structure(name, model_type) | |
| return JSONResponse(content={"success": True, "data": info}) | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=500, | |
| content={"success": False, "error": str(e)}, | |
| ) | |
| # Serve frontend build under / (but exclude /api) | |
| frontend_path = Path(__file__).parent.parent / "frontend" / "dist" | |
| if frontend_path.exists(): | |
| app.mount("/", StaticFiles(directory=frontend_path, html=True), name="frontend") | |
| else: | |
| print("Frontend build not found. Skipping StaticFiles mount.") | |