| import json | |
| from pathlib import Path | |
| import gradio as gr | |
| import pandas as pd | |
| TITLE = 'EASI Leaderboard' | |
| DATA_PATH = Path("leaderboard.csv") | |
| META_PATH = Path("benchmark.json") | |
| DISPLAY_TEXT_LIMIT = 96 | |
| df = pd.read_csv(DATA_PATH) | |
| if "score" in df.columns: | |
| df["score"] = pd.to_numeric(df["score"], errors="coerce") | |
| meta = {} | |
| if META_PATH.exists(): | |
| try: | |
| meta = json.loads(META_PATH.read_text(encoding="utf-8")) | |
| except Exception: | |
| meta = {} | |
| def metric_narrative(): | |
| likely_metrics = (((meta.get("analysis") or {}).get("likely_metrics")) or []) | |
| if likely_metrics: | |
| return ( | |
| "This benchmark is ranked by the primary `score` column (descending). " | |
| "Reported benchmark metrics include: " + ", ".join([str(x) for x in likely_metrics]) + "." | |
| ) | |
| known_non_metrics = { | |
| "model_name", | |
| "score", | |
| "source_title", | |
| "source_url", | |
| "notes", | |
| } | |
| metric_like = [c for c in df.columns if c not in known_non_metrics] | |
| if metric_like: | |
| return ( | |
| "This benchmark is ranked by the primary `score` column (descending). " | |
| "Table columns include: " + ", ".join(metric_like) + "." | |
| ) | |
| return "This benchmark is ranked by the primary `score` column (descending)." | |
| def render(query, top_k): | |
| x = df.copy() | |
| if query.strip(): | |
| x = x[x["model_name"].astype(str).str.contains(query.strip(), case=False, na=False)] | |
| if "score" in x.columns: | |
| x = x.sort_values(by=["score"], ascending=False, na_position="last") | |
| x = x.head(int(top_k)).reset_index(drop=True) | |
| x = compact_for_display(x) | |
| x.insert(0, "display_rank", x.index + 1) | |
| return x | |
| def compact_for_display(frame): | |
| out = frame.copy() | |
| for col in out.columns: | |
| if out[col].dtype != object: | |
| continue | |
| out[col] = out[col].apply(truncate_cell) | |
| return out | |
| def truncate_cell(value): | |
| if value is None: | |
| return value | |
| text = str(value) | |
| if len(text) <= DISPLAY_TEXT_LIMIT: | |
| return text | |
| return text[: DISPLAY_TEXT_LIMIT - 3] + "..." | |
| with gr.Blocks(title=TITLE) as demo: | |
| gr.Markdown(f"# {TITLE}") | |
| gr.Markdown(metric_narrative()) | |
| gr.Markdown(f"Rows: {len(df)}") | |
| with gr.Row(): | |
| gr.DownloadButton("Download CSV", value=str(DATA_PATH)) | |
| if Path("leaderboard.json").exists(): | |
| gr.DownloadButton("Download JSON", value="leaderboard.json") | |
| with gr.Row(): | |
| query = gr.Textbox(label="Model contains") | |
| top_k = gr.Slider(minimum=5, maximum=500, step=1, value=100, label="Top K") | |
| table = gr.Dataframe( | |
| value=render("", 100), | |
| interactive=False, | |
| wrap=False, | |
| row_count=(20, "fixed"), | |
| ) | |
| query.change(render, [query, top_k], table) | |
| top_k.change(render, [query, top_k], table) | |
| demo.launch() | |