# -*- coding: utf-8 -*- """Metacognition Leaderboard — FastAPI (HF Space, Docker). index.html + /api/leaderboard + /submit.""" import json, os from fastapi import FastAPI, Request from fastapi.responses import FileResponse, JSONResponse app = FastAPI(title="Metacognition Leaderboard") HERE = os.path.dirname(os.path.abspath(__file__)) SUB_REPO = "ginigen-ai/Metacognition-Submissions" @app.get("/") def index(): return FileResponse(os.path.join(HERE, "index.html")) @app.get("/api/leaderboard") def leaderboard(): p = os.path.join(HERE, "leaderboard_mcq.json") if os.path.exists(p): return JSONResponse(json.load(open(p, encoding="utf-8"))) return JSONResponse([]) @app.post("/submit") async def submit(req: Request): try: data = await req.json() except Exception: return JSONResponse({"ok": False, "msg": "invalid request"}, status_code=400) mid = str(data.get("model_id", "")).strip().strip("/") if "/" not in mid or len(mid) < 3 or " " in mid: return JSONResponse({"ok": False, "msg": "Enter a valid HF model id like org/model-name"}, status_code=400) tok = os.environ.get("HF_TOKEN") if not tok: return JSONResponse({"ok": False, "msg": "server not configured (no token)"}, status_code=500) from huggingface_hub import HfApi, hf_hub_download api = HfApi(token=tok) try: p = hf_hub_download(SUB_REPO, "submissions.json", repo_type="dataset", token=tok) subs = json.load(open(p, encoding="utf-8")) except Exception: subs = [] if any(s.get("model_id") == mid for s in subs): return JSONResponse({"ok": True, "msg": f"{mid} is already queued.", "count": len(subs)}) subs.append({"model_id": mid, "submitter": str(data.get("submitter", "anon"))[:40], "ts": str(data.get("ts", "")), "status": "queued"}) api.upload_file(path_or_fileobj=json.dumps(subs, ensure_ascii=False, indent=1).encode(), path_in_repo="submissions.json", repo_id=SUB_REPO, repo_type="dataset", commit_message=f"submit {mid}") return JSONResponse({"ok": True, "msg": f"Submitted {mid} — queued for daily 09:00 KST measurement.", "count": len(subs)}) @app.get("/health") def health(): return {"ok": True}