| |
| |
| |
| |
| |
| |
| |
| |
| |
| """Build the public-safe derived view of the Hub's agent-usage telemetry. |
| |
| The source dataset id is injected via the AGENT_USAGE_SRC env var (on Jobs: |
| `-e AGENT_USAGE_SRC=...`), so this public script never names it. |
| |
| This is the scheduled HF Job body. It: |
| 1. fetches the private source (monthly + daily parquets) |
| 2. keeps only the public columns (relative shares; raw counts are never |
| published) and only registered harness rows (the rest fold into `unknown`) |
| 3. writes derived parquets, renders the card PNGs, and pushes everything |
| (data + charts + card + this script) to the public dataset repo |
| """ |
|
|
| import os |
| from datetime import date |
| from pathlib import Path |
|
|
| import pandas as pd |
| from huggingface_hub import snapshot_download |
| from huggingface_hub.constants import ENDPOINT |
| from huggingface_hub.utils import get_session |
|
|
| try: |
| import agent_usage as au |
| except ModuleNotFoundError: |
| |
| |
| import sys |
|
|
| from huggingface_hub import hf_hub_download |
|
|
| _mod = hf_hub_download( |
| os.environ["AGENT_USAGE_PUSH_REPO"], "agent_usage.py", repo_type="dataset" |
| ) |
| sys.path.insert(0, str(Path(_mod).parent)) |
| import agent_usage as au |
|
|
| SRC = os.environ.get("AGENT_USAGE_SRC") |
| if not SRC: |
| raise SystemExit("Set AGENT_USAGE_SRC to the source dataset id (on Jobs: -e AGENT_USAGE_SRC=...)") |
| |
| |
| KEEP = {"month", "day", "agent", "pct_requests", "pct_users"} |
| |
| |
| PUBLISH_SOURCE = "huggingface_hub" |
| ROLLOUT_MONTH = "2026-04" |
|
|
| |
| |
| |
| |
| |
| _resp = get_session().get(f"{ENDPOINT}/api/agent-harnesses", timeout=10) |
| _resp.raise_for_status() |
| PUBLISH_AGENTS = set(_resp.json()["harnesses"]) | {"unknown"} |
|
|
| HERE = Path(__file__).parent |
| OUT = HERE / "preview" |
| DATA = OUT / "data" |
|
|
|
|
| def derive(folder: str, key_col: str) -> pd.DataFrame: |
| """Pull one granularity from the source, drop totals, write derived parquets.""" |
| src_root = Path( |
| snapshot_download(SRC, repo_type="dataset", allow_patterns=f"data/{folder}/*.parquet") |
| ) |
| out_dir = DATA / folder |
| out_dir.mkdir(parents=True, exist_ok=True) |
|
|
| frames = [] |
| for parquet in sorted((src_root / "data" / folder).glob("*.parquet")): |
| |
| if folder == "monthly" and parquet.stem < ROLLOUT_MONTH: |
| continue |
| df = pd.read_parquet(parquet) |
| if key_col not in df.columns: |
| df[key_col] = parquet.stem |
| if "source" in df.columns: |
| df = df[df["source"] == PUBLISH_SOURCE] |
| df = df[[c for c in df.columns if c in KEEP]] |
| |
| df.loc[~df["agent"].isin(PUBLISH_AGENTS), "agent"] = "unknown" |
| group_cols = [c for c in df.columns if not c.startswith("pct_")] |
| df = df.groupby(group_cols, as_index=False).sum() |
| df.to_parquet(out_dir / parquet.name, index=False) |
| frames.append(df) |
|
|
| if not frames: |
| raise SystemExit(f"No {folder} parquets found") |
| all_df = pd.concat(frames, ignore_index=True) |
|
|
| |
| |
| extras = set(all_df.columns) - KEEP |
| if extras: |
| raise SystemExit(f"Leak: non-allowlisted columns survived in {folder}: {extras}") |
| if not {key_col, "agent", "pct_requests"} <= set(all_df.columns): |
| raise SystemExit(f"Source schema drift: expected share columns missing in {folder}") |
|
|
| print(f"✓ {folder}: {len(frames)} parquets → {out_dir}/ ({len(all_df)} rows)") |
| return all_df |
|
|
|
|
| |
|
|
| monthly = derive("monthly", "month") |
| daily = derive("daily", "day") |
|
|
| |
| gs = monthly.groupby("month")["pct_requests"].sum() |
| if not ((gs > 95) & (gs < 105)).all(): |
| print(f"⚠ monthly pct_requests group sums look off: {gs.to_dict()}") |
|
|
| latest = au.latest_month(monthly) |
| leaders = au.top_agents(monthly, n=5) |
| print(f"\nLatest month: {latest}") |
| print(au.leaderboard_table(monthly).to_string(index=False)) |
| print(f"\nTrend cohort (top 5, {latest}): {leaders}") |
|
|
| |
|
|
| au.save_png(au.leaderboard_figure(monthly), OUT / "leaderboard.png") |
| au.save_png(au.trend_figure(daily, agents=leaders), OUT / "trend.png") |
| print(f"\n✓ {OUT/'leaderboard.png'}") |
| print(f"✓ {OUT/'trend.png'}") |
|
|
| |
| |
| |
| |
| |
|
|
| PUSH_REPO = os.environ.get("AGENT_USAGE_PUSH_REPO") |
| if PUSH_REPO: |
| from huggingface_hub import CommitOperationAdd, HfApi |
|
|
| api = HfApi() |
| |
| |
| api.create_repo(PUSH_REPO, repo_type="dataset", private=True, exist_ok=True) |
|
|
| |
| |
| if api.file_exists( |
| PUSH_REPO, f"data/monthly/{latest}.parquet", repo_type="dataset" |
| ) and not os.environ.get("AGENT_USAGE_FORCE"): |
| print( |
| f"\n✓ {PUSH_REPO} already has {latest} — nothing new, skipping push " |
| "(AGENT_USAGE_FORCE=1 overrides)" |
| ) |
| raise SystemExit(0) |
|
|
| ops = [] |
| for parquet in sorted(DATA.rglob("*.parquet")): |
| ops.append(CommitOperationAdd(str(parquet.relative_to(OUT)), str(parquet))) |
| for png in ("leaderboard.png", "trend.png"): |
| ops.append(CommitOperationAdd(f"assets/{png}", str(OUT / png))) |
| |
| |
| |
| ops.append(CommitOperationAdd("build_local.py", str(Path(__file__).resolve()))) |
| ops.append(CommitOperationAdd("agent_usage.py", au.__file__)) |
|
|
| card_tpl = HERE / "card" / "README.md" |
| if not card_tpl.exists(): |
| from huggingface_hub import hf_hub_download |
|
|
| card_tpl = Path(hf_hub_download(PUSH_REPO, "card/README.md", repo_type="dataset")) |
| ops.append(CommitOperationAdd("card/README.md", str(card_tpl))) |
|
|
| |
| card = ( |
| card_tpl.read_text() |
| .replace("{{LATEST_MONTH}}", latest) |
| .replace("{{GENERATED}}", str(date.today())) |
| ) |
| ops.append(CommitOperationAdd("README.md", card.encode())) |
|
|
| commit = api.create_commit( |
| repo_id=PUSH_REPO, |
| repo_type="dataset", |
| operations=ops, |
| commit_message=f"Update derived agent-usage shares (latest month: {latest})", |
| ) |
| print(f"\n✓ pushed {len(ops)} files → {commit.commit_url}") |
| else: |
| print("\n(no AGENT_USAGE_PUSH_REPO set — local build only, nothing pushed)") |
|
|