Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import git
|
| 4 |
import shutil
|
|
|
|
| 5 |
|
| 6 |
# Function to clone a GitHub or Hugging Face Space repository
|
| 7 |
def clone_repo(repo_url):
|
|
@@ -9,33 +10,51 @@ def clone_repo(repo_url):
|
|
| 9 |
# Extract repo name from URL
|
| 10 |
repo_name = repo_url.split('/')[-1]
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Clone the repository
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
return f"An error occurred: {str(e)}"
|
| 20 |
|
| 21 |
# Gradio Interface
|
| 22 |
def download_repo(repo_type, repo_url):
|
| 23 |
-
# Handle both GitHub and Hugging Face Space repositories with git clone
|
| 24 |
if repo_type in ["GitHub", "Hugging Face"]:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
return "Invalid repository type selected."
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Create the Gradio interface
|
| 30 |
interface = gr.Interface(
|
| 31 |
-
fn=
|
| 32 |
inputs=[
|
| 33 |
gr.Radio(choices=["GitHub", "Hugging Face"], label="Repository Type"),
|
| 34 |
gr.Textbox(label="Repository URL (GitHub or Hugging Face)")
|
| 35 |
],
|
| 36 |
-
outputs="
|
| 37 |
-
title="Repository Cloner",
|
| 38 |
-
description="Enter the GitHub
|
| 39 |
)
|
| 40 |
|
| 41 |
# Launch the app
|
|
|
|
| 2 |
import os
|
| 3 |
import git
|
| 4 |
import shutil
|
| 5 |
+
import zipfile
|
| 6 |
|
| 7 |
# Function to clone a GitHub or Hugging Face Space repository
|
| 8 |
def clone_repo(repo_url):
|
|
|
|
| 10 |
# Extract repo name from URL
|
| 11 |
repo_name = repo_url.split('/')[-1]
|
| 12 |
|
| 13 |
+
# Remove the folder if it already exists (optional step)
|
| 14 |
+
if os.path.exists(repo_name):
|
| 15 |
+
shutil.rmtree(repo_name)
|
| 16 |
+
|
| 17 |
# Clone the repository
|
| 18 |
+
git.Repo.clone_from(repo_url, repo_name)
|
| 19 |
+
|
| 20 |
+
# Zip the cloned repository
|
| 21 |
+
zip_file = f"{repo_name}.zip"
|
| 22 |
+
shutil.make_archive(repo_name, 'zip', repo_name)
|
| 23 |
+
|
| 24 |
+
# Return the path to the zip file for downloading
|
| 25 |
+
return zip_file
|
| 26 |
except Exception as e:
|
| 27 |
return f"An error occurred: {str(e)}"
|
| 28 |
|
| 29 |
# Gradio Interface
|
| 30 |
def download_repo(repo_type, repo_url):
|
|
|
|
| 31 |
if repo_type in ["GitHub", "Hugging Face"]:
|
| 32 |
+
zip_file = clone_repo(repo_url)
|
| 33 |
+
if os.path.exists(zip_file):
|
| 34 |
+
return zip_file # Return path to the zip file for download
|
| 35 |
+
else:
|
| 36 |
+
return "Failed to create the ZIP file."
|
| 37 |
else:
|
| 38 |
return "Invalid repository type selected."
|
| 39 |
|
| 40 |
+
# Gradio Interface with file download
|
| 41 |
+
def zip_download_link(repo_type, repo_url):
|
| 42 |
+
zip_file = download_repo(repo_type, repo_url)
|
| 43 |
+
if isinstance(zip_file, str) and zip_file.endswith(".zip"):
|
| 44 |
+
return gr.File(zip_file) # Provide a downloadable file
|
| 45 |
+
else:
|
| 46 |
+
return "An error occurred."
|
| 47 |
+
|
| 48 |
# Create the Gradio interface
|
| 49 |
interface = gr.Interface(
|
| 50 |
+
fn=zip_download_link,
|
| 51 |
inputs=[
|
| 52 |
gr.Radio(choices=["GitHub", "Hugging Face"], label="Repository Type"),
|
| 53 |
gr.Textbox(label="Repository URL (GitHub or Hugging Face)")
|
| 54 |
],
|
| 55 |
+
outputs="file", # File output to allow ZIP download
|
| 56 |
+
title="Repository Cloner & Downloader",
|
| 57 |
+
description="Enter the GitHub or Hugging Face Space URL to clone and download as a ZIP."
|
| 58 |
)
|
| 59 |
|
| 60 |
# Launch the app
|