Linaqruf commited on
Commit
f9583f1
·
verified ·
1 Parent(s): d0141ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -32
app.py CHANGED
@@ -14,7 +14,7 @@ import argparse
14
  import gradio as gr
15
  from huggingface_hub import snapshot_download, HfApi
16
  from modelscope.hub.api import HubApi
17
- from modelscope.hub.constants import Licenses, ModelVisibility
18
  import sys
19
  import io
20
  import threading
@@ -102,9 +102,6 @@ class MigrationTool:
102
  except Exception as login_error:
103
  return False, f"✗ ModelScope Login failed: {str(login_error)}\n\n💡 Tip: Ensure you are using an 'SDK Token' from https://www.modelscope.cn (NOT modelscope.ai). The token usually starts with 'ms-'."
104
 
105
- # Determine visibility
106
- vis = ModelVisibility.PUBLIC if visibility == "public" else ModelVisibility.PRIVATE
107
-
108
  # Map license types
109
  license_map = {
110
  "apache-2.0": Licenses.APACHE_V2,
@@ -119,36 +116,52 @@ class MigrationTool:
119
  }
120
  lic = license_map.get(license_type.lower(), Licenses.APACHE_V2)
121
 
 
 
 
122
  # Create repository if it doesn't exist
123
- try:
124
- if repo_type == "model":
125
- api.create_model(
126
- model_id=repo_id,
127
- visibility=vis,
128
- license=lic,
129
- chinese_name=chinese_name,
130
- token=token,
131
- )
132
- else:
133
- api.create_dataset(
134
- dataset_id=repo_id,
135
- visibility=vis,
136
- license=lic,
137
- chinese_name=chinese_name,
138
- token=token,
139
- )
140
- except Exception:
141
- # Repository might already exist, continue to push
142
- # We update the visibility explicitly to ensure it matches user choice
143
  try:
144
- api.set_repo_visibility(
145
- repo_id=repo_id,
146
- repo_type=repo_type,
147
- visibility=visibility, # "public" or "private"
148
- token=token
149
- )
150
- except Exception as vis_error:
151
- print(f"Warning: Failed to update visibility: {vis_error}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  # Push the model/dataset
154
  if repo_type == "model":
 
14
  import gradio as gr
15
  from huggingface_hub import snapshot_download, HfApi
16
  from modelscope.hub.api import HubApi
17
+ from modelscope.hub.constants import Licenses, ModelVisibility, DatasetVisibility
18
  import sys
19
  import io
20
  import threading
 
102
  except Exception as login_error:
103
  return False, f"✗ ModelScope Login failed: {str(login_error)}\n\n💡 Tip: Ensure you are using an 'SDK Token' from https://www.modelscope.cn (NOT modelscope.ai). The token usually starts with 'ms-'."
104
 
 
 
 
105
  # Map license types
106
  license_map = {
107
  "apache-2.0": Licenses.APACHE_V2,
 
116
  }
117
  lic = license_map.get(license_type.lower(), Licenses.APACHE_V2)
118
 
119
+ # Check if repository exists
120
+ repo_exists = api.repo_exists(repo_id=repo_id, repo_type=repo_type, token=token)
121
+
122
  # Create repository if it doesn't exist
123
+ # Important: We must create with the correct visibility BEFORE upload_folder,
124
+ # because upload_folder will create a public repo by default if repo doesn't exist
125
+ if not repo_exists:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  try:
127
+ if repo_type == "model":
128
+ # Determine visibility for models (1=private, 5=public)
129
+ vis = ModelVisibility.PUBLIC if visibility == "public" else ModelVisibility.PRIVATE
130
+ # Build parameters, only include license if not None
131
+ create_params = {
132
+ "model_id": repo_id,
133
+ "visibility": vis,
134
+ "token": token,
135
+ }
136
+ if lic is not None:
137
+ create_params["license"] = lic
138
+ if chinese_name:
139
+ create_params["chinese_name"] = chinese_name
140
+ api.create_model(**create_params)
141
+ else:
142
+ # Determine visibility for datasets (1=private, 5=public)
143
+ vis = DatasetVisibility.PUBLIC if visibility == "public" else DatasetVisibility.PRIVATE
144
+ # For datasets, need to split repo_id into namespace and name
145
+ parts = repo_id.split('/')
146
+ if len(parts) != 2:
147
+ return False, f"✗ Invalid dataset ID format: {repo_id}. Must be 'namespace/name'"
148
+ namespace, dataset_name = parts
149
+ # Build parameters, only include license if not None
150
+ create_params = {
151
+ "dataset_name": dataset_name,
152
+ "namespace": namespace,
153
+ "visibility": vis,
154
+ }
155
+ if lic is not None:
156
+ create_params["license"] = lic
157
+ if chinese_name:
158
+ create_params["chinese_name"] = chinese_name
159
+ api.create_dataset(**create_params)
160
+ except Exception as create_error:
161
+ error_msg = str(create_error)
162
+ # Only ignore if repo already exists (race condition)
163
+ if "already exists" not in error_msg.lower():
164
+ return False, f"✗ Failed to create repository: {error_msg}"
165
 
166
  # Push the model/dataset
167
  if repo_type == "model":