John6666 commited on
Commit
06410a6
·
verified ·
1 Parent(s): a51f990

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +43 -24
app.py CHANGED
@@ -6,7 +6,6 @@ import os
6
  import tempfile
7
  import shutil
8
  import urllib
9
- import hashlib
10
  from huggingface_hub import whoami, HfApi, hf_hub_download, RepoCard
11
  from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
12
  from gradio_huggingfacehub_search import HuggingfaceHubSearch
@@ -20,6 +19,35 @@ HF_REPO_PREFIX = os.environ.get("HF_REPO_PREFIX") if os.environ.get("HF_REPO_PRE
20
  HF_REPO_SUFFIX = os.environ.get("HF_REPO_SUFFIX") if os.environ.get("HF_REPO_SUFFIX") else "" # set your default repo suffix
21
  HF_USER = os.environ.get("HF_USER") if os.environ.get("HF_USER") else "" # set your username
22
  REGEX_HF_REPO = r'^[\w_\-\.]+/[\w_\-\.]+$'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def remove_repo_tags(repo_id: str, tags: list[str], repo_type: str, hf_token: str):
25
  try:
@@ -43,16 +71,11 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, re
43
  except Exception as e:
44
  raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
45
 
46
- try:
47
- if re.fullmatch(REGEX_HF_REPO, source_repo): target = ""
48
- else:
49
- source_repo, target = re.findall(r'^(?:http.+\.co/)?(?:datasets)?(?:spaces)?([\w_\-\.]+/[\w_\-\.]+)/?(?:blob/main/)?(?:resolve/main/)?(.+)?$', source_repo)[0]
50
- target = urllib.parse.unquote(target.removesuffix("/"))
51
 
52
- if re.fullmatch(REGEX_HF_REPO, dst_repo): subfolder = ""
53
- else:
54
- dst_repo, subfolder = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
55
- subfolder = subfolder.removesuffix("/")
56
  if auto_dir: subfolder = source_repo
57
 
58
  if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token): raise gr.Error(f"Repo already exists {dst_repo}")
@@ -124,18 +147,14 @@ def duplicate_m2o(source_repos_str, dst_repo, repo_type, private, overwrite, oau
124
  except Exception as e:
125
  raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
126
 
 
 
127
  try:
128
- if re.fullmatch(REGEX_HF_REPO, dst_repo): subfolder_prefix = ""
129
- else:
130
- dst_repo, subfolder_prefix = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
131
- subfolder_prefix = subfolder.removesuffix("/")
132
  if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token): raise gr.Error(f"Repo already exists {dst_repo}")
133
  source_repos = parse_repos(source_repos_str)
134
  for source_repo in source_repos:
135
- if re.fullmatch(REGEX_HF_REPO, source_repo): target = ""
136
- else:
137
- source_repo, target = re.findall(r'^(?:http.+\.co/)?(?:datasets)?(?:spaces)?([\w_\-\.]+/[\w_\-\.]+)/?(?:blob/main/)?(?:resolve/main/)?(.+)?$', source_repo)[0]
138
- target = urllib.parse.unquote(target.removesuffix("/"))
139
 
140
  subfolder = subfolder_prefix + "/" + source_repo if subfolder_prefix else source_repo
141
 
@@ -184,9 +203,9 @@ def duplicate_m2m(source_repos_str, hf_user, repo_type, private, overwrite, remo
184
  source_repos = parse_repos(source_repos_str)
185
  repo_url_result = 'Find your repo '
186
  for source_repo in source_repos:
187
- if not re.fullmatch(REGEX_HF_REPO, source_repo) or not api.repo_exists(repo_id=source_repo, repo_type=repo_type, token=hf_token): continue
188
  dst_repo = hf_user + "/" + repo_prefix + source_repo.split("/")[-1] + repo_suffix
189
- if not re.fullmatch(REGEX_HF_REPO, dst_repo): continue
190
  if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token):
191
  gr.Info(f"Repo already exists {dst_repo}")
192
  continue
@@ -241,9 +260,9 @@ with gr.Blocks(css=css) as demo:
241
  repo_type = gr.Dropdown(label="repo_type", choices=REPO_TYPES, value="model")
242
  with gr.Row():
243
  is_private = gr.Checkbox(label="Make new repo private?", value=True)
244
- is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=True)
245
- is_subdir = gr.Checkbox(label="Create subdirectories automatically?", value=True)
246
- is_remtag = gr.Checkbox(label="Remove NFAA tag?", value=True)
247
  with gr.Row():
248
  submit_button = gr.Button("Submit", variant="primary")
249
  clear_button = gr.Button("Clear", variant="secondary")
@@ -291,7 +310,7 @@ with gr.Blocks(css=css) as demo:
291
  with gr.Row():
292
  m2m_is_private = gr.Checkbox(label="Make new repo private?", value=True)
293
  m2m_is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=False)
294
- m2m_is_remtag = gr.Checkbox(label="Remove NFAA tag?", value=True)
295
  with gr.Row():
296
  m2m_submit_button = gr.Button("Submit", variant="primary")
297
  m2m_clear_button = gr.Button("Clear", variant="secondary")
 
