Spaces:
Running
Running
Upload 2 files
Browse files- app.py +89 -72
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,73 +1,90 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
from
|
| 4 |
-
from huggingface_hub
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
.
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from huggingface_hub import whoami, HfApi, hf_hub_download, hf_hub_url
|
| 5 |
+
from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
|
| 6 |
+
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
| 7 |
+
|
| 8 |
+
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)
|
| 18 |
+
try:
|
| 19 |
+
if not repo_type in REPO_TYPES:
|
| 20 |
+
raise ValueError("need to select valid repo type")
|
| 21 |
+
_ = whoami(oauth_token.token)
|
| 22 |
+
# ^ this will throw if token is invalid
|
| 23 |
+
|
| 24 |
+
if overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token):
|
| 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():
|
| 29 |
+
api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=Path(file).name, 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(file).name, repo_type=repo_type, token=hf_token)
|
| 32 |
+
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}"
|
| 35 |
+
else: repo_url = f"https://huggingface.co/{dst_repo}"
|
| 36 |
+
else:
|
| 37 |
+
r = requests.post(
|
| 38 |
+
f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
|
| 39 |
+
headers=build_hf_headers(token=oauth_token.token),
|
| 40 |
+
json={"repository": dst_repo, "private": private},
|
| 41 |
+
)
|
| 42 |
+
hf_raise_for_status(r)
|
| 43 |
+
|
| 44 |
+
repo_url = r.json().get("url")
|
| 45 |
+
|
| 46 |
+
return (
|
| 47 |
+
f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
|
| 48 |
+
"sp.jpg",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
|
| 53 |
+
|
| 54 |
+
interface = gr.Interface(
|
| 55 |
+
fn=duplicate,
|
| 56 |
+
inputs=[
|
| 57 |
+
HuggingfaceHubSearch(
|
| 58 |
+
placeholder="Source repository (e.g. osanseviero/src)",
|
| 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"),
|
| 69 |
+
gr.Image(show_label=False),
|
| 70 |
+
],
|
| 71 |
+
title="Duplicate your repo!",
|
| 72 |
+
description="Duplicate a Hugging Face repository! This Space is a an experimental demo.",
|
| 73 |
+
allow_flagging="never",
|
| 74 |
+
live=False
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
def swap_visibilty(profile: gr.OAuthProfile | None):
|
| 78 |
+
return gr.update(elem_classes=["main_ui_logged_in"]) if profile else gr.update(elem_classes=["main_ui_logged_out"])
|
| 79 |
+
|
| 80 |
+
css = '''
|
| 81 |
+
.main_ui_logged_out{opacity: 0.3; pointer-events: none}
|
| 82 |
+
'''
|
| 83 |
+
with gr.Blocks(css=css) as demo:
|
| 84 |
+
gr.LoginButton()
|
| 85 |
+
with gr.Column(elem_classes="main_ui_logged_out") as main_ui:
|
| 86 |
+
interface.render()
|
| 87 |
+
demo.load(fn=swap_visibilty, outputs=main_ui)
|
| 88 |
+
|
| 89 |
+
demo.queue()
|
| 90 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
huggingface_hub
|
| 2 |
-
gradio_huggingfacehub_search
|
|
|
|
| 1 |
+
huggingface_hub>=0.22.2
|
| 2 |
+
gradio_huggingfacehub_search>=0.0.7
|