|
|
import gradio as gr |
|
|
from huggingface_hub import HfApi |
|
|
from git import Repo |
|
|
import os |
|
|
import uuid |
|
|
from slugify import slugify |
|
|
|
|
|
def clone_and_upload(repo_git, repo_hf, sdk_type, github_token, request: gr.Request): |
|
|
|
|
|
profile = request.username |
|
|
oauth_token = request.oauth_token |
|
|
|
|
|
if not profile or not oauth_token: |
|
|
return "❌ Please log in first." |
|
|
|
|
|
folder = str(uuid.uuid4()) |
|
|
|
|
|
try: |
|
|
|
|
|
cloned_repo = Repo.clone_from(repo_git, folder) |
|
|
|
|
|
|
|
|
api = HfApi(token=oauth_token) |
|
|
hf_repo_id = f"{profile}/{slugify(repo_hf)}" |
|
|
|
|
|
api.create_repo(repo_id=hf_repo_id, repo_type="space", space_sdk=sdk_type) |
|
|
api.upload_folder(folder_path=folder, repo_id=hf_repo_id, repo_type="space") |
|
|
|
|
|
return f"✅ Uploaded to Hugging Face: https://huggingface.co/spaces/{hf_repo_id}" |
|
|
except Exception as e: |
|
|
return f"❌ Error: {str(e)}" |
|
|
finally: |
|
|
if os.path.exists(folder): |
|
|
os.system(f"rm -rf {folder}") |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
login_button = gr.LoginButton() |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
repo_git = gr.Textbox(label="GitHub Repository URL") |
|
|
repo_hf = gr.Textbox(label="Hugging Face Space Name") |
|
|
sdk_choices = gr.Radio(["gradio", "streamlit", "docker", "static"], label="SDK Choices") |
|
|
github_token = gr.Textbox(label="GitHub Token (Optional)", type="password") |
|
|
with gr.Column(): |
|
|
output = gr.Textbox(label="Output") |
|
|
|
|
|
btn = gr.Button("Bring over!") |
|
|
btn.click(fn=clone_and_upload, inputs=[repo_git, repo_hf, sdk_choices, github_token], outputs=output) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(auth="huggingface", show_api=False, share=True) |