Michael Rabinovich commited on
Commit ·
b5ad973
1
Parent(s): 628bc9e
refactor: split leaderboard read path into its own module
Browse filesMirrors the shape adyen/DABstep uses on their reference Space (UI
assembly in app.py, business logic in a sibling module). app.py drops
from 224 lines to 158 by extracting the env-driven repo constants,
the LOCAL_RESULTS_PATH + LEADERBOARD_COLS schema, the Hub/local row
loaders, and load_leaderboard() into leaderboard.py. The submit
handler stays in app.py for now; it'll move to a third module
(submit.py) when Step 6 (E) lands the real validation + async eval.
No behaviour change: load_leaderboard is imported back into app.py
and wired into the same gr.Dataframe / refresh button pair as before.
Smoke-tested locally (cadgenbench-space env): app module imports
cleanly, load_leaderboard() returns a dataframe with the expected
columns.
Files changed (2)
- app.py+8-85
- leaderboard.pyAdded+91
11234567345697812139101117181220212213141516172425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394181920"""CADGenBench Leaderboard Space."""CADGenBench Leaderboard Space - Gradio UI assembly.
Step 3 prototype: a hand-crafted ``results.jsonl`` drives the leaderboardtable, and the Submit tab is a UI-only stub. The read path (Step 5) willswap the JSONL for ``datasets.load_dataset(HF_SUBMISSIONS_REPO, 'results')``and the write path (Step 6) will run ``cadgenbench evaluate`` and push aresult row back to the submissions dataset via ``HfApi``.Read path lives in :mod:`leaderboard`. The submit handler is a UI-onlystub here; the real validation + async eval lands in :mod:`submit` aspart of Step 6 (E)."""
from __future__ import annotations
import jsonimport osfrom pathlib import Path
import gradio as grimport pandas as pdfrom huggingface_hub import hf_hub_download
HF_ORG = os.getenv("HF_ORG", "michaelr27")HF_SUBMISSIONS_REPO = os.getenv( "HF_SUBMISSIONS_REPO", f"{HF_ORG}/cadgenbench-submissions"from leaderboard import ( HF_DATA_REPO, HF_SUBMISSIONS_REPO, load_leaderboard,)HF_DATA_REPO = os.getenv("HF_DATA_REPO", f"{HF_ORG}/cadgenbench-data")
LOCAL_RESULTS_PATH = Path(__file__).parent / "results.jsonl"
LEADERBOARD_COLS = [ "submission_name", "submitter_name", "aggregate_score", "validity_rate", "submitted_at", "cadgenbench_version",]
def _load_rows_from_hub() -> list[dict] | None: """Pull results.jsonl from the submissions dataset.
Returns None on any failure so callers can fall back to the local file. """ try: path = hf_hub_download( repo_id=HF_SUBMISSIONS_REPO, filename="results.jsonl", repo_type="dataset", force_download=True, ) return [ json.loads(line) for line in Path(path).read_text().splitlines() if line.strip() ] except Exception as e: # noqa: BLE001 — any failure should fall back print(f"[load_leaderboard] Hub fetch failed ({type(e).__name__}: {e})") return None
def _load_rows_from_local() -> list[dict]: if not LOCAL_RESULTS_PATH.exists(): return [] return [ json.loads(line) for line in LOCAL_RESULTS_PATH.read_text().splitlines() if line.strip() ]
def _fmt_pct(x: float | None) -> str: """Render a 0-1 fraction as 'NN%' (or 'NN.N%' for non-whole values).""" if x is None: return "" pct = float(x) * 100 return f"{pct:.0f}%" if pct == int(pct) else f"{pct:.1f}%"
def load_leaderboard() -> pd.DataFrame: rows = _load_rows_from_hub() if rows is None: print("[load_leaderboard] falling back to local results.jsonl") rows = _load_rows_from_local() if not rows: return pd.DataFrame(columns=LEADERBOARD_COLS) df = pd.DataFrame(rows) cols = [c for c in LEADERBOARD_COLS if c in df.columns] df = ( df[cols] .sort_values("aggregate_score", ascending=False, na_position="last") .reset_index(drop=True) ) if "validity_rate" in df.columns: df["validity_rate"] = df["validity_rate"].map(_fmt_pct) return df
def handle_submit(12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091"""Leaderboard read path.
Loads `results.jsonl` from the submissions dataset on the Hub (or fallsback to the local mirror on any Hub error) and shapes the rows into thedataframe shown on the Leaderboard tab. Module-level constants describethe env-var-driven repo identities that the submit path also consumes."""from __future__ import annotations
import jsonimport osfrom pathlib import Path
import pandas as pdfrom huggingface_hub import hf_hub_download
HF_ORG = os.getenv("HF_ORG", "michaelr27")HF_SUBMISSIONS_REPO = os.getenv( "HF_SUBMISSIONS_REPO", f"{HF_ORG}/cadgenbench-submissions")HF_DATA_REPO = os.getenv("HF_DATA_REPO", f"{HF_ORG}/cadgenbench-data")
LOCAL_RESULTS_PATH = Path(__file__).parent / "results.jsonl"
LEADERBOARD_COLS = [ "submission_name", "submitter_name", "aggregate_score", "validity_rate", "submitted_at", "cadgenbench_version",]
def _load_rows_from_hub() -> list[dict] | None: """Pull results.jsonl from the submissions dataset.
Returns None on any failure so callers can fall back to the local file. """ try: path = hf_hub_download( repo_id=HF_SUBMISSIONS_REPO, filename="results.jsonl", repo_type="dataset", force_download=True, ) return [ json.loads(line) for line in Path(path).read_text().splitlines() if line.strip() ] except Exception as e: # noqa: BLE001 - any failure should fall back print(f"[load_leaderboard] Hub fetch failed ({type(e).__name__}: {e})") return None
def _load_rows_from_local() -> list[dict]: if not LOCAL_RESULTS_PATH.exists(): return [] return [ json.loads(line) for line in LOCAL_RESULTS_PATH.read_text().splitlines() if line.strip() ]
def _fmt_pct(x: float | None) -> str: """Render a 0-1 fraction as 'NN%' (or 'NN.N%' for non-whole values).""" if x is None: return "" pct = float(x) * 100 return f"{pct:.0f}%" if pct == int(pct) else f"{pct:.1f}%"
def load_leaderboard() -> pd.DataFrame: rows = _load_rows_from_hub() if rows is None: print("[load_leaderboard] falling back to local results.jsonl") rows = _load_rows_from_local() if not rows: return pd.DataFrame(columns=LEADERBOARD_COLS) df = pd.DataFrame(rows) cols = [c for c in LEADERBOARD_COLS if c in df.columns] df = ( df[cols] .sort_values("aggregate_score", ascending=False, na_position="last") .reset_index(drop=True) ) if "validity_rate" in df.columns: df["validity_rate"] = df["validity_rate"].map(_fmt_pct) return dfapp.py+8-85
leaderboard.pyAdded+91