Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
from pathlib import Path
|
|
|
|
|
|
|
| 4 |
from huggingface_hub import whoami, HfApi, hf_hub_download
|
| 5 |
from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
|
| 6 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
|
@@ -9,9 +11,10 @@ ENDPOINT = "https://huggingface.co"
|
|
| 9 |
# ENDPOINT = "http://localhost:5564"
|
| 10 |
|
| 11 |
REPO_TYPES = ["model", "dataset", "space"]
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
-
def duplicate(source_repo, dst_repo, repo_type, private, overwrite, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
|
| 15 |
print(oauth_token.token)
|
| 16 |
hf_token = oauth_token.token
|
| 17 |
api = HfApi(token=hf_token)
|
|
@@ -21,14 +24,21 @@ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, oauth_token:
|
|
| 21 |
_ = whoami(oauth_token.token)
|
| 22 |
# ^ this will throw if token is invalid
|
| 23 |
|
| 24 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
|
| 26 |
file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, token=hf_token)
|
| 27 |
if not Path(file).exists(): continue
|
| 28 |
if Path(file).is_dir(): # unused for now
|
| 29 |
-
api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=path, repo_type=repo_type, token=hf_token)
|
| 30 |
elif Path(file).is_file():
|
| 31 |
-
api.upload_file(repo_id=dst_repo, path_or_fileobj=file, path_in_repo=path, repo_type=repo_type, token=hf_token)
|
| 32 |
if Path(file).exists(): Path(file).unlink()
|
| 33 |
if repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}"
|
| 34 |
elif repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}"
|
|
@@ -59,10 +69,11 @@ interface = gr.Interface(
|
|
| 59 |
search_type=["model", "dataset", "space"],
|
| 60 |
sumbit_on_select=False,
|
| 61 |
),
|
| 62 |
-
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
|
| 63 |
gr.Dropdown(choices=REPO_TYPES, value="model"),
|
| 64 |
-
gr.Checkbox(label="Make new repo private?"),
|
| 65 |
-
gr.Checkbox(label="Overwrite existing repo?"),
|
|
|
|
| 66 |
],
|
| 67 |
outputs=[
|
| 68 |
gr.Markdown(label="output"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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
|
|
|
|
| 11 |
# ENDPOINT = "http://localhost:5564"
|
| 12 |
|
| 13 |
REPO_TYPES = ["model", "dataset", "space"]
|
| 14 |
+
HF_REPO = os.environ.get("HF_REPO") if os.environ.get("HF_REPO") else "" # set your default repo
|
| 15 |
|
| 16 |
|
| 17 |
+
def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
|
| 18 |
print(oauth_token.token)
|
| 19 |
hf_token = oauth_token.token
|
| 20 |
api = HfApi(token=hf_token)
|
|
|
|
| 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]
|
| 30 |
+
subfolder = subfolder.removesuffix("/")
|
| 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)
|
| 40 |
elif Path(file).is_file():
|
| 41 |
+
api.upload_file(repo_id=dst_repo, path_or_fileobj=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
|
| 42 |
if Path(file).exists(): Path(file).unlink()
|
| 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}"
|
|
|
|
| 69 |
search_type=["model", "dataset", "space"],
|
| 70 |
sumbit_on_select=False,
|
| 71 |
),
|
| 72 |
+
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)", value=HF_REPO),
|
| 73 |
gr.Dropdown(choices=REPO_TYPES, value="model"),
|
| 74 |
+
gr.Checkbox(label="Make new repo private?", value=True),
|
| 75 |
+
gr.Checkbox(label="Overwrite existing repo?", value=True),
|
| 76 |
+
gr.Checkbox(label="Create subdirectories automatically?", value=True),
|
| 77 |
],
|
| 78 |
outputs=[
|
| 79 |
gr.Markdown(label="output"),
|