Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,99 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import create_repo, upload_file, Repository, whoami
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
|
| 6 |
def duplicate(source_repo, dst_repo, token, repo_type):
|
| 7 |
-
#
|
|
|
|
| 8 |
repo_namespace, dst_id = dst_repo.split("/")
|
| 9 |
-
|
| 10 |
-
org = None
|
| 11 |
-
if repo_namespace != username:
|
| 12 |
-
org = repo_namespace
|
| 13 |
|
| 14 |
# Create the destination repo
|
| 15 |
if repo_type in ["space", "dataset"]:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
else:
|
| 20 |
-
url = create_repo(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Clone source repo
|
| 23 |
endpoint = "huggingface.co/"
|
| 24 |
if repo_type in ["space", "dataset"]:
|
| 25 |
-
endpoint +=
|
| 26 |
full_path = f"https://{username}:{token}@{endpoint}{source_repo}"
|
| 27 |
-
local_dir = "hub/
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
if
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if ".git" not in root:
|
| 42 |
-
# remove hub/namespace/reponame
|
| 43 |
-
directory_path_in_repo = "/".join(root.split("/")[3:])
|
| 44 |
path_in_repo = os.path.join(directory_path_in_repo, f)
|
| 45 |
local_file_path = os.path.join(local_dir, path_in_repo)
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
for filename in os.listdir(local_dir):
|
| 52 |
-
file_path = os.path.join(local_dir, filename)
|
| 53 |
-
if os.path.isfile(file_path) or os.path.islink(file_path):
|
| 54 |
-
os.unlink(file_path)
|
| 55 |
-
elif os.path.isdir(file_path):
|
| 56 |
-
shutil.rmtree(file_path)
|
| 57 |
|
| 58 |
-
return f"Find your repo <a href='{url}' target=
|
| 59 |
|
|
|
|
| 60 |
interface = gr.Interface(
|
| 61 |
fn=duplicate,
|
| 62 |
inputs=[
|
| 63 |
-
gr.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
],
|
| 68 |
-
outputs=
|
| 69 |
title="Duplicate your repo!",
|
| 70 |
-
description="Duplicate a Hugging Face repository! You need to specify a write token obtained
|
| 71 |
-
article="
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import create_repo, upload_file, Repository, whoami
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
|
| 6 |
def duplicate(source_repo, dst_repo, token, repo_type):
|
| 7 |
+
# Get username from token
|
| 8 |
+
username = whoami(token=token)["name"]
|
| 9 |
repo_namespace, dst_id = dst_repo.split("/")
|
| 10 |
+
org = repo_namespace if repo_namespace != username else None
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Create the destination repo
|
| 13 |
if repo_type in ["space", "dataset"]:
|
| 14 |
+
url = create_repo(
|
| 15 |
+
repo_id=dst_id,
|
| 16 |
+
token=token,
|
| 17 |
+
organization=org,
|
| 18 |
+
repo_type=repo_type,
|
| 19 |
+
space_sdk="gradio" if repo_type == "space" else None,
|
| 20 |
+
private=False
|
| 21 |
+
)
|
| 22 |
else:
|
| 23 |
+
url = create_repo(
|
| 24 |
+
repo_id=dst_id,
|
| 25 |
+
token=token,
|
| 26 |
+
organization=org,
|
| 27 |
+
private=False
|
| 28 |
+
)
|
| 29 |
|
| 30 |
# Clone source repo
|
| 31 |
endpoint = "huggingface.co/"
|
| 32 |
if repo_type in ["space", "dataset"]:
|
| 33 |
+
endpoint += f"{repo_type}/"
|
| 34 |
full_path = f"https://{username}:{token}@{endpoint}{source_repo}"
|
| 35 |
+
local_dir = f"hub/{source_repo}"
|
| 36 |
|
| 37 |
+
# Clone repository
|
| 38 |
+
repo = Repository(
|
| 39 |
+
local_dir=local_dir,
|
| 40 |
+
clone_from=full_path,
|
| 41 |
+
repo_type=repo_type if repo_type in ["space", "dataset"] else None,
|
| 42 |
+
use_auth_token=token
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
# Upload files
|
| 46 |
+
for root, _, files in os.walk(local_dir):
|
| 47 |
+
if not root.startswith(".") and ".git" not in root:
|
| 48 |
+
for f in files:
|
| 49 |
+
if not f.startswith("."):
|
| 50 |
+
directory_path_in_repo = "/".join(root.split("/")[2:])
|
|
|
|
|
|
|
|
|
|
| 51 |
path_in_repo = os.path.join(directory_path_in_repo, f)
|
| 52 |
local_file_path = os.path.join(local_dir, path_in_repo)
|
| 53 |
+
upload_file(
|
| 54 |
+
path_or_fileobj=local_file_path,
|
| 55 |
+
path_in_repo=path_in_repo,
|
| 56 |
+
repo_id=dst_repo,
|
| 57 |
+
token=token,
|
| 58 |
+
repo_type=repo_type if repo_type != "model" else None
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
# Clean up
|
| 62 |
+
shutil.rmtree(local_dir, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
return f"Find your repo <a href='{url}' target='_blank' style='text-decoration:underline'>here</a>"
|
| 65 |
|
| 66 |
+
# Updated Gradio interface
|
| 67 |
interface = gr.Interface(
|
| 68 |
fn=duplicate,
|
| 69 |
inputs=[
|
| 70 |
+
gr.Textbox(
|
| 71 |
+
label="Source repository",
|
| 72 |
+
placeholder="e.g. osanseviero/src"
|
| 73 |
+
),
|
| 74 |
+
gr.Textbox(
|
| 75 |
+
label="Destination repository",
|
| 76 |
+
placeholder="e.g. osanseviero/dst"
|
| 77 |
+
),
|
| 78 |
+
gr.Textbox(
|
| 79 |
+
label="Write access token",
|
| 80 |
+
type="password",
|
| 81 |
+
placeholder="Your HF token"
|
| 82 |
+
),
|
| 83 |
+
gr.Dropdown(
|
| 84 |
+
choices=["model", "dataset", "space"],
|
| 85 |
+
label="Repository type",
|
| 86 |
+
value="model"
|
| 87 |
+
)
|
| 88 |
],
|
| 89 |
+
outputs=gr.HTML(),
|
| 90 |
title="Duplicate your repo!",
|
| 91 |
+
description="Duplicate a Hugging Face repository! You need to specify a write token obtained from https://hf.co/settings/tokens. This Space is an experimental demo.",
|
| 92 |
+
article="""
|
| 93 |
+
<p>Find your write token at
|
| 94 |
+
<a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>
|
| 95 |
+
""",
|
| 96 |
+
allow_flagging="never"
|
| 97 |
)
|
| 98 |
+
|
| 99 |
+
interface.launch()
|