Infinite-World / scripts /upload_to_hf.py
wuruiqi0722's picture
Upload folder using huggingface_hub
4794722 verified
#!/usr/bin/env python3
"""
Upload Infinite-World repo to Hugging Face Hub (including checkpoints).
Prerequisites:
1. pip install huggingface_hub
2. huggingface-cli login # or: from huggingface_hub import login; login()
Usage:
cd infinite-world
python scripts/upload_to_hf.py [REPO_ID]
Examples:
python scripts/upload_to_hf.py
python scripts/upload_to_hf.py your-username/infinite-world
"""
import os
import sys
# Project root = parent of scripts/
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def main():
try:
from huggingface_hub import HfApi, create_repo, whoami
except ImportError:
print("Install: pip install huggingface_hub")
sys.exit(1)
repo_id = (
(sys.argv[1] if len(sys.argv) > 1 else None)
or os.environ.get("HF_REPO_ID")
or "MeiGen-AI/Infinite-World"
)
# Check login first (avoid 401 later)
try:
info = whoami()
print(f"[HF] Logged in as: {info.get('name', info.get('type', '?'))}")
except Exception as e:
print("[HF] Not logged in or token invalid (401).")
print(" Run: huggingface-cli login")
print(" Get a token with WRITE at: https://huggingface.co/settings/tokens")
print(" For org repo MeiGen-AI/Infinite-World, your account must have write access to the MeiGen-AI org.")
sys.exit(1)
api = HfApi()
repo_type = "model"
# Create repo if it doesn't exist (skip if 401; repo may already exist)
try:
create_repo(repo_id, repo_type=repo_type, exist_ok=True)
print(f"[HF] Repo ready: https://huggingface.co/{repo_id}")
except Exception as e:
err = str(e).lower()
if "401" in err or "unauthorized" in err:
print("[HF] No write permission for this repo. Fix: use a token with write access; for MeiGen-AI/Infinite-World, be a member of MeiGen-AI org or use the org token.")
sys.exit(1)
print(f"[HF] Create repo: {e}")
# Continue; repo might already exist
# Exclude cache/outputs; do NOT use .gitignore so checkpoints/ is included
ignore_patterns = [
"__pycache__",
"*.pyc",
".git",
"outputs",
".cursor",
"*.egg-info",
".eggs",
]
# Upload: use use_gitignore=False so checkpoints/ (in .gitignore) is included
upload_kw = dict(
folder_path=PROJECT_ROOT,
repo_id=repo_id,
repo_type=repo_type,
ignore_patterns=ignore_patterns,
)
print(f"[HF] Uploading from {PROJECT_ROOT} to {repo_id} (including checkpoints/) ...")
try:
api.upload_folder(**upload_kw, use_gitignore=False)
except TypeError:
# Older huggingface_hub: no use_gitignore → upload checkpoints separately
api.upload_folder(**upload_kw)
checkpoints_dir = os.path.join(PROJECT_ROOT, "checkpoints")
if os.path.isdir(checkpoints_dir):
print(f"[HF] Uploading checkpoints/ to {repo_id} ...")
api.upload_folder(
folder_path=checkpoints_dir,
repo_id=repo_id,
path_in_repo="checkpoints",
repo_type=repo_type,
)
print(f"[HF] Done: https://huggingface.co/{repo_id}")
if __name__ == "__main__":
main()