Download code/scripts/hf_sync.py from hello-test1/PhotonTTS: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/scripts/hf_sync.py
- Command line
-
hf download hf://hello-test1/PhotonTTS/code/scripts/hf_sync.py
-
curl -L -o hf_sync.py https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/scripts/hf_sync.py
4.26 kB
| """Back up local artifacts to the public Hugging Face repos, keeping total storage under a cap. | |
| model repo <user>/PhotonTTS code snapshot + checkpoints (runs/<name>/ckpt-*) | |
| dataset repo <user>/PhotonTTS-data latent shards, manifests, processed metadata | |
| Usage (on the GPU box, inside tmux): | |
| python scripts/hf_sync.py --runs /root/runs --data /root/data/shards --every 3600 | |
| Raw audio is never uploaded by this script. The cap counts both repos together. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import os | |
| import time | |
| from pathlib import Path | |
| from huggingface_hub import HfApi | |
| TB = 1000**4 | |
| CODE_IGNORE = ["**/__pycache__/**", "**/.pytest_cache/**", ".venv/**", "runs/**", "data/**", "*.pyc", ".git/**"] | |
| def repo_bytes(api: HfApi, repo_id: str, repo_type: str) -> int: | |
| total = 0 | |
| for f in api.list_repo_tree(repo_id, repo_type=repo_type, recursive=True): | |
| total += getattr(f, "size", 0) or 0 | |
| return total | |
| def dir_bytes(path: Path) -> int: | |
| return sum(p.stat().st_size for p in path.rglob("*") if p.is_file()) | |
| def keep_last_checkpoints(run_dir: Path, keep: int) -> list[Path]: | |
| ckpts = sorted((p for p in run_dir.glob("ckpt-*") if p.is_dir()), key=lambda p: p.stat().st_mtime) | |
| return ckpts[-keep:] | |
| def sync_once(args, api: HfApi) -> None: | |
| user = api.whoami()["name"] | |
| model_repo, data_repo = f"{user}/{args.model_repo}", f"{user}/{args.data_repo}" | |
| used = repo_bytes(api, model_repo, "model") + repo_bytes(api, data_repo, "dataset") | |
| cap = int(args.cap_tb * TB) | |
| print(f"[hf_sync] used {used / TB:.3f} TB of {args.cap_tb} TB cap", flush=True) | |
| if args.code: | |
| api.upload_folder(repo_id=model_repo, folder_path=args.code, path_in_repo="code", | |
| ignore_patterns=CODE_IGNORE, commit_message="sync code") | |
| if args.runs and Path(args.runs).exists(): | |
| for run in sorted(p for p in Path(args.runs).iterdir() if p.is_dir()): | |
| for ck in keep_last_checkpoints(run, args.keep): | |
| size = dir_bytes(ck) | |
| if used + size > cap: | |
| print(f"[hf_sync] SKIP {ck} ({size / 1e9:.1f} GB): would exceed cap", flush=True) | |
| continue | |
| api.upload_folder(repo_id=model_repo, folder_path=str(ck), path_in_repo=f"runs/{run.name}/{ck.name}", | |
| commit_message=f"checkpoint {run.name}/{ck.name}") | |
| used += size # upper bound; HF dedupes unchanged files | |
| logs = run / "logs" | |
| if logs.exists(): | |
| api.upload_folder(repo_id=model_repo, folder_path=str(logs), path_in_repo=f"runs/{run.name}/logs", | |
| commit_message=f"logs {run.name}") | |
| if args.data and Path(args.data).exists(): | |
| size = dir_bytes(Path(args.data)) | |
| if used + size > cap: | |
| print(f"[hf_sync] data dir {size / TB:.2f} TB may exceed cap; uploading anyway is refused", flush=True) | |
| else: | |
| # resumable, chunked, multi-worker upload designed for TB-scale folders | |
| api.upload_large_folder(repo_id=data_repo, repo_type="dataset", folder_path=args.data, | |
| ignore_patterns=["**/raw/**", "*.wav", "*.flac", "*.mp3", "*.opus"]) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--code", default=str(Path(__file__).resolve().parents[1])) | |
| ap.add_argument("--runs", default=None) | |
| ap.add_argument("--data", default=None) | |
| ap.add_argument("--model-repo", default="PhotonTTS") | |
| ap.add_argument("--data-repo", default="PhotonTTS-data") | |
| ap.add_argument("--cap-tb", type=float, default=float(os.environ.get("PHOTON_HF_CAP_TB", 7.0))) | |
| ap.add_argument("--keep", type=int, default=2, help="checkpoints per run to keep on HF") | |
| ap.add_argument("--every", type=int, default=0, help="seconds between syncs; 0 = once") | |
| args = ap.parse_args() | |
| api = HfApi() | |
| while True: | |
| try: | |
| sync_once(args, api) | |
| except Exception as e: # never crash the sync loop on a transient HF error | |
| print(f"[hf_sync] error: {e!r}", flush=True) | |
| if not args.every: | |
| break | |
| time.sleep(args.every) | |
| if __name__ == "__main__": | |
| main() | |