File size: 1,384 Bytes
562d2e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)