Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import
|
| 3 |
|
| 4 |
def fetch_files(source_repo, hf_token):
|
| 5 |
# Input validation
|
|
@@ -9,133 +9,133 @@ def fetch_files(source_repo, hf_token):
|
|
| 9 |
if not source_repo.strip():
|
| 10 |
yield gr.update(), gr.update(), "Error: Source repository name is required.", []
|
| 11 |
return
|
| 12 |
-
|
| 13 |
api = HfApi(token=hf_token.strip())
|
| 14 |
-
|
| 15 |
try:
|
| 16 |
-
# Initial status update before fetching
|
| 17 |
yield gr.update(), gr.update(), f"Status: Fetching file list from '{source_repo}'...", []
|
| 18 |
-
|
| 19 |
-
# Fetch the flat list of files from the repository
|
| 20 |
files = api.list_repo_files(repo_id=source_repo.strip(), repo_type="model")
|
| 21 |
files.sort()
|
| 22 |
-
|
| 23 |
-
# Build a visual Linux-like tree representation for the CheckboxGroup
|
| 24 |
choices = []
|
| 25 |
for f in files:
|
| 26 |
-
parts = f.split(
|
| 27 |
if len(parts) == 1:
|
| 28 |
display = f"π {f}"
|
| 29 |
else:
|
| 30 |
folder_path = "/".join(parts[:-1])
|
| 31 |
display = f"π {folder_path}/ βββ π {parts[-1]}"
|
| 32 |
-
|
| 33 |
-
# Gradio allows (display_name, value) tuples for CheckboxGroup choices
|
| 34 |
choices.append((display, f))
|
| 35 |
-
|
| 36 |
yield (
|
| 37 |
-
gr.update(visible=True),
|
| 38 |
-
gr.update(choices=choices, value=files),
|
| 39 |
f"Success: Fetched {len(files)} files. Uncheck the ones you do not want to copy.",
|
| 40 |
-
files
|
| 41 |
)
|
| 42 |
-
|
| 43 |
except Exception as e:
|
| 44 |
yield gr.update(visible=False), gr.update(), f"An error occurred: {str(e)}", []
|
| 45 |
|
| 46 |
|
| 47 |
-
def
|
| 48 |
if not target_repo.strip():
|
| 49 |
yield "Error: Target repository name is required."
|
| 50 |
return
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
api = HfApi(token=hf_token.strip())
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
)
|
| 63 |
-
|
| 64 |
-
# 2
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Execute deletion in a single commit natively on HF servers
|
| 74 |
-
api.create_commit(
|
| 75 |
-
repo_id=target_repo.strip(),
|
| 76 |
-
repo_type="model",
|
| 77 |
-
operations=operations,
|
| 78 |
-
commit_message="Clean up: Remove unselected files during selective clone"
|
| 79 |
)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
yield f"An error occurred: {str(e)}"
|
| 85 |
|
| 86 |
|
| 87 |
-
# Notice: The 'theme' parameter has been removed from Blocks to satisfy Gradio 6.0 specs
|
| 88 |
with gr.Blocks(title="Selective HF Model Cloner") as demo:
|
| 89 |
gr.Markdown("# π Selective Hugging Face Model Cloner")
|
| 90 |
-
gr.Markdown(
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
with gr.Row():
|
| 93 |
hf_token = gr.Textbox(
|
| 94 |
-
label="Hugging Face Access Token",
|
| 95 |
-
placeholder="hf_...",
|
| 96 |
type="password",
|
| 97 |
-
info="Requires 'Write' permissions."
|
| 98 |
)
|
| 99 |
-
|
| 100 |
with gr.Row():
|
| 101 |
source_repo = gr.Textbox(
|
| 102 |
-
label="Source Model Repository",
|
| 103 |
-
placeholder="e.g.,
|
| 104 |
)
|
| 105 |
fetch_btn = gr.Button("1. Fetch Files", variant="secondary")
|
| 106 |
-
|
| 107 |
-
# We use a Gradio State to quietly keep track of the complete list of files fetched
|
| 108 |
all_files_state = gr.State([])
|
| 109 |
-
|
| 110 |
-
# This group remains hidden until files are successfully fetched
|
| 111 |
with gr.Group(visible=False) as selection_group:
|
| 112 |
gr.Markdown("### ποΈ Repository Files")
|
| 113 |
-
gr.Markdown("*
|
| 114 |
-
|
| 115 |
file_checkboxes = gr.CheckboxGroup(label="Repository Tree", interactive=True)
|
| 116 |
-
|
| 117 |
target_repo = gr.Textbox(
|
| 118 |
-
label="Target Model Repository",
|
| 119 |
-
placeholder="e.g., your-username/my-custom-
|
| 120 |
)
|
| 121 |
-
clone_btn = gr.Button("2.
|
| 122 |
-
|
| 123 |
output_logs = gr.Textbox(label="Execution Status / Logs", interactive=False)
|
| 124 |
-
|
| 125 |
-
# Trigger Step 1: File Fetching
|
| 126 |
fetch_btn.click(
|
| 127 |
fn=fetch_files,
|
| 128 |
inputs=[source_repo, hf_token],
|
| 129 |
-
outputs=[selection_group, file_checkboxes, output_logs, all_files_state]
|
| 130 |
)
|
| 131 |
-
|
| 132 |
-
# Trigger Step 2: Cloning Process
|
| 133 |
clone_btn.click(
|
| 134 |
-
fn=
|
| 135 |
-
inputs=[source_repo, target_repo, hf_token, file_checkboxes
|
| 136 |
-
outputs=output_logs
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
-
# The theme parameter is now passed into launch() per Gradio 6.0 rules
|
| 141 |
demo.queue().launch(theme=gr.themes.Soft())
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import CommitOperationCopy, HfApi
|
| 3 |
|
| 4 |
def fetch_files(source_repo, hf_token):
|
| 5 |
# Input validation
|
|
|
|
| 9 |
if not source_repo.strip():
|
| 10 |
yield gr.update(), gr.update(), "Error: Source repository name is required.", []
|
| 11 |
return
|
| 12 |
+
|
| 13 |
api = HfApi(token=hf_token.strip())
|
| 14 |
+
|
| 15 |
try:
|
|
|
|
| 16 |
yield gr.update(), gr.update(), f"Status: Fetching file list from '{source_repo}'...", []
|
| 17 |
+
|
|
|
|
| 18 |
files = api.list_repo_files(repo_id=source_repo.strip(), repo_type="model")
|
| 19 |
files.sort()
|
| 20 |
+
|
|
|
|
| 21 |
choices = []
|
| 22 |
for f in files:
|
| 23 |
+
parts = f.split("/")
|
| 24 |
if len(parts) == 1:
|
| 25 |
display = f"π {f}"
|
| 26 |
else:
|
| 27 |
folder_path = "/".join(parts[:-1])
|
| 28 |
display = f"π {folder_path}/ βββ π {parts[-1]}"
|
|
|
|
|
|
|
| 29 |
choices.append((display, f))
|
| 30 |
+
|
| 31 |
yield (
|
| 32 |
+
gr.update(visible=True),
|
| 33 |
+
gr.update(choices=choices, value=files),
|
| 34 |
f"Success: Fetched {len(files)} files. Uncheck the ones you do not want to copy.",
|
| 35 |
+
files,
|
| 36 |
)
|
| 37 |
+
|
| 38 |
except Exception as e:
|
| 39 |
yield gr.update(visible=False), gr.update(), f"An error occurred: {str(e)}", []
|
| 40 |
|
| 41 |
|
| 42 |
+
def copy_repository_files(source_repo, target_repo, hf_token, selected_files):
|
| 43 |
if not target_repo.strip():
|
| 44 |
yield "Error: Target repository name is required."
|
| 45 |
return
|
| 46 |
+
if not selected_files:
|
| 47 |
+
yield "Error: At least one file must be selected to copy."
|
| 48 |
+
return
|
| 49 |
+
|
| 50 |
api = HfApi(token=hf_token.strip())
|
| 51 |
+
src_clean = source_repo.strip()
|
| 52 |
+
tgt_clean = target_repo.strip()
|
| 53 |
+
|
| 54 |
try:
|
| 55 |
+
# Step 1: Ensure target repo exists (create private or public target model)
|
| 56 |
+
yield f"Status: Ensuring destination repository '{tgt_clean}' exists..."
|
| 57 |
+
api.create_repo(
|
| 58 |
+
repo_id=tgt_clean,
|
| 59 |
+
repo_type="model",
|
| 60 |
+
exist_ok=True,
|
| 61 |
+
private=True # Recommended default for gated clones
|
| 62 |
)
|
| 63 |
+
|
| 64 |
+
# Step 2: Build server-side copy operations for selected files
|
| 65 |
+
yield f"Status: Preparing server-side copy for {len(selected_files)} files..."
|
| 66 |
+
operations = [
|
| 67 |
+
CommitOperationCopy(
|
| 68 |
+
src_path_in_repo=f,
|
| 69 |
+
path_in_repo=f,
|
| 70 |
+
src_repo_id=src_clean,
|
| 71 |
+
src_repo_type="model",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
+
for f in selected_files
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
# Step 3: Execute commit natively on Hugging Face servers
|
| 77 |
+
yield f"Status: Committing {len(operations)} files directly to '{tgt_clean}'..."
|
| 78 |
+
api.create_commit(
|
| 79 |
+
repo_id=tgt_clean,
|
| 80 |
+
repo_type="model",
|
| 81 |
+
operations=operations,
|
| 82 |
+
commit_message=f"Selective clone of {len(selected_files)} files from {src_clean}",
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
yield f"Success! Transferred {len(selected_files)} files. View model: https://huggingface.co/{tgt_clean}"
|
| 86 |
+
|
| 87 |
except Exception as e:
|
| 88 |
yield f"An error occurred: {str(e)}"
|
| 89 |
|
| 90 |
|
|
|
|
| 91 |
with gr.Blocks(title="Selective HF Model Cloner") as demo:
|
| 92 |
gr.Markdown("# π Selective Hugging Face Model Cloner")
|
| 93 |
+
gr.Markdown(
|
| 94 |
+
"Fetch files from any accessible source repo (including gated models) and copy selected files directly on the server without downloading weights."
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
with gr.Row():
|
| 98 |
hf_token = gr.Textbox(
|
| 99 |
+
label="Hugging Face Access Token",
|
| 100 |
+
placeholder="hf_...",
|
| 101 |
type="password",
|
| 102 |
+
info="Requires 'Write' permissions on your account and accepted terms on gated models.",
|
| 103 |
)
|
| 104 |
+
|
| 105 |
with gr.Row():
|
| 106 |
source_repo = gr.Textbox(
|
| 107 |
+
label="Source Model Repository",
|
| 108 |
+
placeholder="e.g., black-forest-labs/flux-3-image-klein",
|
| 109 |
)
|
| 110 |
fetch_btn = gr.Button("1. Fetch Files", variant="secondary")
|
| 111 |
+
|
|
|
|
| 112 |
all_files_state = gr.State([])
|
| 113 |
+
|
|
|
|
| 114 |
with gr.Group(visible=False) as selection_group:
|
| 115 |
gr.Markdown("### ποΈ Repository Files")
|
| 116 |
+
gr.Markdown("*Uncheck files you want to omit from the destination repository.*")
|
| 117 |
+
|
| 118 |
file_checkboxes = gr.CheckboxGroup(label="Repository Tree", interactive=True)
|
| 119 |
+
|
| 120 |
target_repo = gr.Textbox(
|
| 121 |
+
label="Target Model Repository",
|
| 122 |
+
placeholder="e.g., your-username/my-custom-model",
|
| 123 |
)
|
| 124 |
+
clone_btn = gr.Button("2. Copy Selected Files", variant="primary")
|
| 125 |
+
|
| 126 |
output_logs = gr.Textbox(label="Execution Status / Logs", interactive=False)
|
| 127 |
+
|
|
|
|
| 128 |
fetch_btn.click(
|
| 129 |
fn=fetch_files,
|
| 130 |
inputs=[source_repo, hf_token],
|
| 131 |
+
outputs=[selection_group, file_checkboxes, output_logs, all_files_state],
|
| 132 |
)
|
| 133 |
+
|
|
|
|
| 134 |
clone_btn.click(
|
| 135 |
+
fn=copy_repository_files,
|
| 136 |
+
inputs=[source_repo, target_repo, hf_token, file_checkboxes],
|
| 137 |
+
outputs=output_logs,
|
| 138 |
)
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
|
|
|
| 141 |
demo.queue().launch(theme=gr.themes.Soft())
|