Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import requests
|
|
3 |
from pathlib import Path
|
4 |
import re
|
5 |
import os
|
|
|
|
|
6 |
from huggingface_hub import whoami, HfApi, hf_hub_download
|
7 |
from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
|
8 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
@@ -23,7 +25,10 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oa
|
|
23 |
raise ValueError("need to select valid repo type")
|
24 |
_ = whoami(oauth_token.token)
|
25 |
# ^ this will throw if token is invalid
|
26 |
-
|
|
|
|
|
|
|
27 |
if re.fullmatch(r'^[\w_\-\.]+/[\w_\-\.]+$', dst_repo): subfolder = ""
|
28 |
else:
|
29 |
dst_repo, subfolder = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
|
@@ -31,9 +36,10 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oa
|
|
31 |
if auto_dir: subfolder = source_repo
|
32 |
|
33 |
if overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token) or subfolder:
|
|
|
34 |
api.create_repo(repo_id=dst_repo, repo_type=repo_type, private=private, exist_ok=True, token=hf_token)
|
35 |
for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
|
36 |
-
file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, token=hf_token)
|
37 |
if not Path(file).exists(): continue
|
38 |
if Path(file).is_dir(): # unused for now
|
39 |
api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
|
@@ -43,6 +49,7 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oa
|
|
43 |
if repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}"
|
44 |
elif repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}"
|
45 |
else: repo_url = f"https://huggingface.co/{dst_repo}"
|
|
|
46 |
else:
|
47 |
r = requests.post(
|
48 |
f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
|
@@ -59,7 +66,7 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oa
|
|
59 |
)
|
60 |
|
61 |
except Exception as e:
|
62 |
-
|
63 |
|
64 |
interface = gr.Interface(
|
65 |
fn=duplicate,
|
|
|
3 |
from pathlib import Path
|
4 |
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
|
|
|
25 |
raise ValueError("need to select valid repo type")
|
26 |
_ = whoami(oauth_token.token)
|
27 |
# ^ this will throw if token is invalid
|
28 |
+
except Exception as e:
|
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]
|
|
|
36 |
if auto_dir: subfolder = source_repo
|
37 |
|
38 |
if overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token) or subfolder:
|
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
|
45 |
api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
|
|
|
49 |
if repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}"
|
50 |
elif repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}"
|
51 |
else: repo_url = f"https://huggingface.co/{dst_repo}"
|
52 |
+
shutil.rmtree(temp_dir)
|
53 |
else:
|
54 |
r = requests.post(
|
55 |
f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
|
|
|
66 |
)
|
67 |
|
68 |
except Exception as e:
|
69 |
+
print(e)
|
70 |
|
71 |
interface = gr.Interface(
|
72 |
fn=duplicate,
|