File size: 1,867 Bytes
ded0ba7
 
 
7606652
ded0ba7
 
 
b86fa09
 
 
 
 
 
b6daf8b
ded0ba7
7606652
a441613
7606652
 
 
db5da8b
7606652
b86fa09
 
db5da8b
7606652
 
db5da8b
b6daf8b
db5da8b
7606652
db5da8b
7606652
 
db5da8b
7606652
a441613
b6daf8b
a504c5f
ded0ba7
 
7606652
 
ded0ba7
b86fa09
ded0ba7
7606652
2cb5f58
7606652
b86fa09
ded0ba7
9375019
b755c85
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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):
    # Extract user profile & OAuth token from the 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:
        # Clone GitHub repo
        cloned_repo = Repo.clone_from(repo_git, folder)

        # Upload to Hugging Face
        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}")  # Clean up the cloned folder

# Gradio UI
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)