DataSynthis_ML_JobTask / upload_to_hf.py
Hiruni2207's picture
Upload upload_to_hf.py with huggingface_hub
562d2e4 verified
import argparse
import os
from huggingface_hub import HfApi, HfFolder, Repository
def upload_to_hf(repo_id, path, token=None):
api = HfApi()
if token is None:
token = os.getenv("HF_TOKEN")
if token is None:
raise ValueError("HF token not provided. Set HF_TOKEN environment variable or pass --token")
# Create the repo if it doesn't exist
api.create_repo(repo_id=repo_id, exist_ok=True, token=token)
# Upload all files from path
for root, _, files in os.walk(path):
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, path)
print(f"Uploading {rel_path}...")
api.upload_file(
path_or_fileobj=file_path,
path_in_repo=rel_path,
repo_id=repo_id,
repo_type="model",
token=token
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--repo_id", type=str, required=True, help="Hugging Face repo id (username/repo_name)")
parser.add_argument("--path", type=str, required=True, help="Path to upload")
parser.add_argument("--token", type=str, default=None, help="Hugging Face token")
args = parser.parse_args()
upload_to_hf(args.repo_id, args.path, args.token)