Linaqruf commited on
Commit
c438f1b
Β·
verified Β·
1 Parent(s): 78c2c4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +320 -321
app.py CHANGED
@@ -1,321 +1,320 @@
1
- """
2
- HuggingFace to ModelScope Migration Tool
3
-
4
- This Gradio app enables migration of models and datasets from HuggingFace to ModelScope.
5
- """
6
-
7
- import os
8
- import shutil
9
- import tempfile
10
- from pathlib import Path
11
- from typing import Tuple, Optional
12
-
13
- import gradio as gr
14
- from huggingface_hub import snapshot_download, HfApi
15
- from modelscope.hub.api import HubApi
16
- from modelscope.hub.constants import Licenses, ModelVisibility
17
-
18
-
19
- class MigrationTool:
20
- """Handles migration of models and datasets between HuggingFace and ModelScope."""
21
-
22
- def __init__(self):
23
- self.temp_dir = None
24
-
25
- def download_from_hf(
26
- self,
27
- repo_id: str,
28
- repo_type: str = "model",
29
- token: Optional[str] = None
30
- ) -> Tuple[bool, str, Optional[str]]:
31
- """Download a repository from HuggingFace.
32
-
33
- Args:
34
- repo_id: HuggingFace repository ID (e.g., 'username/repo-name')
35
- repo_type: Type of repository ('model' or 'dataset')
36
- token: HuggingFace authentication token
37
-
38
- Returns:
39
- Tuple of (success, message, local_path)
40
- """
41
- try:
42
- # Create temporary directory
43
- self.temp_dir = tempfile.mkdtemp(prefix="hf2ms_")
44
-
45
- # Download the repository
46
- local_path = snapshot_download(
47
- repo_id=repo_id,
48
- repo_type=repo_type,
49
- local_dir=self.temp_dir,
50
- local_dir_use_symlinks=False,
51
- token=token
52
- )
53
-
54
- return True, f"βœ“ Successfully downloaded {repo_type} from HuggingFace", local_path
55
- except Exception as e:
56
- return False, f"βœ— Download failed: {str(e)}", None
57
-
58
- def upload_to_ms(
59
- self,
60
- local_path: str,
61
- repo_id: str,
62
- token: str,
63
- repo_type: str = "model",
64
- visibility: str = "public",
65
- license_type: str = "apache-2.0",
66
- chinese_name: Optional[str] = None
67
- ) -> Tuple[bool, str]:
68
- """Upload a repository to ModelScope.
69
-
70
- Args:
71
- local_path: Local path to the repository
72
- repo_id: ModelScope repository ID (e.g., 'username/repo-name')
73
- token: ModelScope authentication token
74
- repo_type: Type of repository ('model' or 'dataset')
75
- visibility: Repository visibility ('public' or 'private')
76
- license_type: License type
77
- chinese_name: Optional Chinese name for the repository
78
-
79
- Returns:
80
- Tuple of (success, message)
81
- """
82
- try:
83
- # Create HubApi instance and login
84
- api = HubApi()
85
- api.login(token)
86
-
87
- # Determine visibility
88
- vis = ModelVisibility.PUBLIC if visibility == "public" else ModelVisibility.PRIVATE
89
-
90
- # Map license types
91
- license_map = {
92
- "apache-2.0": Licenses.APACHE_V2,
93
- "mit": Licenses.MIT,
94
- "gpl-3.0": Licenses.GPL_V3,
95
- "other": Licenses.OTHER,
96
- }
97
- lic = license_map.get(license_type.lower(), Licenses.APACHE_V2)
98
-
99
- # Create repository if it doesn't exist
100
- try:
101
- if repo_type == "model":
102
- api.create_model(
103
- model_id=repo_id,
104
- visibility=vis,
105
- license=lic,
106
- chinese_name=chinese_name
107
- )
108
- except Exception as create_error:
109
- # Repository might already exist, continue to push
110
- pass
111
-
112
- # Push the model/dataset
113
- if repo_type == "model":
114
- api.push_model(
115
- model_id=repo_id,
116
- model_dir=local_path
117
- )
118
- else:
119
- # For datasets, use similar approach
120
- api.push_model(
121
- model_id=repo_id,
122
- model_dir=local_path
123
- )
124
-
125
- return True, f"βœ“ Successfully uploaded {repo_type} to ModelScope"
126
- except Exception as e:
127
- return False, f"βœ— Upload failed: {str(e)}"
128
-
129
- def cleanup(self):
130
- """Clean up temporary files."""
131
- if self.temp_dir and os.path.exists(self.temp_dir):
132
- try:
133
- shutil.rmtree(self.temp_dir)
134
- self.temp_dir = None
135
- except Exception as e:
136
- print(f"Warning: Failed to clean up temporary directory: {e}")
137
-
138
- def migrate(
139
- self,
140
- hf_token: str,
141
- ms_token: str,
142
- hf_repo_id: str,
143
- ms_repo_id: str,
144
- repo_type: str,
145
- visibility: str,
146
- license_type: str,
147
- chinese_name: Optional[str] = None
148
- ) -> str:
149
- """Perform the complete migration process.
150
-
151
- Returns:
152
- Status message with migration progress
153
- """
154
- output = []
155
-
156
- # Validate inputs
157
- if not hf_token or not ms_token:
158
- return "βœ— Error: Both HuggingFace and ModelScope tokens are required"
159
-
160
- if not hf_repo_id or not ms_repo_id:
161
- return "βœ— Error: Both source and destination repository IDs are required"
162
-
163
- # Download from HuggingFace
164
- output.append(f"⬇️ Downloading {repo_type} from HuggingFace: {hf_repo_id}...")
165
- success, msg, local_path = self.download_from_hf(hf_repo_id, repo_type, hf_token)
166
- output.append(msg)
167
- if not success:
168
- self.cleanup()
169
- return "\n".join(output)
170
-
171
- # Upload to ModelScope
172
- output.append(f"\n⬆️ Uploading {repo_type} to ModelScope: {ms_repo_id}...")
173
- success, msg = self.upload_to_ms(
174
- local_path,
175
- ms_repo_id,
176
- ms_token,
177
- repo_type,
178
- visibility,
179
- license_type,
180
- chinese_name
181
- )
182
- output.append(msg)
183
-
184
- # Cleanup
185
- output.append("\n🧹 Cleaning up temporary files...")
186
- self.cleanup()
187
- output.append("βœ“ Cleanup complete")
188
-
189
- if success:
190
- output.append(f"\nβœ… Migration completed successfully!")
191
- output.append(f"Your {repo_type} is now available at: https://www.modelscope.cn/models/{ms_repo_id}")
192
- else:
193
- output.append(f"\n❌ Migration failed")
194
-
195
- return "\n".join(output)
196
-
197
-
198
- def create_interface():
199
- """Create the Gradio interface."""
200
-
201
- migration_tool = MigrationTool()
202
-
203
- with gr.Blocks(title="HuggingFace to ModelScope Migration Tool") as app:
204
- gr.Markdown("""
205
- # πŸš€ HuggingFace to ModelScope Migration Tool
206
-
207
- Easily migrate your models and datasets from HuggingFace to ModelScope.
208
-
209
- ## πŸ“‹ Instructions:
210
- 1. Get your **HuggingFace token** from: https://huggingface.co/settings/tokens
211
- 2. Get your **ModelScope token** from: https://www.modelscope.cn/my/myaccesstoken
212
- 3. Fill in the repository details below
213
- 4. Click "Start Migration"
214
- """)
215
-
216
- with gr.Row():
217
- with gr.Column():
218
- gr.Markdown("### πŸ”‘ Authentication")
219
- hf_token = gr.Textbox(
220
- label="HuggingFace Token",
221
- type="password",
222
- placeholder="hf_...",
223
- info="Your HuggingFace access token"
224
- )
225
- ms_token = gr.Textbox(
226
- label="ModelScope Token",
227
- type="password",
228
- placeholder="Enter your ModelScope token",
229
- info="Your ModelScope SDK token"
230
- )
231
-
232
- with gr.Column():
233
- gr.Markdown("### πŸ“¦ Repository Details")
234
- repo_type = gr.Radio(
235
- choices=["model", "dataset"],
236
- label="Repository Type",
237
- value="model",
238
- info="Select what you want to migrate"
239
- )
240
- visibility = gr.Radio(
241
- choices=["public", "private"],
242
- label="Visibility",
243
- value="public",
244
- info="Visibility of the repository on ModelScope"
245
- )
246
-
247
- with gr.Row():
248
- with gr.Column():
249
- hf_repo_id = gr.Textbox(
250
- label="Source HuggingFace Repo ID",
251
- placeholder="username/repo-name",
252
- info="e.g., bert-base-uncased or username/my-model"
253
- )
254
-
255
- with gr.Column():
256
- ms_repo_id = gr.Textbox(
257
- label="Destination ModelScope Repo ID",
258
- placeholder="username/repo-name",
259
- info="Your ModelScope username/repo-name"
260
- )
261
-
262
- with gr.Row():
263
- with gr.Column():
264
- license_type = gr.Dropdown(
265
- choices=["apache-2.0", "mit", "gpl-3.0", "other"],
266
- label="License",
267
- value="apache-2.0",
268
- info="License for the repository"
269
- )
270
-
271
- with gr.Column():
272
- chinese_name = gr.Textbox(
273
- label="Chinese Name (Optional)",
274
- placeholder="ζ¨‘εž‹δΈ­ζ–‡εη§°",
275
- info="Optional Chinese name for the repository"
276
- )
277
-
278
- migrate_btn = gr.Button("πŸš€ Start Migration", variant="primary", size="lg")
279
-
280
- output = gr.Textbox(
281
- label="Migration Status",
282
- lines=15,
283
- interactive=False
284
- )
285
-
286
- migrate_btn.click(
287
- fn=migration_tool.migrate,
288
- inputs=[
289
- hf_token,
290
- ms_token,
291
- hf_repo_id,
292
- ms_repo_id,
293
- repo_type,
294
- visibility,
295
- license_type,
296
- chinese_name
297
- ],
298
- outputs=output
299
- )
300
-
301
- gr.Markdown("""
302
- ---
303
- ### πŸ“ Notes:
304
- - Large models may take some time to download and upload
305
- - Make sure you have enough disk space for temporary storage
306
- - Private repositories require appropriate token permissions
307
- - The tool will create the repository on ModelScope if it doesn't exist
308
-
309
- ### πŸ”— Resources:
310
- - [HuggingFace Hub](https://huggingface.co/)
311
- - [ModelScope](https://www.modelscope.cn/)
312
- - [HuggingFace Token Settings](https://huggingface.co/settings/tokens)
313
- - [ModelScope Token Settings](https://www.modelscope.cn/my/myaccesstoken)
314
- """)
315
-
316
- return app
317
-
318
-
319
- if __name__ == "__main__":
320
- app = create_interface()
321
- app.launch(share=False)
 
