File size: 1,681 Bytes
c3173d9
9bdefe0
199524d
c3173d9
199524d
c3173d9
9bdefe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b95bf00
c3173d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b95bf00
 
d30efe2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
)

@app.get("/api/hello")
def greet_json():
    return {"Hello": "World!"}

@app.get("/api/model")
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.")