Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,47 +8,48 @@ import json
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
# ⚠️ REMPLACE PAR LE CHEMIN DE TON DATASET
|
| 12 |
DATASET_REPO_ID = "Javare/Local_AI_Leaderboard"
|
| 13 |
FILENAME = "scores.json"
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
|
|
|
|
| 16 |
class ScoreEntry(BaseModel):
|
| 17 |
config: str
|
| 18 |
browser: str
|
| 19 |
power: str
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def get_scores():
|
| 23 |
try:
|
| 24 |
-
# Télécharge le fichier de scores depuis le dataset Hugging Face
|
| 25 |
path = hf_hub_download(repo_id=DATASET_REPO_ID, filename=FILENAME, repo_type="dataset", token=HF_TOKEN)
|
| 26 |
with open(path, "r", encoding="utf-8") as f:
|
| 27 |
return json.load(f)
|
| 28 |
except Exception:
|
| 29 |
-
# Si le fichier n'existe pas encore (premier lancement), on démarre à vide
|
| 30 |
return []
|
| 31 |
|
| 32 |
@app.get("/api/scores")
|
| 33 |
def read_scores():
|
| 34 |
scores = get_scores()
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
@app.post("/api/score")
|
| 39 |
def add_score(entry: ScoreEntry):
|
| 40 |
if not HF_TOKEN:
|
| 41 |
-
raise HTTPException(status_code=500, detail="HF_TOKEN manquant
|
| 42 |
|
| 43 |
scores = get_scores()
|
| 44 |
scores.append(entry.dict())
|
| 45 |
|
| 46 |
-
# Sauvegarde locale temporaire du JSON complet
|
| 47 |
local_path = "scores.json"
|
| 48 |
with open(local_path, "w", encoding="utf-8") as f:
|
| 49 |
json.dump(scores, f, ensure_ascii=False, indent=2)
|
| 50 |
|
| 51 |
-
# Envoi sécurisé vers ton Dataset Hugging Face
|
| 52 |
api = HfApi()
|
| 53 |
try:
|
| 54 |
api.upload_file(
|
|
@@ -59,11 +60,10 @@ def add_score(entry: ScoreEntry):
|
|
| 59 |
token=HF_TOKEN
|
| 60 |
)
|
| 61 |
except Exception as e:
|
| 62 |
-
raise HTTPException(status_code=500, detail=
|
| 63 |
|
| 64 |
return {"status": "success"}
|
| 65 |
|
| 66 |
-
# Déclare l'accès aux fichiers statiques de ton interface
|
| 67 |
@app.get("/")
|
| 68 |
def read_index():
|
| 69 |
return FileResponse("index.html")
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 11 |
DATASET_REPO_ID = "Javare/Local_AI_Leaderboard"
|
| 12 |
FILENAME = "scores.json"
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
|
| 15 |
+
# Nouveau modèle étendu selon tes exigences
|
| 16 |
class ScoreEntry(BaseModel):
|
| 17 |
config: str
|
| 18 |
browser: str
|
| 19 |
power: str
|
| 20 |
+
min_tps: float
|
| 21 |
+
max_tps: float
|
| 22 |
+
avg_tps: float
|
| 23 |
+
total_tokens: int
|
| 24 |
+
duration: float
|
| 25 |
|
| 26 |
def 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")
|
| 35 |
def 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")
|
| 42 |
def 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(
|
|
|
|
| 60 |
token=HF_TOKEN
|
| 61 |
)
|
| 62 |
except Exception as e:
|
| 63 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 64 |
|
| 65 |
return {"status": "success"}
|
| 66 |
|
|
|
|
| 67 |
@app.get("/")
|
| 68 |
def read_index():
|
| 69 |
return FileResponse("index.html")
|