1
+ """
2
+ HuggingFace to ModelScope Migration Tool
3
+
4
+ This Gradio app enables migration of models and datasets from HuggingFace to ModelScope.
5
+ """
6
+
7
+ import os
8
+ import shutil
9
+ import tempfile
10
+ from pathlib import Path
11
+ from typing import Tuple, Optional
12
+
13
+ import gradio as gr
14
+ from huggingface_hub import snapshot_download, HfApi
15
+ from modelscope.hub.api import HubApi
16
+ from modelscope.hub.constants import Licenses, ModelVisibility
17
+
18
+
19
+ class MigrationTool:
20
+ """Handles migration of models and datasets between HuggingFace and ModelScope."""
21
+
22
+ def __init__(self):
23
+ self.temp_dir = None
24
+
25
+ def download_from_hf(
26
+ self,
27
+ repo_id: str,
28
+ repo_type: str = "model",
29
+ token: Optional[str] = None
30
+ ) -> Tuple[bool, str, Optional[str]]:
31
+ """Download a repository from HuggingFace.
32
+
33
+ Args:
34
+ repo_id: HuggingFace repository ID (e.g., 'username/repo-name')
35
+ repo_type: Type of repository ('model' or 'dataset')
36
+ token: HuggingFace authentication token
37
+
38
+ Returns:
39
+ Tuple of (success, message, local_path)
40
+ """
41
+ try:
42
+ # Create temporary directory
43
+ self.temp_dir = tempfile.mkdtemp(prefix="hf2ms_")
44
+
45
+ # Download the repository
46
+ local_path = snapshot_download(
47
+ repo_id=repo_id,
48
+ repo_type=repo_type,
49
+ local_dir=self.temp_dir,
50
+ local_dir_use_symlinks=False,
51
+ token=token
52
+ )
53
+
54
+ return True, f"βœ“ Successfully downloaded {repo_type} from HuggingFace", local_path
55
+ except Exception as e:
56
+ return False, f"βœ— Download failed: {str(e)}", None
57
+
58
+ def upload_to_ms(
59
+ self,
60
+ local_path: str,
61
+ repo_id: str,
62
+ token: str,
63
+ repo_type: str = "model",
64
+ visibility: str = "public",
65
+ license_type: str = "apache-2.0",
66
+ chinese_name: Optional[str] = None
67
+ ) -> Tuple[bool, str]:
68
+ """Upload a repository to ModelScope.
69
+
70
+ Args:
71
+ local_path: Local path to the repository
72
+ repo_id: ModelScope repository ID (e.g., 'username/repo-name')
73
+ token: ModelScope authentication token
74
+ repo_type: Type of repository ('model' or 'dataset')
75
+ visibility: Repository visibility ('public' or 'private')
76
+ license_type: License type
77
+ chinese_name: Optional Chinese name for the repository
78
+
79
+ Returns:
80
+ Tuple of (success, message)
81
+ """
82
+ try:
83
+ # Create HubApi instance with token
84
+ api = HubApi(token=token)
85
+
86
+ # Determine visibility
87
+ vis = ModelVisibility.PUBLIC if visibility == "public" else ModelVisibility.PRIVATE
88
+
89
+ # Map license types
90
+ license_map = {
91
+ "apache-2.0": Licenses.APACHE_V2,
92
+ "mit": Licenses.MIT,
93
+ "gpl-3.0": Licenses.GPL_V3,
94
+ "other": Licenses.OTHER,
95
+ }
96
+ lic = license_map.get(license_type.lower(), Licenses.APACHE_V2)
97
+
98
+ # Create repository if it doesn't exist
99
+ try:
100
+ if repo_type == "model":
101
+ api.create_model(
102
+ model_id=repo_id,
103
+ visibility=vis,
104
+ license=lic,
105
+ chinese_name=chinese_name
106
+ )
107
+ except Exception as create_error:
108
+ # Repository might already exist, continue to push
109
+ pass
110
+
111
+ # Push the model/dataset
112
+ if repo_type == "model":
113
+ api.push_model(
114
+ model_id=repo_id,
115
+ model_dir=local_path
116
+ )
117
+ else:
118
+ # For datasets, use similar approach
119
+ api.push_model(
120
+ model_id=repo_id,
121
+ model_dir=local_path
122
+ )
123
+
124
+ return True, f"βœ“ Successfully uploaded {repo_type} to ModelScope"
125
+ except Exception as e:
126
+ return False, f"βœ— Upload failed: {str(e)}"
127
+
128
+ def cleanup(self):
129
+ """Clean up temporary files."""
130
+ if self.temp_dir and os.path.exists(self.temp_dir):
131
+ try:
132
+ shutil.rmtree(self.temp_dir)
133
+ self.temp_dir = None
134
+ except Exception as e:
135
+ print(f"Warning: Failed to clean up temporary directory: {e}")
136
+
137
+ def migrate(
138
+ self,
139
+ hf_token: str,
140
+ ms_token: str,
141
+ hf_repo_id: str,
142
+ ms_repo_id: str,
143
+ repo_type: str,
144
+ visibility: str,
145
+ license_type: str,
146
+ chinese_name: Optional[str] = None
147
+ ) -> str:
148
+ """Perform the complete migration process.
149
+
150
+ Returns:
151
+ Status message with migration progress
152
+ """
153
+ output = []
154
+
155
+ # Validate inputs
156
+ if not hf_token or not ms_token:
157
+ return "βœ— Error: Both HuggingFace and ModelScope tokens are required"
158
+
159
+ if not hf_repo_id or not ms_repo_id:
160
+ return "βœ— Error: Both source and destination repository IDs are required"
161
+
162
+ # Download from HuggingFace
163
+ output.append(f"⬇️ Downloading {repo_type} from HuggingFace: {hf_repo_id}...")
164
+ success, msg, local_path = self.download_from_hf(hf_repo_id, repo_type, hf_token)
165
+ output.append(msg)
166
+ if not success:
167
+ self.cleanup()
168
+ return "\n".join(output)
169
+
170
+ # Upload to ModelScope
171
+ output.append(f"\n⬆️ Uploading {repo_type} to ModelScope: {ms_repo_id}...")
172
+ success, msg = self.upload_to_ms(
173
+ local_path,
174
+ ms_repo_id,
175
+ ms_token,
176
+ repo_type,
177
+ visibility,
178
+ license_type,
179
+ chinese_name
180
+ )
181
+ output.append(msg)
182
+
183
+ # Cleanup
184
+ output.append("\n🧹 Cleaning up temporary files...")
185
+ self.cleanup()
186
+ output.append("βœ“ Cleanup complete")
187
+
188
+ if success:
189
+ output.append(f"\nβœ… Migration completed successfully!")
190
+ output.append(f"Your {repo_type} is now available at: https://www.modelscope.cn/models/{ms_repo_id}")
191
+ else:
192
+ output.append(f"\n❌ Migration failed")
193
+
194
+ return "\n".join(output)
195
+
196
+
197
+ def create_interface():
198
+ """Create the Gradio interface."""
199
+
200
+ migration_tool = MigrationTool()
201
+
202
+ with gr.Blocks(title="HuggingFace to ModelScope Migration Tool") as app:
203
+ gr.Markdown("""
204
+ # πŸš€ HuggingFace to ModelScope Migration Tool
205
+
206
+ Easily migrate your models and datasets from HuggingFace to ModelScope.
207
+
208
+ ## πŸ“‹ Instructions:
209
+ 1. Get your **HuggingFace token** from: https://huggingface.co/settings/tokens
210
+ 2. Get your **ModelScope token** from: https://www.modelscope.cn/my/myaccesstoken
211
+ 3. Fill in the repository details below
212
+ 4. Click "Start Migration"
213
+ """)
214
+
215
+ with gr.Row():
216
+ with gr.Column():
217
+ gr.Markdown("### πŸ”‘ Authentication")
218
+ hf_token = gr.Textbox(
219
+ label="HuggingFace Token",
220
+ type="password",
221
+ placeholder="hf_...",
222
+ info="Your HuggingFace access token"
223
+ )
224
+ ms_token = gr.Textbox(
225
+ label="ModelScope Token",
226
+ type="password",
227
+ placeholder="Enter your ModelScope token",
228
+ info="Your ModelScope SDK token"
229
+ )
230
+
231
+ with gr.Column():
232
+ gr.Markdown("### πŸ“¦ Repository Details")
233
+ repo_type = gr.Radio(
234
+ choices=["model", "dataset"],
235
+ label="Repository Type",
236
+ value="model",
237
+ info="Select what you want to migrate"
238
+ )
239
+ visibility = gr.Radio(
240
+ choices=["public", "private"],
241
+ label="Visibility",
242
+ value="public",
243
+ info="Visibility of the repository on ModelScope"
244
+ )
245
+
246
+ with gr.Row():
247
+ with gr.Column():
248
+ hf_repo_id = gr.Textbox(
249
+ label="Source HuggingFace Repo ID",
250
+ placeholder="username/repo-name",
251
+ info="e.g., bert-base-uncased or username/my-model"
252
+ )
253
+
254
+ with gr.Column():
255
+ ms_repo_id = gr.Textbox(
256
+ label="Destination ModelScope Repo ID",
257
+ placeholder="username/repo-name",
258
+ info="Your ModelScope username/repo-name"
259
+ )
260
+
261
+ with gr.Row():
262
+ with gr.Column():
263
+ license_type = gr.Dropdown(
264
+ choices=["apache-2.0", "mit", "gpl-3.0", "other"],
265
+ label="License",
266
+ value="apache-2.0",
267
+ info="License for the repository"
268
+ )
269
+
270
+ with gr.Column():
271
+ chinese_name = gr.Textbox(
272
+ label="Chinese Name (Optional)",
273
+ placeholder="ζ¨‘εž‹δΈ­ζ–‡εη§°",
274
+ info="Optional Chinese name for the repository"
275
+ )
276
+
277
+ migrate_btn = gr.Button("πŸš€ Start Migration", variant="primary", size="lg")
278
+
279
+ output = gr.Textbox(
280
+ label="Migration Status",
281
+ lines=15,
282
+ interactive=False
283
+ )
284
+
285
+ migrate_btn.click(
286
+ fn=migration_tool.migrate,
287
+ inputs=[
288
+ hf_token,
289
+ ms_token,
290
+ hf_repo_id,
291
+ ms_repo_id,
292
+ repo_type,
293
+ visibility,
294
+ license_type,
295
+ chinese_name
296
+ ],
297
+ outputs=output
298
+ )
299
+
300
+ gr.Markdown("""
301
+ ---
302
+ ### πŸ“ Notes:
303
+ - Large models may take some time to download and upload
304
+ - Make sure you have enough disk space for temporary storage
305
+ - Private repositories require appropriate token permissions
306
+ - The tool will create the repository on ModelScope if it doesn't exist
307
+
308
+ ### πŸ”— Resources:
309
+ - [HuggingFace Hub](https://huggingface.co/)
310
+ - [ModelScope](https://www.modelscope.cn/)
311
+ - [HuggingFace Token Settings](https://huggingface.co/settings/tokens)
312
+ - [ModelScope Token Settings](https://www.modelscope.cn/my/myaccesstoken)
313
+ """)
314
+
315
+ return app
316
+
317
+
318
+ if __name__ == "__main__":
319
+ app = create_interface()
320
+ app.launch(share=False)