#!/usr/bin/env python3
"""
Build leaderboard data for the llm-bench.io HuggingFace Static Space.
Fetches the public benchmarks API, aggregates by model + VRAM tier
(last 30 days, >= 3 runs, completed quality assessments only), then:
1. writes data.json (enables interactive filter/sort on the live Space)
2. renders index.html (pre-rendered rows so the no-JS org-card view works)
Run: python3 build.py
"""
import json
import urllib.parse
import urllib.request
from collections import defaultdict
from datetime import datetime, timedelta, timezone
API = "https://llm-bench.io/api/benchmarks"
WINDOW_DAYS = 30
MIN_RUNS = 3
HERE = "/tmp/space-readme"
SCENARIOS = {
"coding_agent": "Coding",
"openclaw": "Agent",
"roleplay": "Role-Play",
"research": "Research",
}
def fetch_all():
records, offset, total = [], 0, None
while True:
url = f"{API}?limit=1000&offset={offset}"
req = urllib.request.Request(url, headers={"User-Agent": "llm-bench-hub-leaderboard/1.0"})
with urllib.request.urlopen(req, timeout=60) as r:
page = json.load(r)
total = page["total"]
records.extend(page["benchmarks"])
offset += len(page["benchmarks"])
if len(records) >= total or not page["benchmarks"]:
break
return total, records
def max_vram(b):
gpus = b.get("gpuList") or []
return max((g.get("vram_gb") or 0) for g in gpus) if gpus else 0
def hardware_label(b):
gpus = b.get("gpuList") or []
names = []
for g in gpus:
n = g.get("name") or ""
if n and n not in names:
names.append(n)
if names:
return " + ".join(names)
cpu = (b.get("cpuName") or "").split(" @ ")[0].strip()
return cpu or "CPU"
def esc(s):
return (
str(s)
.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace('"', """)
.replace("'", "'")
)
def qcolor(v):
if v is None:
return "#f87171"
if v >= 85:
return "#4ade80"
if v >= 75:
return "#facc15"
return "#f87171"
def render_static(out):
"""Fill template.html placeholders with pre-rendered rows (no-JS view)."""
rows = out["rows"]
scen_keys = ["coding_agent", "openclaw", "roleplay", "research"]
trs = []
for r in rows:
link = "https://llm-bench.io/benchmarks?model=" + urllib.parse.quote(r["model"], safe="")
q = r.get("quality")
if q is not None:
qcell = (
'
'
f''
f"{q:.1f} | "
)
else:
qcell = '– | '
sc_cells = []
for k in scen_keys:
v = r.get("scenarios", {}).get(k)
sc_cells.append(f'{v:.1f} | ' if v is not None else '– | ')
speed = r.get("speed")
speed_cell = f'{speed:.1f} | ' if speed is not None else '– | '
trs.append(
' \n'
' | \n'
f' {r["vram"]} GB | \n'
f' {r["runs"]} | \n'
f" {qcell}\n"
f" {speed_cell}\n"
+ " ".join(sc_cells)
+ f'\n {r["updated"]} | \n'
"
"
)
meta = (
f'{len(rows)} models'
f'{out["inWindow"]} submissions in window'
f'of {out["totalSubmissions"]} total on llm-bench.io'
)
gen = datetime.fromisoformat(out["generatedAt"]).strftime("%d %b %Y")
with open(f"{HERE}/template.html") as f:
html = f.read()
html = html.replace("__ROWS__", "\n".join(trs))
html = html.replace("__META__", meta)
html = html.replace("__WIN__", str(out["windowDays"]))
html = html.replace("__MINR__", str(out["minRuns"]))
html = html.replace("__GEN__", gen)
with open(f"{HERE}/index.html", "w") as f:
f.write(html)
def main():
total, records = fetch_all()
cutoff = datetime.now(timezone.utc) - timedelta(days=WINDOW_DAYS)
def ts(b):
t = b.get("timestamp")
if not t:
return None
return datetime.fromisoformat(t.replace("Z", "+00:00"))
in_window = [
b for b in records
if (t := ts(b)) is not None and t >= cutoff and max_vram(b) > 0 and not b.get("flaggedBroken")
]
groups = defaultdict(list)
for b in in_window:
groups[(max_vram(b), b.get("modelName"))].append(b)
def avg(items):
vals = [x for x in items if x is not None]
return round(sum(vals) / len(vals), 1) if vals else None
rows = []
for (vram, model), bs in groups.items():
if len(bs) < MIN_RUNS:
continue
# completed quality scores per scenario
scen_scores = {k: [] for k in SCENARIOS}
for b in bs:
for q in b.get("qualityAssessments") or []:
if q.get("status") == "completed" and q.get("overallKpiScore") is not None:
k = q.get("scenarioKey")
if k in scen_scores:
scen_scores[k].append(q["overallKpiScore"])
speeds = [b.get("tests", {}).get("tokenGenerationSpeed") for b in bs]
speed = avg(speeds)
quality = avg([b.get("avgQualityScore") for b in bs])
last = max(b["timestamp"] for b in bs if b.get("timestamp"))
hw = hardware_label(bs[0])
row = {
"vram": vram,
"model": model,
"hardware": hw,
"runs": len(bs),
"quality": quality,
"speed": speed,
"scenarios": {k: avg(v) for k, v in scen_scores.items() if v},
"updated": last[:10],
}
rows.append(row)
rows.sort(key=lambda r: (r["quality"] or 0, r["speed"] or 0), reverse=True)
out = {
"generatedAt": datetime.now(timezone.utc).isoformat(timespec="seconds"),
"windowDays": WINDOW_DAYS,
"minRuns": MIN_RUNS,
"totalSubmissions": total,
"inWindow": len(in_window),
"scenarios": SCENARIOS,
"rows": rows,
}
with open(f"{HERE}/data.json", "w") as f:
json.dump(out, f, separators=(",", ":"))
print(f"rows={len(rows)} window_records={len(in_window)} total={total} -> {HERE}/data.json")
render_static(out)
print("rendered index.html from template.html")
if __name__ == "__main__":
main()