John6666 commited on
Commit
ccc5a40
·
verified ·
1 Parent(s): 67fcdea

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -2
app.py CHANGED
@@ -5,6 +5,7 @@ import re
5
  import os
6
  import tempfile
7
  import shutil
 
8
  from huggingface_hub import whoami, HfApi, hf_hub_download
9
  from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
10
  from gradio_huggingfacehub_search import HuggingfaceHubSearch
@@ -14,7 +15,7 @@ ENDPOINT = "https://huggingface.co"
14
 
15
  REPO_TYPES = ["model", "dataset", "space"]
16
  HF_REPO = os.environ.get("HF_REPO") if os.environ.get("HF_REPO") else "" # set your default repo
17
-
18
 
19
  def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
20
  print(oauth_token.token)
@@ -29,7 +30,12 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oa
29
  raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
30
 
31
  try:
32
- if re.fullmatch(r'^[\w_\-\.]+/[\w_\-\.]+$', dst_repo): subfolder = ""
 
 
 
 
 
33
  else:
34
  dst_repo, subfolder = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
35
  subfolder = subfolder.removesuffix("/")
@@ -39,6 +45,7 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oa
39
  temp_dir = tempfile.mkdtemp()
40
  api.create_repo(repo_id=dst_repo, repo_type=repo_type, private=private, exist_ok=True, token=hf_token)
41
  for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
 
42
  file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, local_dir=temp_dir, token=hf_token)
43
  if not Path(file).exists(): continue
44
  if Path(file).is_dir(): # unused for now
 
5
  import os
6
  import tempfile
7
  import shutil
8
+ import urllib
9
  from huggingface_hub import whoami, HfApi, hf_hub_download
10
  from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
11
  from gradio_huggingfacehub_search import HuggingfaceHubSearch
 
15
 
16
  REPO_TYPES = ["model", "dataset", "space"]
17
  HF_REPO = os.environ.get("HF_REPO") if os.environ.get("HF_REPO") else "" # set your default repo
18
+ REGEX_HF_REPO = r'^[\w_\-\.]+/[\w_\-\.]+$'
19
 
20
  def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
21
  print(oauth_token.token)
 
30
  raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
31
 
32
  try:
33
+ if re.fullmatch(REGEX_HF_REPO, source_repo): target = ""
34
+ else:
35
+ source_repo, target = re.findall(r'^(?:http.+\.co/)?(?:datasets)?(?:spaces)?([\w_\-\.]+/[\w_\-\.]+)/?(?:blob/main/)?(?:resolve/main/)?(.+)?$', source_repo)[0]
36
+ target = urllib.parse.unquote(target.removesuffix("/"))
37
+
38
+ if re.fullmatch(REGEX_HF_REPO, dst_repo): subfolder = ""
39
  else:
40
  dst_repo, subfolder = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
41
  subfolder = subfolder.removesuffix("/")
 
45
  temp_dir = tempfile.mkdtemp()
46
  api.create_repo(repo_id=dst_repo, repo_type=repo_type, private=private, exist_ok=True, token=hf_token)
47
  for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
48
+ if target and target not in path: continue
49
  file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, local_dir=temp_dir, token=hf_token)
50
  if not Path(file).exists(): continue
51
  if Path(file).is_dir(): # unused for now