#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Upload one release folder to Hugging Face. Dry-run unless --confirm is passed. Copy this file into a release folder, set REPO_ID and REPO_TYPE, then run: python upload.py --write-sums # regenerate SHA256SUMS over public files only python upload.py --confirm # upload Only the public file set is ever uploaded (an explicit commit), so BUILD.md, upload.py, .gitignore, .gitattributes and caches can never leak to the Hub. """ import argparse import hashlib import sys from pathlib import Path REPO_ID = "failed09/bashkir-ngram-index" REPO_TYPE = "dataset" # model | dataset INTERNAL_NAMES = {"upload.py", "BUILD.md", "README_TEMPLATE.md", ".gitignore", ".gitattributes"} INTERNAL_SUFFIXES = (".pyc", ".log", ".err.log") if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8") def is_internal(path: Path) -> bool: return ( path.name in INTERNAL_NAMES or "__pycache__" in path.parts or path.name.endswith(INTERNAL_SUFFIXES) ) def release_files(here: Path): return sorted( p.relative_to(here).as_posix() for p in here.rglob("*") if p.is_file() and not is_internal(p) ) def write_sums(here: Path, files) -> None: lines = [] for name in files: if name == "SHA256SUMS": continue digest = hashlib.sha256((here / name).read_bytes()).hexdigest() lines.append(f"{digest} {name}") (here / "SHA256SUMS").write_text("\n".join(lines) + "\n", encoding="utf-8") print(f"SHA256SUMS written for {len(lines)} public files.") def main(): ap = argparse.ArgumentParser() ap.add_argument("--repo-id", default=REPO_ID) ap.add_argument("--repo-type", default=REPO_TYPE, choices=["model", "dataset"]) ap.add_argument("--message", default=None, help="commit message override") ap.add_argument("--write-sums", action="store_true", help="regenerate SHA256SUMS for public files and exit") ap.add_argument("--prune", action="store_true", help="also delete remote files that are not part of the public set") ap.add_argument("--confirm", action="store_true", help="without this flag only prints a dry-run, nothing is uploaded") args = ap.parse_args() here = Path(__file__).resolve().parent files = release_files(here) if args.write_sums: write_sums(here, files) return print(f"Release folder: {here}") print(f"Repository: {args.repo_id} ({args.repo_type})") print(f"Public files ({len(files)}):") for name in files: size_mb = (here / name).stat().st_size / (1024 * 1024) print(f" {name:<45} {size_mb:8.2f} MB") internal = sorted( p.relative_to(here).as_posix() for p in here.rglob("*") if p.is_file() and is_internal(p) ) if internal: print(f"Internal files (not uploaded, must not appear in SHA256SUMS): {internal}") if not (here / "SHA256SUMS").exists(): print("WARN: SHA256SUMS is missing; run --write-sums.") from huggingface_hub import CommitOperationAdd, CommitOperationDelete, HfApi api = HfApi() remote = {} if args.prune or args.confirm: try: info = api.repo_info(args.repo_id, repo_type=args.repo_type, files_metadata=True) remote = {s.rfilename: (s.lfs.sha256 if s.lfs else None) for s in info.siblings} except Exception: remote = {} operations, skipped = [], 0 for name in files: local = hashlib.sha256((here / name).read_bytes()).hexdigest() if remote.get(name) == local: skipped += 1 continue operations.append(CommitOperationAdd(path_in_repo=name, path_or_fileobj=str(here / name))) stale = [] if args.prune: keep = set(files) | {".gitattributes"} stale = sorted(name for name in remote if name not in keep) operations.extend(CommitOperationDelete(path_in_repo=name) for name in stale) if not args.confirm: if stale: print(f"Would prune (not in public set): {stale}") print("\nDRY-RUN: pass --confirm to upload to Hugging Face.") return if not operations: print("\nEverything is already up to date; nothing to upload.") return api.create_repo(args.repo_id, repo_type=args.repo_type, exist_ok=True) api.create_commit( repo_id=args.repo_id, repo_type=args.repo_type, operations=operations, commit_message=args.message or "Release update", ) prefix = "datasets/" if args.repo_type == "dataset" else "" summary = (f"\nUploaded {len(operations) - len(stale)} file(s), " f"skipped {skipped} unchanged") if stale: summary += f", pruned {len(stale)} stale: {stale}" print(f"{summary}: https://huggingface.co/{prefix}{args.repo_id}") if __name__ == "__main__": main()