Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def clone_repo(repo_id: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Clones a Hugging Face repository into the Space's local storage.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
repo_id: The ID of the repository to clone (e.g., 'meta-llama/Llama-2-7b-hf').
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
A string indicating the status of the clone operation.
|
| 14 |
+
"""
|
| 15 |
+
if not repo_id or len(repo_id.strip()) == 0:
|
| 16 |
+
return "β Error: Repository ID cannot be empty. Please enter a valid ID."
|
| 17 |
+
|
| 18 |
+
# Construct the full repository URL
|
| 19 |
+
repo_url = f"https://huggingface.co/{repo_id}"
|
| 20 |
+
|
| 21 |
+
# Use the repo name as the local directory name
|
| 22 |
+
local_dir_name = repo_id.split('/')[-1]
|
| 23 |
+
|
| 24 |
+
# Prevent cloning into an existing directory
|
| 25 |
+
if os.path.exists(local_dir_name):
|
| 26 |
+
return f"β οΈ Warning: Directory '{local_dir_name}' already exists. The repository may have been cloned already."
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Execute the 'git clone' command
|
| 30 |
+
# The '--depth 1' flag does a shallow clone to save time and space
|
| 31 |
+
# Remove it if you need the full commit history
|
| 32 |
+
process = subprocess.run(
|
| 33 |
+
["git", "clone", "--depth", "1", repo_url],
|
| 34 |
+
capture_output=True,
|
| 35 |
+
text=True,
|
| 36 |
+
check=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Combine stdout and stderr for a complete log
|
| 40 |
+
log_output = f"STDOUT:\n{process.stdout}\n\nSTDERR:\n{process.stderr}"
|
| 41 |
+
return f"β
Success! Cloned '{repo_id}' into '/{local_dir_name}'.\n\nπ **Log:**\n```\n{log_output}\n```"
|
| 42 |
+
|
| 43 |
+
except subprocess.CalledProcessError as e:
|
| 44 |
+
# Handle errors from the git command (e.g., repo not found)
|
| 45 |
+
error_output = f"STDOUT:\n{e.stdout}\n\nSTDERR:\n{e.stderr}"
|
| 46 |
+
return f"β **Error:** Failed to clone repository '{repo_id}'.\nIt might be private or spelled incorrectly.\n\nπ **Log:**\n```\n{error_output}\n```"
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
# Catch any other unexpected errors
|
| 50 |
+
return f"An unexpected error occurred: {str(e)}"
|
| 51 |
+
|
| 52 |
+
# --- Gradio Interface ---
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 54 |
+
gr.Markdown(
|
| 55 |
+
"""
|
| 56 |
+
# π€ Hugging Face Repository Cloner
|
| 57 |
+
Enter a repository ID to clone it into this Space's persistent storage.
|
| 58 |
+
"""
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
repo_id_input = gr.Textbox(
|
| 63 |
+
label="HF Repository ID",
|
| 64 |
+
placeholder="e.g., meta-llama/Llama-2-7b-chat-hf",
|
| 65 |
+
scale=4
|
| 66 |
+
)
|
| 67 |
+
clone_button = gr.Button("Clone Repository", variant="primary", scale=1)
|
| 68 |
+
|
| 69 |
+
output_status = gr.Markdown(label="Clone Status")
|
| 70 |
+
|
| 71 |
+
clone_button.click(
|
| 72 |
+
fn=clone_repo,
|
| 73 |
+
inputs=repo_id_input,
|
| 74 |
+
outputs=output_status
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
gr.Examples(
|
| 78 |
+
["gradio-app/gradio", "CompVis/stable-diffusion-v1-4", "facebook/dino"],
|
| 79 |
+
inputs=repo_id_input
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
demo.launch()
|