|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Utility to push a local folder (checkpoints, adapters, or artifacts) to the Hugging Face Hub. |
| 4 | +
|
| 5 | +Examples: |
| 6 | + python scripts/hf/push_checkpoints.py \ |
| 7 | + --repo-id Junaidi-AI/med-vllm \ |
| 8 | + --local-path outputs/ner-checkpoint \ |
| 9 | + --path-in-repo checkpoints/ner-2025-09-23 \ |
| 10 | + --commit-message "Add NER checkpoint" |
| 11 | +
|
| 12 | +If you need to create a PR instead of pushing to main directly, add --create-pr. |
| 13 | +To rely on credentials saved via `hf auth login`, do not set HF_TOKEN or unset it for this run. |
| 14 | +
|
| 15 | + env -u HF_TOKEN python scripts/hf/push_checkpoints.py ... |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import argparse |
| 21 | +import os |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +from huggingface_hub import HfApi |
| 26 | + |
| 27 | + |
| 28 | +def parse_args() -> argparse.Namespace: |
| 29 | + p = argparse.ArgumentParser(description="Push a local folder to the Hugging Face Hub") |
| 30 | + p.add_argument("--repo-id", required=True, help="Target repo id, e.g. org/name or user/name") |
| 31 | + p.add_argument( |
| 32 | + "--local-path", |
| 33 | + required=True, |
| 34 | + help="Local file or folder to upload (can be a single file or a directory)", |
| 35 | + ) |
| 36 | + p.add_argument( |
| 37 | + "--path-in-repo", |
| 38 | + default=None, |
| 39 | + help="Destination path in repo (defaults to same name as local-path)", |
| 40 | + ) |
| 41 | + p.add_argument( |
| 42 | + "--repo-type", |
| 43 | + default="model", |
| 44 | + choices=["model", "dataset", "space"], |
| 45 | + help="Repo type on the Hub", |
| 46 | + ) |
| 47 | + p.add_argument( |
| 48 | + "--commit-message", |
| 49 | + default="Upload artifacts", |
| 50 | + help="Commit title / first line", |
| 51 | + ) |
| 52 | + p.add_argument( |
| 53 | + "--commit-description", |
| 54 | + default=None, |
| 55 | + help="Optional commit description (body)", |
| 56 | + ) |
| 57 | + p.add_argument( |
| 58 | + "--create-pr", |
| 59 | + action="store_true", |
| 60 | + help="Upload changes as a Pull Request instead of pushing to the default branch", |
| 61 | + ) |
| 62 | + p.add_argument( |
| 63 | + "--revision", |
| 64 | + default=None, |
| 65 | + help="Optional branch name or refs/pr/X reference to push against", |
| 66 | + ) |
| 67 | + return p.parse_args() |
| 68 | + |
| 69 | + |
| 70 | +def main() -> int: |
| 71 | + args = parse_args() |
| 72 | + |
| 73 | + local_path = Path(args.local_path) |
| 74 | + if not local_path.exists(): |
| 75 | + print(f"[ERROR] Local path does not exist: {local_path}") |
| 76 | + return 2 |
| 77 | + |
| 78 | + # Respect existing saved credential; avoid env HF_TOKEN overriding by allowing caller to unset it. |
| 79 | + api = HfApi() |
| 80 | + |
| 81 | + # Use HfApi.upload_folder for directories, or upload_file for single files |
| 82 | + try: |
| 83 | + if local_path.is_dir(): |
| 84 | + commit = api.upload_folder( |
| 85 | + repo_id=args.repo_id, |
| 86 | + repo_type=args.repo_type, |
| 87 | + folder_path=str(local_path), |
| 88 | + path_in_repo=(args.path_in_repo or local_path.name), |
| 89 | + commit_message=args.commit_message, |
| 90 | + commit_description=args.commit_description, |
| 91 | + create_pr=args.create_pr, |
| 92 | + revision=args.revision, |
| 93 | + ) |
| 94 | + else: |
| 95 | + # Single file |
| 96 | + commit = api.upload_file( |
| 97 | + repo_id=args.repo_id, |
| 98 | + repo_type=args.repo_type, |
| 99 | + path_or_fileobj=str(local_path), |
| 100 | + path_in_repo=(args.path_in_repo or local_path.name), |
| 101 | + commit_message=args.commit_message, |
| 102 | + commit_description=args.commit_description, |
| 103 | + create_pr=args.create_pr, |
| 104 | + revision=args.revision, |
| 105 | + ) |
| 106 | + except Exception as e: |
| 107 | + print(f"[ERROR] Upload failed: {e}") |
| 108 | + return 3 |
| 109 | + |
| 110 | + print(commit) |
| 111 | + return 0 |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + raise SystemExit(main()) |
0 commit comments