Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import git
|
| 4 |
-
from huggingface_hub import snapshot_download, login
|
| 5 |
import shutil
|
| 6 |
|
| 7 |
-
# Function to clone a GitHub repository
|
| 8 |
-
def
|
| 9 |
try:
|
| 10 |
# Extract repo name from URL
|
| 11 |
repo_name = repo_url.split('/')[-1]
|
|
@@ -13,40 +12,17 @@ def download_github_repo(repo_url):
|
|
| 13 |
# Clone the repository
|
| 14 |
if not os.path.exists(repo_name):
|
| 15 |
git.Repo.clone_from(repo_url, repo_name)
|
| 16 |
-
return f"
|
| 17 |
else:
|
| 18 |
-
return f"
|
| 19 |
-
except Exception as e:
|
| 20 |
-
return f"An error occurred: {str(e)}"
|
| 21 |
-
|
| 22 |
-
# Function to download a Hugging Face Space or model/dataset using an environment variable for the token
|
| 23 |
-
def download_huggingface_repo(repo_id):
|
| 24 |
-
try:
|
| 25 |
-
# Get Hugging Face token from environment variable
|
| 26 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 27 |
-
if hf_token is None:
|
| 28 |
-
return "Hugging Face token not found. Please set the HF_TOKEN environment variable."
|
| 29 |
-
|
| 30 |
-
# Log in using the Hugging Face token
|
| 31 |
-
login(token=hf_token)
|
| 32 |
-
|
| 33 |
-
# Check if it's a Hugging Face Space
|
| 34 |
-
if repo_id.startswith("spaces/"):
|
| 35 |
-
repo_path = snapshot_download(repo_id, repo_type="space", token=hf_token)
|
| 36 |
-
else:
|
| 37 |
-
# Download the Hugging Face model or dataset snapshot
|
| 38 |
-
repo_path = snapshot_download(repo_id, token=hf_token)
|
| 39 |
-
|
| 40 |
-
return f"Hugging Face repository '{repo_id}' downloaded successfully."
|
| 41 |
except Exception as e:
|
| 42 |
return f"An error occurred: {str(e)}"
|
| 43 |
|
| 44 |
# Gradio Interface
|
| 45 |
-
def download_repo(repo_type,
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return download_huggingface_repo(repo_url_or_id)
|
| 50 |
else:
|
| 51 |
return "Invalid repository type selected."
|
| 52 |
|
|
@@ -55,11 +31,11 @@ interface = gr.Interface(
|
|
| 55 |
fn=download_repo,
|
| 56 |
inputs=[
|
| 57 |
gr.Radio(choices=["GitHub", "Hugging Face"], label="Repository Type"),
|
| 58 |
-
gr.Textbox(label="Repository URL (GitHub
|
| 59 |
],
|
| 60 |
outputs="text",
|
| 61 |
-
title="Repository
|
| 62 |
-
description="Enter the GitHub repo URL or Hugging Face
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch the app
|
|
|
|
| 1 |
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):
|
| 8 |
try:
|
| 9 |
# Extract repo name from URL
|
| 10 |
repo_name = repo_url.split('/')[-1]
|
|
|
|
| 12 |
# Clone the repository
|
| 13 |
if not os.path.exists(repo_name):
|
| 14 |
git.Repo.clone_from(repo_url, repo_name)
|
| 15 |
+
return f"Repository '{repo_name}' cloned successfully from {repo_url}."
|
| 16 |
else:
|
| 17 |
+
return f"Repository '{repo_name}' already exists locally."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
return clone_repo(repo_url)
|
|
|
|
| 26 |
else:
|
| 27 |
return "Invalid repository type selected."
|
| 28 |
|
|
|
|
| 31 |
fn=download_repo,
|
| 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="text",
|
| 37 |
+
title="Repository Cloner",
|
| 38 |
+
description="Enter the GitHub repo URL or Hugging Face Space URL to clone."
|
| 39 |
)
|
| 40 |
|
| 41 |
# Launch the app
|