hf-to-zip / app.py
soiz1's picture
Update app.py
d4925d2 verified
raw
history blame
1.6 kB
import os
import shutil
import zipfile
import gradio as gr
from git import Repo
def clone_and_zip_repo(repo_url):
# Directory settings
clone_dir = "cloned_repo"
zip_file = "repo.zip"
# Clean up old directories and files if they exist
if os.path.exists(clone_dir):
shutil.rmtree(clone_dir)
if os.path.exists(zip_file):
os.remove(zip_file)
try:
# Clone the repository
Repo.clone_from(repo_url, clone_dir)
# Create a zip file
with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(clone_dir):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, clone_dir)
zipf.write(file_path, arcname)
return zip_file
except Exception as e:
return f"Error: {str(e)}"
# Gradio interface
def download_repo(repo_url):
result = clone_and_zip_repo(repo_url)
if result.endswith(".zip"):
return result
else:
return result
with gr.Blocks() as demo:
gr.Markdown("## Clone and Download Hugging Face Repository")
with gr.Row():
repo_url_input = gr.Textbox(label="Hugging Face Repo URL", placeholder="Enter the repository URL")
with gr.Row():
download_button = gr.Button("Clone and Download")
with gr.Row():
output = gr.File(label="Download Zip File")
download_button.click(download_repo, inputs=[repo_url_input], outputs=[output])
# Run the Gradio app
demo.launch()