arena / app.py
primenumbersam's picture
Refactor leaderboard with normalized cumulative return, CDF plot, and pyrefly config
d3a39db
Raw History Blame Contribute Delete
3.12 kB
from __future__ import annotations
import logging
from datetime import datetime, timezone
from pathlib import Path
from threading import RLock
from typing import Any
import gradio as gr
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from src.about import (
CITATION_BUTTON_LABEL,
CITATION_BUTTON_TEXT,
LLM_BENCHMARKS_TEXT,
SUBTITLE,
TITLE,
)
from src.leaderboard.market_metrics import (
compute_metrics_and_cdf,
load_market_prices,
resolve_db_path,
)
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger("leaderboard")
ROOT = Path(__file__).parent
STATIC_DIR = ROOT / "static"
DATA_LOCK = RLock()
DATA: dict[str, Any] = {}
try:
import spaces
except ImportError:
spaces = None
def _gpu_decorator(fn):
return spaces.GPU(fn) if spaces is not None else fn
@_gpu_decorator
def run_model_inference(prompt):
# Placeholder for future ZeroGPU ML model inference.
return prompt
def _empty_payload(message: str | None = None) -> dict[str, Any]:
return {
"rows": [],
"cdf": {},
"cum_returns": {},
"copy": {
"title": TITLE,
"subtitle": SUBTITLE,
"about": LLM_BENCHMARKS_TEXT.strip(),
"citation_label": CITATION_BUTTON_LABEL,
"citation": CITATION_BUTTON_TEXT.strip(),
},
"updated_at": datetime.now(timezone.utc).isoformat(),
"error": message,
}
def refresh_data(*, download: bool = True) -> dict[str, Any]:
"""Refresh market metrics and CDF data from SQLite storage."""
payload = _empty_payload()
try:
db_path = resolve_db_path(download=download)
prices_df = load_market_prices(db_path)
rows, cdf, cum_returns = compute_metrics_and_cdf(prices_df)
payload["rows"] = rows
payload["cdf"] = cdf
payload["cum_returns"] = cum_returns
except Exception as exc:
LOGGER.exception("Could not refresh market data")
payload["error"] = f"Could not load market data: {exc}"
with DATA_LOCK:
DATA.clear()
DATA.update(payload)
return dict(DATA)
server = gr.Server(
title=TITLE,
description=SUBTITLE,
version="2.0.0",
)
server.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
@server.get("/", response_class=HTMLResponse, include_in_schema=False)
async def homepage() -> HTMLResponse:
return HTMLResponse((STATIC_DIR / "index.html").read_text(encoding="utf-8"))
@server.api(name="leaderboard_data", queue=False, concurrency_limit=None)
def leaderboard_data() -> dict[str, Any]:
"""Return annualized return metrics, daily return CDFs, and template copy."""
with DATA_LOCK:
return dict(DATA) if DATA else _empty_payload("Data is still loading.")
if __name__ == "__main__":
refresh_data()
scheduler = BackgroundScheduler(daemon=True)
scheduler.add_job(refresh_data, "interval", minutes=10, max_instances=1, coalesce=True)
scheduler.start()
server.launch()