Model_TrOCR-Sin-Printed-Text / upload_weights_to_hub.py
danush99's picture
Upload upload_weights_to_hub.py with huggingface_hub
14836a6 verified
#!/usr/bin/env python3
"""
Upload model files to Hugging Face Hub (danush99/Model_TrOCR-Sin-Normal-Text).
No git required: this uses the Hugging Face API (HTTP), not git push.
From this directory:
pip install huggingface_hub
export HF_TOKEN=your_token # get token from https://huggingface.co/settings/tokens
python upload_weights_to_hub.py
Or: python upload_weights_to_hub.py --token YOUR_TOKEN
"""
import os
import argparse
from pathlib import Path
# All files to push to the model repo (missing files are skipped)
FILES_TO_UPLOAD = (
".gitattributes",
"config.json",
"generation_config.json",
"model.safetensors",
"preprocessor_config.json",
"README.md",
"training_args.bin",
"upload_weights_to_hub.py",
)
def main():
p = argparse.ArgumentParser(description="Upload model files to danush99/Model_TrOCR-Sin-Normal-Text")
p.add_argument("--token", default=os.environ.get("HF_TOKEN"), help="Hugging Face token")
p.add_argument("--repo", default="danush99/Model_TrOCR-Sin-Normal-Text", help="Repo id")
args = p.parse_args()
if not args.token:
print("Set HF_TOKEN or pass --token=YOUR_TOKEN")
raise SystemExit(1)
try:
from huggingface_hub import HfApi
except ImportError:
print("Run: pip install huggingface_hub")
raise SystemExit(1)
api = HfApi(token=args.token)
repo_id = args.repo
folder = Path(__file__).resolve().parent
for name in FILES_TO_UPLOAD:
path = folder / name
if not path.is_file():
print("Skip (not found):", name)
continue
print("Uploading", name, "...")
api.upload_file(path_or_fileobj=str(path), path_in_repo=name, repo_id=repo_id, repo_type="model")
print("Done:", name)
print("All files uploaded to https://huggingface.co/" + repo_id)
if __name__ == "__main__":
main()