# /// script # requires-python = ">=3.10" # dependencies = ["huggingface_hub>=0.34"] # /// """Refresh the Space's data and rebuild its static page. Designed to run as a scheduled Hugging Face Job. The Space itself is static — nothing runs on the Hub when someone opens it — so the numbers only move when something rebuilds the page from outside. That is this script. It pulls the Space's own sources, re-reads the public Hub API, rebuilds index.html, and pushes back only what actually changed. Run it as a scheduled Job: hf jobs scheduled uv run daily \\ https://huggingface.co/spaces/eustlb/tts-models-on-the-hub/raw/main/refresh.py \\ --secrets HF_TOKEN Or once, locally: python3 refresh.py """ from __future__ import annotations import json import os import subprocess import sys import tempfile from pathlib import Path from huggingface_hub import HfApi, snapshot_download SPACE = os.environ.get("SPACE_ID") or json.loads( (Path(__file__).with_name("task.json")).read_text())["space"] # Rebuilt every run; pushed only when the bytes differ. OUTPUTS = ["data.json", "releases.json", "index.html", "style.css"] # Everything the build needs to run. SOURCES = ["fetch_data.py", "select_releases.py", "build_static.py", "render.py", "task.json", "pins.json"] def main() -> int: api = HfApi() print(f"refreshing {SPACE}", flush=True) with tempfile.TemporaryDirectory(prefix="tts-refresh-") as tmp: work = Path(tmp) # Work from what the Space actually holds, so a change pushed by hand # is picked up rather than overwritten by a stale copy. local = Path( snapshot_download( repo_id=SPACE, repo_type="space", allow_patterns=SOURCES + OUTPUTS, local_dir=work / "space", ) ) before = {f: (local / f).read_bytes() for f in OUTPUTS if (local / f).exists()} env = dict(os.environ, PYTHONPATH=str(local)) for step in ("fetch_data.py", "select_releases.py", "build_static.py"): print(f" $ python {step}", flush=True) result = subprocess.run([sys.executable, step], cwd=local, env=env) if result.returncode != 0: print(f" {step} failed with exit code {result.returncode}", flush=True) return result.returncode changed = [ f for f in OUTPUTS if (local / f).exists() and before.get(f) != (local / f).read_bytes() ] if not changed: print(" no change — nothing to push", flush=True) return 0 print(f" changed: {', '.join(changed)}", flush=True) commit = api.upload_folder( repo_id=SPACE, repo_type="space", folder_path=str(local), allow_patterns=changed, commit_message="Refresh Hub counts and rebuild the page", ) print(f" pushed: {commit.commit_url}", flush=True) return 0 if __name__ == "__main__": raise SystemExit(main())