JackyChunKit commited on
Commit
9919ebb
·
verified ·
1 Parent(s): b9d91fd

Upload upload_huggingface.py

Browse files
Files changed (1) hide show
  1. upload_huggingface.py +141 -0
upload_huggingface.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ from transformers import AutoModel, AutoTokenizer, AutoConfig
4
+ from huggingface_hub import HfApi, login
5
+ import sys
6
+
7
+ def parse_arguments():
8
+ parser = argparse.ArgumentParser(description='Upload model to Hugging Face Hub')
9
+
10
+ parser.add_argument(
11
+ '--model_path',
12
+ type=str,
13
+ required=True,
14
+ help='Local path to the model'
15
+ )
16
+
17
+ parser.add_argument(
18
+ '--repo_name',
19
+ type=str,
20
+ required=True,
21
+ help='Destination repo name (format: username/repo_name)'
22
+ )
23
+
24
+ parser.add_argument(
25
+ '--token',
26
+ type=str,
27
+ required=True,
28
+ help='Hugging Face API token'
29
+ )
30
+
31
+ parser.add_argument(
32
+ '--model_card',
33
+ type=str,
34
+ default=None,
35
+ help='Path to model card README.md file (optional)'
36
+ )
37
+
38
+ parser.add_argument(
39
+ '--private',
40
+ action='store_true',
41
+ help='Make the repository private'
42
+ )
43
+
44
+ return parser.parse_args()
45
+
46
+ def create_default_model_card(repo_name):
47
+ return f"""---
48
+ language: en
49
+ tags:
50
+ - pytorch
51
+ - transformer
52
+ license: apache-2.0
53
+ ---
54
+
55
+ # Model Card for {repo_name}
56
+
57
+ ## Model Description
58
+
59
+ [Provide a brief description of your model here]
60
+
61
+ ## Intended Use
62
+
63
+ [Describe the intended use of your model]
64
+
65
+ ## Training Procedure
66
+
67
+ [Describe how the model was trained]
68
+
69
+ ## Evaluation Results
70
+
71
+ [Provide evaluation metrics and results]
72
+
73
+ ## Limitations
74
+
75
+ [Describe any known limitations]
76
+
77
+ ## Additional Information
78
+
79
+ For more information, please visit the model repository.
80
+ """
81
+
82
+ def upload_model(model_path, repo_name, token, model_card=None, private=False):
83
+ try:
84
+ # Login to Hugging Face
85
+ login(token)
86
+ print("Successfully logged in to Hugging Face")
87
+
88
+ # Load model, tokenizer, and config
89
+ print("Loading model, tokenizer, and config...")
90
+ model = AutoModel.from_pretrained(model_path)
91
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
92
+ config = AutoConfig.from_pretrained(model_path)
93
+
94
+ # Create model card if not provided
95
+ if model_card is None:
96
+ print("No model card provided. Creating default model card...")
97
+ model_card_content = create_default_model_card(repo_name)
98
+ with open("README.md", "w", encoding="utf-8") as f:
99
+ f.write(model_card_content)
100
+ else:
101
+ # Copy provided model card
102
+ with open(model_card, "r", encoding="utf-8") as f:
103
+ model_card_content = f.read()
104
+ with open("README.md", "w", encoding="utf-8") as f:
105
+ f.write(model_card_content)
106
+
107
+ # Push everything to hub
108
+ print(f"Uploading model to {repo_name}...")
109
+ model.push_to_hub(repo_name, private=private)
110
+ tokenizer.push_to_hub(repo_name, private=private)
111
+ config.push_to_hub(repo_name, private=private)
112
+
113
+ # Clean up
114
+ if os.path.exists("README.md"):
115
+ os.remove("README.md")
116
+
117
+ print(f"Successfully uploaded model to https://huggingface.co/{repo_name}")
118
+ return True
119
+
120
+ except Exception as e:
121
+ print(f"Error occurred: {str(e)}")
122
+ return False
123
+
124
+ def main():
125
+ args = parse_arguments()
126
+
127
+ success = upload_model(
128
+ model_path=args.model_path,
129
+ repo_name=args.repo_name,
130
+ token=args.token,
131
+ model_card=args.model_card,
132
+ private=args.private
133
+ )
134
+
135
+ if success:
136
+ sys.exit(0)
137
+ else:
138
+ sys.exit(1)
139
+
140
+ if __name__ == "__main__":
141
+ main()