Michael Rabinovich commited on
Commit
9fcb684
·
1 Parent(s): 855bb8a

leaderboard: fetch results.jsonl via direct cache-busted HTTPS

Browse files

hf_hub_download (even with force_download=True) hands back stale
bytes for several minutes after a commit: the local disk cache,
revision pinning, and the Hub's CDN-fronted resolve endpoint all
add their own staleness windows. End effect: pending and failed
rows landed by the submit worker were on the Hub but invisible
on the leaderboard tab until you waited it out, defeating the
whole point of Refresh.

Switch _load_rows_from_hub to a plain requests.get against the
dataset's resolve/main URL with Cache-Control: no-cache and a
millisecond cache-bust query param. Auth via huggingface_hub
get_token() so the same code reads the still-private submissions
dataset from the Space (HF_TOKEN secret) and from local dev
(~/.cache/huggingface/token) without forking the code path.

Adds requests to requirements.txt (was a transitive dep via
huggingface_hub; making it explicit so a future upstream change
doesn't silently drop it).

Files changed (2) hide show
  1. leaderboard.py +38 -15
  2. requirements.txt +1 -0
leaderboard.py CHANGED
@@ -8,11 +8,16 @@ the env-var-driven repo identities that the submit path also consumes.
8
  from __future__ import annotations
9
 
10
  import json
 
11
  import os
 
12
  from pathlib import Path
13
 
14
  import pandas as pd
15
- from huggingface_hub import hf_hub_download
 
 
 
16
 
17
  HF_ORG = os.getenv("HF_ORG", "michaelr27")
18
  HF_SUBMISSIONS_REPO = os.getenv(
@@ -21,6 +26,8 @@ HF_SUBMISSIONS_REPO = os.getenv(
21
  HF_DATA_REPO = os.getenv("HF_DATA_REPO", f"{HF_ORG}/cadgenbench-data")
22
 
23
  LOCAL_RESULTS_PATH = Path(__file__).parent / "results.jsonl"
 
 
24
 
25
  LEADERBOARD_COLS = [
26
  "status",
@@ -37,24 +44,40 @@ FAILED_CELL_TAG = "✗ failed"
37
 
38
 
39
  def _load_rows_from_hub() -> list[dict] | None:
40
- """Pull results.jsonl from the submissions dataset.
41
-
42
- Returns None on any failure so callers can fall back to the local file.
 
 
 
 
 
 
 
 
 
43
  """
 
 
 
 
 
 
 
 
44
  try:
45
- path = hf_hub_download(
46
- repo_id=HF_SUBMISSIONS_REPO,
47
- filename="results.jsonl",
48
- repo_type="dataset",
49
- force_download=True,
50
  )
51
- return [
52
- json.loads(line)
53
- for line in Path(path).read_text().splitlines()
54
- if line.strip()
55
- ]
56
  except Exception as e: # noqa: BLE001 - any failure should fall back
57
- print(f"[load_leaderboard] Hub fetch failed ({type(e).__name__}: {e})")
58
  return None
59
 
60
 
 
8
  from __future__ import annotations
9
 
10
  import json
11
+ import logging
12
  import os
13
+ import time
14
  from pathlib import Path
15
 
16
  import pandas as pd
17
+ import requests
18
+ from huggingface_hub import get_token
19
+
20
+ logger = logging.getLogger(__name__)
21
 
22
  HF_ORG = os.getenv("HF_ORG", "michaelr27")
23
  HF_SUBMISSIONS_REPO = os.getenv(
 
26
  HF_DATA_REPO = os.getenv("HF_DATA_REPO", f"{HF_ORG}/cadgenbench-data")
27
 
28
  LOCAL_RESULTS_PATH = Path(__file__).parent / "results.jsonl"
29
+ RESULTS_FILENAME = "results.jsonl"
30
+ HUB_FETCH_TIMEOUT_SECONDS = 30
31
 
32
  LEADERBOARD_COLS = [
33
  "status",
 
44
 
45
 
46
  def _load_rows_from_hub() -> list[dict] | None:
47
+ """Pull results.jsonl from the submissions dataset via raw HTTPS.
48
+
49
+ Avoids :func:`huggingface_hub.hf_hub_download` because its layered
50
+ caching (local disk cache + revision pinning + the Hub's own
51
+ CDN-fronted resolve endpoint) can hand back stale bytes for a few
52
+ minutes even with ``force_download=True``, which makes pending
53
+ rows look like they never landed. A direct GET with a cache-bust
54
+ query param and ``Cache-Control: no-cache`` consistently sees the
55
+ latest commit on the dataset's ``main`` branch within seconds.
56
+
57
+ Returns None on any failure so callers can fall back to the local
58
+ mirror.
59
  """
60
+ url = (
61
+ f"https://huggingface.co/datasets/{HF_SUBMISSIONS_REPO}"
62
+ f"/resolve/main/{RESULTS_FILENAME}"
63
+ )
64
+ headers = {"Cache-Control": "no-cache"}
65
+ token = get_token()
66
+ if token:
67
+ headers["Authorization"] = f"Bearer {token}"
68
  try:
69
+ r = requests.get(
70
+ url,
71
+ headers=headers,
72
+ params={"_cb": str(int(time.time() * 1000))},
73
+ timeout=HUB_FETCH_TIMEOUT_SECONDS,
74
  )
75
+ r.raise_for_status()
76
+ rows = [json.loads(line) for line in r.text.splitlines() if line.strip()]
77
+ logger.info("Loaded %d rows from Hub", len(rows))
78
+ return rows
 
79
  except Exception as e: # noqa: BLE001 - any failure should fall back
80
+ logger.warning("Hub fetch failed (%s: %s)", type(e).__name__, e)
81
  return None
82
 
83
 
requirements.txt CHANGED
@@ -2,3 +2,4 @@ gradio==6.14.0
2
  pandas>=2.0
3
  huggingface_hub>=0.27.0
4
  datasets>=3.0
 
 
2
  pandas>=2.0
3
  huggingface_hub>=0.27.0
4
  datasets>=3.0
5
+ requests>=2.31