|
from fastapi import FastAPI, HTTPException |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from pydantic import BaseModel |
|
import json, os |
|
from evaluator import evaluate_model |
|
|
|
app = FastAPI() |
|
DB_PATH = "models_results.json" |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
class ModelIn(BaseModel): |
|
model_name: str |
|
|
|
@app.get("/results") |
|
def get_results(): |
|
try: |
|
with open(DB_PATH, "r") as f: |
|
return json.load(f) |
|
except (json.JSONDecodeError, FileNotFoundError): |
|
|
|
return [] |
|
|
|
@app.post("/evaluate") |
|
def eval_and_store(req: ModelIn): |
|
model_name = req.model_name |
|
|
|
data = json.load(open(DB_PATH)) |
|
if any(d["model"] == model_name for d in data): |
|
raise HTTPException(400, "Model already evaluated") |
|
|
|
try: |
|
metrics = evaluate_model( |
|
model_name=model_name, |
|
dataset_name="sunbird/salt", |
|
split="dev", |
|
) |
|
|
|
except Exception as e: |
|
raise HTTPException(500, f"Evaluation failed: {e}") |
|
|
|
data.append({"model": model_name, "metrics": metrics}) |
|
with open(DB_PATH, "w") as f: |
|
json.dump(data, f, indent=2) |
|
return {"status": "ok", "metrics": metrics} |
|
|
|
|
|
from fastapi.staticfiles import StaticFiles |
|
app.mount("/", StaticFiles(directory="frontend/build", html=True), name="static") |
|
|