Nymbo's picture
Update app.py
ffce82d verified
import gradio as gr
import tempfile
import shutil
from git import Repo
from github import Github
import os
def clone_and_push(hf_username, hf_space_name, github_repo_name, github_token):
tmp_dir = None
try:
# Prepare the git URL for the Hugging Face Spaces repository
hf_repo_url = f"https://huggingface.co/spaces/{hf_username}/{hf_space_name}.git"
# Create a temporary directory to clone the repository
tmp_dir = tempfile.mkdtemp()
# Clone the Hugging Face Spaces repository
repo = Repo.clone_from(hf_repo_url, tmp_dir)
# Authenticate with GitHub using the provided token
g = Github(github_token)
user = g.get_user()
# Create a new GitHub repository
github_repo = user.create_repo(github_repo_name)
# Remove the existing 'origin' remote pointing to Hugging Face
repo.delete_remote(repo.remotes.origin)
# Construct the authenticated GitHub URL
github_username = user.login
github_remote_url = f"https://{github_username}:{github_token}@github.com/{github_username}/{github_repo_name}.git"
# Add the new 'origin' remote pointing to GitHub
repo.create_remote('origin', github_remote_url)
# Push the code to the new GitHub repository
repo.git.push('origin', 'HEAD:refs/heads/main', set_upstream=True)
return f"Successfully pushed to GitHub repository: {github_repo.html_url}"
except Exception as e:
return f"An error occurred: {str(e)}"
finally:
# Clean up the temporary directory
if tmp_dir and os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
iface = gr.Interface(
theme="Nymbo/Nymbo_Theme",
fn=clone_and_push,
inputs=[
gr.Textbox(label="Hugging Face Username", placeholder="e.g., john_doe"),
gr.Textbox(label="Hugging Face Spaces Name", placeholder="e.g., my-space"),
gr.Textbox(label="GitHub Repository Name", placeholder="e.g., my-new-repo"),
gr.Textbox(label="GitHub Personal Access Token", type="password"),
],
outputs="text",
title="Hugging Face Spaces to GitHub Repo",
description="""
Enter the details to clone a Hugging Face Spaces repository and push it to a new GitHub repository.
**Note:** Your GitHub personal access token must have permissions to create repositories and push code (usually the 'repo' scope).
""",
)
if __name__ == "__main__":
iface.launch()