amber / tools /push_tools.py
amongglue's picture
Corpus index: 104 datasets, 5,735 h
c56d466 verified
Raw History Blame Contribute Delete
3.33 kB
#!/usr/bin/env python3
"""Publish the toolchain to the Hub in a single commit.
The Hub allows 1000 API requests per five minutes across the whole account,
and the upload lanes are already spending most of that budget on data. Sending
the toolchain one file at a time competes with them for no reason: this batches
every changed file into one commit, which is one round trip regardless of how
many files moved.
"""
from __future__ import annotations
import argparse, os, sys, time
from pathlib import Path
os.environ.setdefault("HF_HUB_DISABLE_XET", "1")
ROOT = Path(__file__).resolve().parent.parent
REPO_ID = "amongglue/amber"
# Everything needed to reproduce the corpus, mapped to its path in the repo.
SUBDIRS = ("eegx", "ingest", "tools", "tests")
EXTRA = {
"data/content_tiers.json": "provenance/content_tiers.json",
"work/discovery/dryad.jsonl": "provenance/discovery_dryad.jsonl",
"work/discovery/figshare.jsonl": "provenance/discovery_figshare.jsonl",
"work/discovery/osf.jsonl": "provenance/discovery_osf.jsonl",
"work/discovery/zenodo.jsonl": "provenance/discovery_zenodo.jsonl",
"work/discovery/figshare_files.jsonl": "provenance/queue_figshare.jsonl",
"work/discovery/osf_files.jsonl": "provenance/queue_osf.jsonl",
"SPEC.md": "SPEC.md",
}
def operations(only: list[str] | None):
from huggingface_hub import CommitOperationAdd
ops = []
for sub in SUBDIRS:
d = ROOT / sub
if not d.is_dir():
continue
for p in sorted(d.rglob("*")):
if p.suffix not in (".py", ".sh") or "__pycache__" in p.parts:
continue
rel = p.relative_to(ROOT).as_posix()
if only and not any(o in rel for o in only):
continue
ops.append(CommitOperationAdd(f"tools/{rel}", str(p)))
for local, remote in EXTRA.items():
p = ROOT / local
if not p.exists() or p.stat().st_size == 0:
continue
if only and not any(o in local for o in only):
continue
ops.append(CommitOperationAdd(remote, str(p)))
return ops
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--only", nargs="*", default=None,
help="substrings of paths to include")
ap.add_argument("-m", "--message", default="update toolchain")
ap.add_argument("--dry-run", action="store_true")
a = ap.parse_args()
ops = operations(a.only)
if not ops:
print("nothing to push")
return 0
print(f"{len(ops)} files:")
for o in ops[:40]:
print(" ", o.path_in_repo)
if len(ops) > 40:
print(f" ... and {len(ops)-40} more")
if a.dry_run:
return 0
from huggingface_hub import HfApi
api = HfApi()
for attempt in range(5):
try:
api.create_commit(repo_id=REPO_ID, repo_type="dataset",
operations=ops, commit_message=a.message)
print("pushed in one commit")
return 0
except Exception as e:
msg = str(e)[:120]
print(f"attempt {attempt+1}: {type(e).__name__}: {msg}")
if "429" not in msg and "rate" not in msg.lower():
return 1
time.sleep(90)
return 1
if __name__ == "__main__":
sys.exit(main())