Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import HfApi, snapshot_download
|
| 5 |
+
|
| 6 |
+
def clone_repository(source_repo, target_repo, hf_token):
|
| 7 |
+
if not hf_token.strip():
|
| 8 |
+
return "Error: Hugging Face Access Token is required."
|
| 9 |
+
if not source_repo.strip() or not target_repo.strip():
|
| 10 |
+
return "Error: Both source and target repository names are required."
|
| 11 |
+
|
| 12 |
+
api = HfApi(token=hf_token.strip())
|
| 13 |
+
local_dir = "temp_clone_dir"
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# 1. Create the target repository on your account
|
| 17 |
+
yield f"Status: Creating target repository '{target_repo}'..."
|
| 18 |
+
api.create_repo(repo_id=target_repo.strip(), repo_type="model", exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# 2. Clean up any leftover temporary directories from previous runs
|
| 21 |
+
if os.path.exists(local_dir):
|
| 22 |
+
shutil.rmtree(local_dir)
|
| 23 |
+
|
| 24 |
+
# 3. Download the source repository files
|
| 25 |
+
yield f"Status: Downloading files from '{source_repo}' (this may take some time depending on the model size)..."
|
| 26 |
+
downloaded_dir = snapshot_download(
|
| 27 |
+
repo_id=source_repo.strip(),
|
| 28 |
+
local_dir=local_dir,
|
| 29 |
+
token=hf_token.strip(),
|
| 30 |
+
max_workers=4
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# 4. Upload the files to your target repository
|
| 34 |
+
yield f"Status: Uploading files to '{target_repo}'..."
|
| 35 |
+
api.upload_folder(
|
| 36 |
+
folder_path=downloaded_dir,
|
| 37 |
+
repo_id=target_repo.strip(),
|
| 38 |
+
repo_type="model"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# 5. Clean up local storage
|
| 42 |
+
shutil.rmtree(local_dir)
|
| 43 |
+
|
| 44 |
+
yield f"Success! Model successfully cloned to: https://huggingface.co/{target_repo.strip()}"
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
# Ensure cleanup happens even if an error occurs
|
| 48 |
+
if os.path.exists(local_dir):
|
| 49 |
+
shutil.rmtree(local_dir)
|
| 50 |
+
yield f"An error occurred: {str(e)}"
|
| 51 |
+
|
| 52 |
+
# Build the Gradio Interface using the .Soft theme
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="HF Model Cloner") as demo:
|
| 54 |
+
gr.Markdown("# 📑 Hugging Face Model Cloner")
|
| 55 |
+
gr.Markdown("Clone any public or accessible model repository to your own Hugging Face account under a custom name.")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
hf_token = gr.Textbox(
|
| 59 |
+
label="Hugging Face Access Token",
|
| 60 |
+
placeholder="hf_...",
|
| 61 |
+
type="password",
|
| 62 |
+
info="Requires 'Write' permissions to create and upload to your repository."
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
source_repo = gr.Textbox(
|
| 67 |
+
label="Source Model Repository",
|
| 68 |
+
placeholder="e.g., meta-llama/Llama-3.2-1B"
|
| 69 |
+
)
|
| 70 |
+
target_repo = gr.Textbox(
|
| 71 |
+
label="Target Model Repository",
|
| 72 |
+
placeholder="e.g., your-username/my-custom-llama"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
clone_btn = gr.Button("Start Cloning Process", variant="primary")
|
| 76 |
+
output_logs = gr.Textbox(label="Execution Status / Logs", interactive=False)
|
| 77 |
+
|
| 78 |
+
# Use streaming output (yield) to show live progress updates
|
| 79 |
+
clone_btn.click(
|
| 80 |
+
fn=clone_repository,
|
| 81 |
+
inputs=[source_repo, target_repo, hf_token],
|
| 82 |
+
outputs=output_logs
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.queue().launch()
|