Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,18 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import create_repo, upload_file, whoami, Repository
|
| 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 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
# Create the destination repo
|
|
|
|
|
|
|
| 14 |
if repo_type in ["space", "dataset"]:
|
| 15 |
url = create_repo(
|
| 16 |
repo_id=dst_repo,
|
|
@@ -27,51 +30,80 @@ def duplicate(source_repo, dst_repo, token, repo_type):
|
|
| 27 |
)
|
| 28 |
|
| 29 |
# Clone source repo
|
|
|
|
|
|
|
| 30 |
endpoint = "huggingface.co/"
|
| 31 |
if repo_type in ["space", "dataset"]:
|
| 32 |
endpoint += f"{repo_type}/"
|
| 33 |
full_path = f"https://{username}:{token}@{endpoint}{source_repo}"
|
| 34 |
local_dir = f"hub/{source_repo}"
|
| 35 |
|
| 36 |
-
# Clone repository
|
| 37 |
repo = Repository(
|
| 38 |
local_dir=local_dir,
|
| 39 |
clone_from=full_path,
|
| 40 |
repo_type=repo_type if repo_type in ["space", "dataset"] else None,
|
| 41 |
-
token=token
|
| 42 |
)
|
| 43 |
|
| 44 |
-
#
|
|
|
|
| 45 |
for root, _, files in os.walk(local_dir):
|
| 46 |
if not root.startswith(".") and ".git" not in root:
|
| 47 |
for f in files:
|
| 48 |
if not f.startswith("."):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Clean up
|
|
|
|
|
|
|
| 61 |
shutil.rmtree(local_dir, ignore_errors=True)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Updated Gradio interface
|
| 66 |
with gr.Blocks(title="Duplicate your repo!") as interface:
|
| 67 |
gr.Markdown("""
|
| 68 |
# Duplicate your repo!
|
| 69 |
-
Duplicate a Hugging Face repository! You need
|
| 70 |
This Space is an experimental demo.
|
| 71 |
""")
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
-
|
|
|
|
| 75 |
source_input = gr.Textbox(
|
| 76 |
label="Source repository",
|
| 77 |
placeholder="e.g. osanseviero/src"
|
|
@@ -91,9 +123,16 @@ with gr.Blocks(title="Duplicate your repo!") as interface:
|
|
| 91 |
value="model"
|
| 92 |
)
|
| 93 |
submit_btn = gr.Button("Duplicate")
|
| 94 |
-
|
| 95 |
-
with gr.Column():
|
| 96 |
output = gr.HTML(label="Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
gr.Markdown("""
|
| 99 |
Find your write token at
|
|
@@ -103,7 +142,7 @@ with gr.Blocks(title="Duplicate your repo!") as interface:
|
|
| 103 |
submit_btn.click(
|
| 104 |
fn=duplicate,
|
| 105 |
inputs=[source_input, dest_input, token_input, repo_type_input],
|
| 106 |
-
outputs=output
|
| 107 |
)
|
| 108 |
|
| 109 |
interface.launch()
|
|
|
|
| 2 |
from huggingface_hub import create_repo, upload_file, whoami, Repository
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
+
def duplicate(source_repo, dst_repo, token, repo_type, progress=gr.Progress()):
|
| 8 |
# Get username from token
|
| 9 |
username = whoami(token=token)["name"]
|
| 10 |
|
| 11 |
+
progress(0, desc="Starting duplication process")
|
| 12 |
+
logs = ["Starting duplication process..."]
|
| 13 |
|
| 14 |
# Create the destination repo
|
| 15 |
+
progress(0.1, desc="Creating destination repository")
|
| 16 |
+
logs.append("Creating destination repository")
|
| 17 |
if repo_type in ["space", "dataset"]:
|
| 18 |
url = create_repo(
|
| 19 |
repo_id=dst_repo,
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
# Clone source repo
|
| 33 |
+
progress(0.2, desc="Cloning source repository")
|
| 34 |
+
logs.append("Cloning source repository")
|
| 35 |
endpoint = "huggingface.co/"
|
| 36 |
if repo_type in ["space", "dataset"]:
|
| 37 |
endpoint += f"{repo_type}/"
|
| 38 |
full_path = f"https://{username}:{token}@{endpoint}{source_repo}"
|
| 39 |
local_dir = f"hub/{source_repo}"
|
| 40 |
|
|
|
|
| 41 |
repo = Repository(
|
| 42 |
local_dir=local_dir,
|
| 43 |
clone_from=full_path,
|
| 44 |
repo_type=repo_type if repo_type in ["space", "dataset"] else None,
|
| 45 |
+
token=token
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Get list of files to upload
|
| 49 |
+
files_to_upload = []
|
| 50 |
for root, _, files in os.walk(local_dir):
|
| 51 |
if not root.startswith(".") and ".git" not in root:
|
| 52 |
for f in files:
|
| 53 |
if not f.startswith("."):
|
| 54 |
+
files_to_upload.append((root, f))
|
| 55 |
+
|
| 56 |
+
# Upload files with individual progress
|
| 57 |
+
progress(0.3, desc="Preparing to upload files")
|
| 58 |
+
logs.append(f"Found {len(files_to_upload)} files to upload")
|
| 59 |
+
|
| 60 |
+
for i, (root, f) in enumerate(files_to_upload):
|
| 61 |
+
file_progress = (i + 1) / len(files_to_upload)
|
| 62 |
+
overall_progress = 0.3 + (0.6 * file_progress) # 0.3 to 0.9 range for upload
|
| 63 |
+
directory_path_in_repo = "/".join(root.split("/")[2:])
|
| 64 |
+
path_in_repo = os.path.join(directory_path_in_repo, f)
|
| 65 |
+
local_file_path = os.path.join(local_dir, path_in_repo)
|
| 66 |
+
|
| 67 |
+
progress(
|
| 68 |
+
(overall_progress, [
|
| 69 |
+
(file_progress, f"Uploading {f}"),
|
| 70 |
+
]),
|
| 71 |
+
desc=f"Uploading file {i+1}/{len(files_to_upload)}"
|
| 72 |
+
)
|
| 73 |
+
logs.append(f"Uploaded {f} to {path_in_repo}")
|
| 74 |
+
|
| 75 |
+
upload_file(
|
| 76 |
+
path_or_fileobj=local_file_path,
|
| 77 |
+
path_in_repo=path_in_repo,
|
| 78 |
+
repo_id=dst_repo,
|
| 79 |
+
token=token,
|
| 80 |
+
repo_type=repo_type if repo_type != "model" else None
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
# Clean up
|
| 84 |
+
progress(0.95, desc="Cleaning up temporary files")
|
| 85 |
+
logs.append("Cleaning up temporary files")
|
| 86 |
shutil.rmtree(local_dir, ignore_errors=True)
|
| 87 |
|
| 88 |
+
progress(1.0, desc="Completed")
|
| 89 |
+
logs.append("Duplication completed successfully")
|
| 90 |
+
|
| 91 |
+
return (
|
| 92 |
+
f"Find your repo <a href='{url}' target='_blank' style='text-decoration:underline'>here</a>",
|
| 93 |
+
"\n".join(logs)
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
# Updated Gradio interface with progress panel
|
| 97 |
with gr.Blocks(title="Duplicate your repo!") as interface:
|
| 98 |
gr.Markdown("""
|
| 99 |
# Duplicate your repo!
|
| 100 |
+
Duplicate a Hugging Face repository! You need a write token from https://hf.co/settings/tokens.
|
| 101 |
This Space is an experimental demo.
|
| 102 |
""")
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
+
# Left panel - Inputs
|
| 106 |
+
with gr.Column(scale=2):
|
| 107 |
source_input = gr.Textbox(
|
| 108 |
label="Source repository",
|
| 109 |
placeholder="e.g. osanseviero/src"
|
|
|
|
| 123 |
value="model"
|
| 124 |
)
|
| 125 |
submit_btn = gr.Button("Duplicate")
|
|
|
|
|
|
|
| 126 |
output = gr.HTML(label="Result")
|
| 127 |
+
|
| 128 |
+
# Right panel - Progress
|
| 129 |
+
with gr.Column(scale=1):
|
| 130 |
+
with gr.Group():
|
| 131 |
+
gr.Markdown("## Progress")
|
| 132 |
+
global_progress = gr.Progress(label="Overall Progress")
|
| 133 |
+
with gr.Column(variant="panel"):
|
| 134 |
+
file_progress = gr.Progress(label="File Progress", track_color="yellow")
|
| 135 |
+
log_output = gr.Textbox(label="Progress Log", lines=10)
|
| 136 |
|
| 137 |
gr.Markdown("""
|
| 138 |
Find your write token at
|
|
|
|
| 142 |
submit_btn.click(
|
| 143 |
fn=duplicate,
|
| 144 |
inputs=[source_input, dest_input, token_input, repo_type_input],
|
| 145 |
+
outputs=[output, log_output]
|
| 146 |
)
|
| 147 |
|
| 148 |
interface.launch()
|