code: sync from git
Browse files- code/hfeco/hubstats.py +71 -0
code/hfeco/hubstats.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Past downloads of our repos, from the git history of cfahlgren1/hub-stats.
|
| 2 |
+
|
| 3 |
+
hub-stats commits a snapshot of every Hub model (models.parquet) about once a day, so its
|
| 4 |
+
history holds each repo's all-time downloads (`downloadsAllTime`, present from 2025-02-27) and
|
| 5 |
+
trailing 30-day downloads (`downloads`) day by day. `backfill` reads one revision every `every`
|
| 6 |
+
days, keeps only the given repos, and writes raw/hubstats.parquet (day, repo_id, d30, dla).
|
| 7 |
+
It runs once; our own daily snapshots carry the series on from its last day.
|
| 8 |
+
"""
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
import pandas as pd
|
| 12 |
+
|
| 13 |
+
from . import store
|
| 14 |
+
from .hub import api
|
| 15 |
+
|
| 16 |
+
SRC = "cfahlgren1/hub-stats"
|
| 17 |
+
URL = "https://huggingface.co/datasets/" + SRC + "/resolve/{sha}/models.parquet"
|
| 18 |
+
PATH = store.LOCAL / "raw" / "hubstats.parquet"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def revisions(every=7, since="2025-01-01"):
|
| 22 |
+
"""(day, sha) of models.parquet commits: the day's last commit, one day per `every` days,
|
| 23 |
+
and always the newest."""
|
| 24 |
+
by_day = {}
|
| 25 |
+
for c in sorted(api.list_repo_commits(SRC, repo_type="dataset"), key=lambda c: c.created_at):
|
| 26 |
+
if "models.parquet" in c.title:
|
| 27 |
+
by_day[str(c.created_at)[:10]] = c.commit_id
|
| 28 |
+
days = sorted(d for d in by_day if d >= since)
|
| 29 |
+
out, nxt = [], ""
|
| 30 |
+
for d in days:
|
| 31 |
+
if d >= nxt:
|
| 32 |
+
out.append((d, by_day[d]))
|
| 33 |
+
nxt = (pd.Timestamp(d) + pd.Timedelta(days=every)).strftime("%Y-%m-%d")
|
| 34 |
+
if days and out[-1][0] != days[-1]:
|
| 35 |
+
out.append((days[-1], by_day[days[-1]]))
|
| 36 |
+
return out
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def backfill(ids, every=7, since="2025-01-01", log=print):
|
| 40 |
+
import duckdb
|
| 41 |
+
|
| 42 |
+
cache = store.LOCAL / "hubstats"
|
| 43 |
+
cache.mkdir(parents=True, exist_ok=True)
|
| 44 |
+
con = duckdb.connect()
|
| 45 |
+
con.sql("SET http_timeout=120; SET http_retries=8; SET http_retry_wait_ms=2000")
|
| 46 |
+
con.register("ours", pd.DataFrame({"id": sorted(ids)}))
|
| 47 |
+
revs = revisions(every, since)
|
| 48 |
+
log(f"hub-stats: {len(ids)} repos, {len(revs)} revisions")
|
| 49 |
+
t0 = time.time()
|
| 50 |
+
for day, sha in revs:
|
| 51 |
+
out = cache / f"{day}.parquet"
|
| 52 |
+
if out.exists():
|
| 53 |
+
continue
|
| 54 |
+
url = URL.format(sha=sha)
|
| 55 |
+
for attempt in range(4):
|
| 56 |
+
try:
|
| 57 |
+
cols = {r[0] for r in con.sql(f"SELECT name FROM parquet_schema('{url}')").fetchall()}
|
| 58 |
+
dla = "downloadsAllTime" if "downloadsAllTime" in cols else "NULL::BIGINT"
|
| 59 |
+
df = con.sql(f"""SELECT id AS repo_id, downloads AS d30, {dla} AS dla
|
| 60 |
+
FROM read_parquet('{url}') WHERE id IN (SELECT id FROM ours)""").df()
|
| 61 |
+
df.insert(0, "day", day)
|
| 62 |
+
df.to_parquet(out, index=False)
|
| 63 |
+
break
|
| 64 |
+
except Exception as e: # network: retry the revision, then leave it out
|
| 65 |
+
log(f"hub-stats {day} attempt {attempt + 1}: {type(e).__name__}: {str(e)[:120]}")
|
| 66 |
+
time.sleep(10 * (attempt + 1))
|
| 67 |
+
df = pd.concat([pd.read_parquet(p) for p in sorted(cache.glob("*.parquet"))], ignore_index=True)
|
| 68 |
+
PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 69 |
+
df.to_parquet(PATH, index=False)
|
| 70 |
+
log(f"hub-stats: {len(df)} rows over {df.day.nunique()} days in {time.time() - t0:.0f}s")
|
| 71 |
+
return PATH
|