6
  import tempfile
7
  import shutil
8
  import urllib
 
9
  from huggingface_hub import whoami, HfApi, hf_hub_download, RepoCard
10
  from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
11
  from gradio_huggingfacehub_search import HuggingfaceHubSearch
 
19
  HF_REPO_SUFFIX = os.environ.get("HF_REPO_SUFFIX") if os.environ.get("HF_REPO_SUFFIX") else "" # set your default repo suffix
20
  HF_USER = os.environ.get("HF_USER") if os.environ.get("HF_USER") else "" # set your username
21
  REGEX_HF_REPO = r'^[\w_\-\.]+/[\w_\-\.]+$'
22
+ REGEX_HF_PATH = r'^[\w_\-\.]+/[\w_\-\.]+(/?:.+)?$'
23
+
24
+ def is_valid_reponame(repo_id: str):
25
+ return re.fullmatch(REGEX_HF_REPO, repo_id)
26
+
27
+ def is_valid_path(hf_path: str):
28
+ return re.fullmatch(REGEX_HF_PATH, hf_path)
29
+
30
+ def extract_src_reponame(source_repo: str):
31
+ try:
32
+ if is_valid_reponame(source_repo): target = ""
33
+ else:
34
+ source_repo, target = re.findall(r'^(?:http.+\.co/)?(?:datasets)?(?:spaces)?([\w_\-\.]+/[\w_\-\.]+)/?(?:blob/main/)?(?:resolve/main/)?(.+)?$', source_repo)[0]
35
+ target = urllib.parse.unquote(target.removesuffix("/"))
36
+ return source_repo, target
37
+ except Exception as e:
38
+ print(e)
39
+ return source_repo, ""
40
+
41
+ def extract_dst_reponame(dst_repo: str):
42
+ try:
43
+ if is_valid_reponame(dst_repo): subfolder = ""
44
+ else:
45
+ dst_repo, subfolder = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
46
+ subfolder = subfolder.removesuffix("/")
47
+ return dst_repo, subfolder
48
+ except Exception as e:
49
+ print(e)
50
+ return dst_repo, ""
51
 
52
  def remove_repo_tags(repo_id: str, tags: list[str], repo_type: str, hf_token: str):
53
  try:
 
71
  except Exception as e:
72
  raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
73
 
74
+ if not is_valid_path(dst_repo): raise gr.Error(f"Invalid dst_repo: {dst_repo}")
 
 
 
 
75
 
76
+ try:
77
+ source_repo, target = extract_src_reponame(source_repo)
78
+ dst_repo, subfolder = extract_dst_reponame(dst_repo)
 
79
  if auto_dir: subfolder = source_repo
80
 
81
  if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token): raise gr.Error(f"Repo already exists {dst_repo}")
 
147
  except Exception as e:
148
  raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
149
 
150
+ if not is_valid_path(dst_repo): raise gr.Error(f"Invalid dst_repo: {dst_repo}")
151
+
152
  try:
153
+ dst_repo, subfolder_prefix = extract_dst_reponame(dst_repo)
 
 
 
154
  if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token): raise gr.Error(f"Repo already exists {dst_repo}")
155
  source_repos = parse_repos(source_repos_str)
156
  for source_repo in source_repos:
157
+ source_repo, target = extract_src_reponame(source_repo)
 
 
 
158
 
159
  subfolder = subfolder_prefix + "/" + source_repo if subfolder_prefix else source_repo
160
 
 
203
  source_repos = parse_repos(source_repos_str)
204
  repo_url_result = 'Find your repo '
205
  for source_repo in source_repos:
206
+ if not is_valid_reponame(source_repo) or not api.repo_exists(repo_id=source_repo, repo_type=repo_type, token=hf_token): continue
207
  dst_repo = hf_user + "/" + repo_prefix + source_repo.split("/")[-1] + repo_suffix
208
+ if not is_valid_reponame(dst_repo): continue
209
  if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token):
210
  gr.Info(f"Repo already exists {dst_repo}")
211
  continue
 
260
  repo_type = gr.Dropdown(label="repo_type", choices=REPO_TYPES, value="model")
261
  with gr.Row():
262
  is_private = gr.Checkbox(label="Make new repo private?", value=True)
263
+ is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=False)
264
+ is_subdir = gr.Checkbox(label="Create subdirectories automatically?", value=False)
265
+ is_remtag = gr.Checkbox(label="Remove NFAA tag?", info="To avoid Inference API bug", value=False)
266
  with gr.Row():
267
  submit_button = gr.Button("Submit", variant="primary")
268
  clear_button = gr.Button("Clear", variant="secondary")
 
310
  with gr.Row():
311
  m2m_is_private = gr.Checkbox(label="Make new repo private?", value=True)
312
  m2m_is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=False)
313
+ m2m_is_remtag = gr.Checkbox(label="Remove NFAA tag?", info="To avoid Inference API bug", value=True)
314
  with gr.Row():
315
  m2m_submit_button = gr.Button("Submit", variant="primary")
316
  m2m_clear_button = gr.Button("Clear", variant="secondary")