File size: 3,200 Bytes
e5bf7d4
c78b448
e5bf7d4
5470817
 
e5bf7d4
 
 
 
 
5470817
 
 
 
 
 
 
e6eb6c0
 
e5bf7d4
 
5470817
 
 
e5bf7d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6eb6c0
e5bf7d4
e6eb6c0
e5bf7d4
 
 
 
 
 
 
 
 
 
 
 
 
bf58a54
 
 
8d4d46d
 
 
 
 
5470817
b98a4ba
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import os
from dotenv import load_dotenv
from huggingface_hub import hf_hub_download
import json
from datetime import datetime, timedelta
import aiofiles
import aiofiles.os

# Load environment variables
load_dotenv()

# API configuration
API_HOST = os.getenv("API_HOST", "0.0.0.0")
API_PORT = int(os.getenv("API_PORT", "3002"))
HUGGING_FACE_HUB_TOKEN = os.getenv("HUGGING_FACE_HUB_TOKEN")
HUGGING_FACE_STORAGE_REPO = os.getenv("HUGGING_FACE_STORAGE_REPO")
CACHE_DIR = "cache"
CACHE_DURATION = timedelta(seconds=10)

app = FastAPI()

# Enable CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # En production, remplacer par les domaines autorisés
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Ensure cache directory exists
os.makedirs(CACHE_DIR, exist_ok=True)

async def get_cached_data():
    cache_file = os.path.join(CACHE_DIR, "leaderboards_cache.json")
    cache_meta_file = os.path.join(CACHE_DIR, "cache_metadata.json")
    
    try:
        if await aiofiles.os.path.exists(cache_meta_file):
            async with aiofiles.open(cache_meta_file, 'r') as f:
                metadata = json.loads(await f.read())
                cache_time = datetime.fromisoformat(metadata['timestamp'])
                
                if datetime.now() - cache_time < CACHE_DURATION:
                    async with aiofiles.open(cache_file, 'r') as f:
                        return json.loads(await f.read())
    except Exception as e:
        print(f"Error reading cache: {e}")
    
    return None

async def update_cache(data):
    try:
        async with aiofiles.open(os.path.join(CACHE_DIR, "leaderboards_cache.json"), 'w') as f:
            await f.write(json.dumps(data))
        async with aiofiles.open(os.path.join(CACHE_DIR, "cache_metadata.json"), 'w') as f:
            await f.write(json.dumps({'timestamp': datetime.now().isoformat()}))
    except Exception as e:
        print(f"Error updating cache: {e}")

@app.get("/api/leaderboards")
async def get_leaderboards():
    try:
        # Check cache first
        cached_data = await get_cached_data()
        if cached_data:
            return cached_data
            
        # If no cache or expired, download from HF
        file_path = hf_hub_download(
            repo_id=HUGGING_FACE_STORAGE_REPO,
            filename="final_leaderboards.json",
            token=HUGGING_FACE_HUB_TOKEN,
            repo_type="dataset"
        )
        
        with open(file_path, 'r') as f:
            data = json.load(f)
            
        # Update cache
        await update_cache(data)
        
        return data
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to fetch leaderboards: {str(e)}")

# Mount static files for the React client
app.mount("/", StaticFiles(directory="static", html=True), name="static")

def start():
    """Entry point for the Poetry script"""
    import uvicorn
    uvicorn.run("server:app", host=API_HOST, port=API_PORT, reload=True)

if __name__ == "__main__":
    start()