-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload_models.py
More file actions
69 lines (59 loc) · 1.89 KB
/
Copy pathupload_models.py
File metadata and controls
69 lines (59 loc) · 1.89 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import argparse
from pathlib import Path
from huggingface_hub import HfApi, create_repo
def upload_model(model_path, repo_id):
"""Upload a single model to Hugging Face Hub."""
# Initialize the Hugging Face API
api = HfApi()
# Ensure model path exists
model_dir = Path(model_path)
if not model_dir.exists() or not model_dir.is_dir():
print(
f"Error: Model directory {model_path} does not exist or is not a directory"
)
return False
# Create the repository if it doesn't exist
print(f"Creating repository {repo_id}...")
try:
create_repo(
repo_id=repo_id,
repo_type="model",
exist_ok=True,
private=False,
)
print(f"Successfully created repository {repo_id}")
except Exception as e:
print(f"Error creating repository: {e}")
return False
# Upload the model
model_name = model_dir.name
print(f"Uploading {model_name} to {repo_id}...")
try:
# Upload the model
api.upload_folder(
folder_path=str(model_dir),
repo_id=repo_id,
repo_type="model",
commit_message=f"Upload {model_name} model",
)
print(f"Successfully uploaded {model_name} to {repo_id}")
return True
except Exception as e:
print(f"Error uploading {model_name}: {e}")
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Upload a model to Hugging Face Hub")
parser.add_argument(
"--model_path",
type=str,
required=True,
help="Path to the model directory to upload",
)
parser.add_argument(
"--repo_id",
type=str,
required=True,
help="Hugging Face repository ID (e.g., 'username/model-name')",
)
args = parser.parse_args()
upload_model(args.model_path, args.repo_id)