File size: 1,945 Bytes
5a5f41d
 
 
 
 
 
 
 
 
 
 
 
 
 
868778b
5a5f41d
 
 
 
868778b
 
 
 
 
5a5f41d
 
 
 
 
 
 
 
 
 
 
 
868778b
 
 
5a5f41d
 
 
 
868778b
5a5f41d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
868778b
5a5f41d
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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")