Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import git | |
import shutil | |
import zipfile | |
# Function to clone a GitHub or Hugging Face Space repository | |
def clone_repo(repo_url): | |
try: | |
# Extract repo name from URL | |
repo_name = repo_url.split('/')[-1] | |
# Remove the folder if it already exists (optional step) | |
if os.path.exists(repo_name): | |
shutil.rmtree(repo_name) | |
# Clone the repository | |
git.Repo.clone_from(repo_url, repo_name) | |
# Zip the cloned repository | |
zip_file = f"{repo_name}.zip" | |
shutil.make_archive(repo_name, 'zip', repo_name) | |
# Return the path to the zip file for downloading | |
return zip_file | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
# Gradio Interface | |
def download_repo(repo_type, repo_url): | |
if repo_type in ["GitHub", "Hugging Face"]: | |
zip_file = clone_repo(repo_url) | |
if os.path.exists(zip_file): | |
return zip_file # Return path to the zip file for download | |
else: | |
return "Failed to create the ZIP file." | |
else: | |
return "Invalid repository type selected." | |
# Gradio Interface with file download | |
def zip_download_link(repo_type, repo_url): | |
zip_file = download_repo(repo_type, repo_url) | |
if isinstance(zip_file, str) and zip_file.endswith(".zip"): | |
return gr.File(zip_file) # Provide a downloadable file | |
else: | |
return "An error occurred." | |
# Create the Gradio interface | |
interface = gr.Interface( | |
fn=zip_download_link, | |
inputs=[ | |
gr.Radio(choices=["GitHub", "Hugging Face"], label="Repository Type"), | |
gr.Textbox(label="Repository URL (GitHub or Hugging Face)") | |
], | |
outputs="file", # File output to allow ZIP download | |
title="Repository Cloner & Downloader", | |
description="Enter the GitHub or Hugging Face Space URL to clone and download as a ZIP." | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
interface.launch() | |