Spaces:
Running
Running
import gradio as gr | |
import os | |
import git | |
from huggingface_hub import snapshot_download, login | |
import shutil | |
# Function to clone a GitHub repository | |
def download_github_repo(repo_url): | |
try: | |
# Extract repo name from URL | |
repo_name = repo_url.split('/')[-1] | |
# Clone the repository | |
if not os.path.exists(repo_name): | |
git.Repo.clone_from(repo_url, repo_name) | |
return f"GitHub repository '{repo_name}' downloaded successfully." | |
else: | |
return f"GitHub repository '{repo_name}' already exists locally." | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
# Function to download a Hugging Face model or dataset with authentication | |
def download_huggingface_repo(repo_id, hf_token): | |
try: | |
# Log in using the Hugging Face token | |
login(token=hf_token) | |
# Download the Hugging Face model or dataset snapshot | |
repo_path = snapshot_download(repo_id, token=hf_token) | |
return f"Hugging Face repository '{repo_id}' downloaded successfully." | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
# Gradio Interface | |
def download_repo(repo_type, repo_url_or_id, hf_token=""): | |
if repo_type == "GitHub": | |
return download_github_repo(repo_url_or_id) | |
elif repo_type == "Hugging Face": | |
return download_huggingface_repo(repo_url_or_id, hf_token) | |
else: | |
return "Invalid repository type selected." | |
# Create the Gradio interface | |
interface = gr.Interface( | |
fn=download_repo, | |
inputs=[ | |
gr.Radio(choices=["GitHub", "Hugging Face"], label="Repository Type"), | |
gr.Textbox(label="Repository URL (GitHub) or Repo ID (Hugging Face)"), | |
gr.Textbox(label="Hugging Face Token (Leave blank if accessing public repo)", type="password") | |
], | |
outputs="text", | |
title="Repository Downloader", | |
description="Enter the GitHub repo URL or Hugging Face repo ID to download. If using Hugging Face private repo, provide your access token." | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
interface.launch() | |