Upload upload_to_hf.py with huggingface_hub
Browse files- upload_to_hf.py +38 -0
upload_to_hf.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import HfApi, HfFolder, Repository
|
| 4 |
+
|
| 5 |
+
def upload_to_hf(repo_id, path, token=None):
|
| 6 |
+
api = HfApi()
|
| 7 |
+
|
| 8 |
+
if token is None:
|
| 9 |
+
token = os.getenv("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
if token is None:
|
| 12 |
+
raise ValueError("HF token not provided. Set HF_TOKEN environment variable or pass --token")
|
| 13 |
+
|
| 14 |
+
# Create the repo if it doesn't exist
|
| 15 |
+
api.create_repo(repo_id=repo_id, exist_ok=True, token=token)
|
| 16 |
+
|
| 17 |
+
# Upload all files from path
|
| 18 |
+
for root, _, files in os.walk(path):
|
| 19 |
+
for file in files:
|
| 20 |
+
file_path = os.path.join(root, file)
|
| 21 |
+
rel_path = os.path.relpath(file_path, path)
|
| 22 |
+
print(f"Uploading {rel_path}...")
|
| 23 |
+
api.upload_file(
|
| 24 |
+
path_or_fileobj=file_path,
|
| 25 |
+
path_in_repo=rel_path,
|
| 26 |
+
repo_id=repo_id,
|
| 27 |
+
repo_type="model",
|
| 28 |
+
token=token
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
parser = argparse.ArgumentParser()
|
| 33 |
+
parser.add_argument("--repo_id", type=str, required=True, help="Hugging Face repo id (username/repo_name)")
|
| 34 |
+
parser.add_argument("--path", type=str, required=True, help="Path to upload")
|
| 35 |
+
parser.add_argument("--token", type=str, default=None, help="Hugging Face token")
|
| 36 |
+
args = parser.parse_args()
|
| 37 |
+
|
| 38 |
+
upload_to_hf(args.repo_id, args.path, args.token)
|