File size: 2,062 Bytes
db5eb35
 
 
d418a31
12451eb
d418a31
 
db5eb35
d418a31
 
 
 
 
 
f6b4a60
d418a31
 
 
 
db5eb35
d418a31
 
 
 
 
 
 
 
 
 
 
 
db5eb35
 
 
 
 
d418a31
 
 
db5eb35
d418a31
 
db5eb35
d418a31
db5eb35
 
d418a31
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
import gradio as gr
from huggingface_hub import HfApi
from git import Repo
import uuid
from slugify import slugify
import os
import shutil

def clone(progress=gr.Progress(), profile=None, oauth_token=None, repo_git=None, repo_hf=None, sdk_type=None):
    """Clones and deploys with progress updates."""
    try:
        progress(0.1, desc="Cloning repository...")
        folder = str(uuid.uuid4())
        Repo.clone_from(repo_git, folder)

        progress(0.4, desc="Creating Hugging Face Space...")
        api = HfApi(token=oauth_token.token)
        repo_id = f"{profile.username}/{slugify(repo_hf)}"
        api.create_repo(repo_id=repo_id, repo_type="space", space_sdk=sdk_type)

        progress(0.7, desc="Uploading files...")
        with open(os.path.join(folder, "space_config.json"), "w") as f:
            import json
            json.dump({"sdk": sdk_type}, f)
        api.upload_folder(folder_path=folder, repo_id=repo_id, repo_type="space")

        shutil.rmtree(folder)
        progress(1.0, desc="Deployment complete!")
        return f"Deployment successful! [View your space](https://huggingface.co/spaces/{repo_id})"

    except Exception as e:
        return f"<span style='color:red;'>Error: {e}</span>"

with gr.Blocks() as demo:
    gr.LoginButton()
    with gr.Row():
        with gr.Column():
            repo_git = gr.Textbox(label="GitHub Repo", placeholder="https://github.com/...", tooltip="Enter the GitHub repository URL.")
            repo_hf = gr.Textbox(label="Space Name", placeholder="my-space", tooltip="Enter a name for your Hugging Face Space.")
            sdk_choices = gr.Radio(["gradio", "streamlit", "docker", "static"], label="SDK", tooltip="Select the SDK for your Space.")
        with gr.Column():
            output = gr.Markdown(label="Output")
            progress = gr.Progress()
    btn = gr.Button("Bring over!")
    btn.click(fn=clone, inputs=[progress, gr.OAuthProfile(), gr.OAuthToken(), repo_git, repo_hf, sdk_choices], outputs=output)

if __name__ == "__main__":
    demo.launch(auth_required=True)