from fastapi import FastAPI, HTTPException from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse from pydantic import BaseModel import os from huggingface_hub import HfApi, hf_hub_download import json app = FastAPI() DATASET_REPO_ID = "Javare/Local_AI_Leaderboard" FILENAME = "scores.json" HF_TOKEN = os.environ.get("HF_TOKEN") # Nouveau modèle étendu selon tes exigences class ScoreEntry(BaseModel): config: str browser: str power: str min_tps: float max_tps: float avg_tps: float total_tokens: int duration: float def get_scores(): try: path = hf_hub_download(repo_id=DATASET_REPO_ID, filename=FILENAME, repo_type="dataset", token=HF_TOKEN) with open(path, "r", encoding="utf-8") as f: return json.load(f) except Exception: return [] @app.get("/api/scores") def read_scores(): scores = get_scores() # On trie le TOP 10 par la vitesse moyenne (avg_tps) scores.sort(key=lambda x: x.get("avg_tps", 0), reverse=True) return scores[:10] @app.post("/api/score") def add_score(entry: ScoreEntry): if not HF_TOKEN: raise HTTPException(status_code=500, detail="HF_TOKEN manquant") scores = get_scores() scores.append(entry.dict()) local_path = "scores.json" with open(local_path, "w", encoding="utf-8") as f: json.dump(scores, f, ensure_ascii=False, indent=2) api = HfApi() try: api.upload_file( path_or_fileobj=local_path, path_in_repo=FILENAME, repo_id=DATASET_REPO_ID, repo_type="dataset", token=HF_TOKEN ) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) return {"status": "success"} @app.get("/") def read_index(): return FileResponse("index.html") app.mount("/assets", StaticFiles(directory="assets"), name="assets")