Spaces:
Configuration error
Configuration error
perf: use snapshot_download for faster restore on Space boot
Browse filesReplaces per-file hf_hub_download loop with single snapshot_download call.
Parallel fetch cuts restore time significantly on cold boot.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
11 unmodified lines1213141516171835 unmodified lines54555657585960616263646566676869707172737475767778798081828384858687888911 unmodified linesimport tempfiletry:from huggingface_hub import HfApi, hf_hub_download, list_repo_filesfrom huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundErrorexcept ImportError:print("[sync] huggingface_hub not installed, skipping sync")35 unmodified linesprint("[sync] No HF_TOKEN — skipping restore")returnos.makedirs(DATA_DIR, exist_ok=True)api = get_api()try:files = list(list_repo_files(REPO_ID, repo_type="dataset", token=HF_TOKEN))except RepositoryNotFoundError:print(f"[sync] Dataset repo {REPO_ID} not found — will create on first backup")returnexcept Exception as e:print(f"[sync] restore list error: {e}")returnif not files:print("[sync] Dataset empty — fresh start")returnfor fname in files:try:local_path = os.path.join(DATA_DIR, fname)os.makedirs(os.path.dirname(local_path), exist_ok=True)hf_hub_download(repo_id=REPO_ID,filename=fname,repo_type="dataset",token=HF_TOKEN,local_dir=DATA_DIR,)print(f"[sync] restored {fname}")except Exception as e:print(f"[sync] restore {fname} error: {e}")print("[sync] restore complete")def backup():if not HF_TOKEN:11 unmodified lines1213141516171835 unmodified lines545556575859606162636465666768697071727311 unmodified linesimport tempfiletry:from huggingface_hub import HfApi, snapshot_download, list_repo_filesfrom huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundErrorexcept ImportError:print("[sync] huggingface_hub not installed, skipping sync")35 unmodified linesprint("[sync] No HF_TOKEN — skipping restore")returnos.makedirs(DATA_DIR, exist_ok=True)try:print(f"[sync] downloading snapshot from {REPO_ID}...")snapshot_download(repo_id=REPO_ID,repo_type="dataset",token=HF_TOKEN,local_dir=DATA_DIR,ignore_patterns=["*.lock"],)print("[sync] restore complete")except RepositoryNotFoundError:print(f"[sync] Dataset repo {REPO_ID} not found — will create on first backup")except Exception as e:print(f"[sync] restore error: {e}")def backup():if not HF_TOKEN:
sync.py+11-27