Javare/Local_AI_WebGPU
0
1from fastapi import FastAPI, HTTPException2from fastapi.staticfiles import StaticFiles3from fastapi.responses import FileResponse4from pydantic import BaseModel5import os6from huggingface_hub import HfApi, hf_hub_download7import json8 9app = FastAPI()10 11DATASET_REPO_ID = "Javare/Local_AI_Leaderboard" 12FILENAME = "scores.json"13HF_TOKEN = os.environ.get("HF_TOKEN")14 15# Nouveau modèle étendu selon tes exigences16class ScoreEntry(BaseModel):17 config: str18 browser: str19 power: str20 min_tps: float21 max_tps: float22 avg_tps: float23 total_tokens: int24 duration: float25 26def get_scores():27 try:28 path = hf_hub_download(repo_id=DATASET_REPO_ID, filename=FILENAME, repo_type="dataset", token=HF_TOKEN)29 with open(path, "r", encoding="utf-8") as f:30 return json.load(f)31 except Exception:32 return []33 34@app.get("/api/scores")35def read_scores():36 scores = get_scores()37 # On trie le TOP 10 par la vitesse moyenne (avg_tps)38 scores.sort(key=lambda x: x.get("avg_tps", 0), reverse=True)39 return scores[:10]40 41@app.post("/api/score")42def add_score(entry: ScoreEntry):43 if not HF_TOKEN:44 raise HTTPException(status_code=500, detail="HF_TOKEN manquant")45 46 scores = get_scores()47 scores.append(entry.dict())48 49 local_path = "scores.json"50 with open(local_path, "w", encoding="utf-8") as f:51 json.dump(scores, f, ensure_ascii=False, indent=2)52 53 api = HfApi()54 try:55 api.upload_file(56 path_or_fileobj=local_path,57 path_in_repo=FILENAME,58 repo_id=DATASET_REPO_ID,59 repo_type="dataset",60 token=HF_TOKEN61 )62 except Exception as e:63 raise HTTPException(status_code=500, detail=str(e))64 65 return {"status": "success"}66 67@app.get("/")68def read_index():69 return FileResponse("index.html")70 71app.mount("/assets", StaticFiles(directory="assets"), name="assets")