File size: 3,298 Bytes
2b6e504
c1b5f9e
9194337
c1b5f9e
 
 
 
 
316e88f
c1b5f9e
 
 
 
 
79ac7b6
c1b5f9e
 
 
 
 
79ac7b6
 
ee94e30
c1b5f9e
 
79ac7b6
 
c1b5f9e
 
79ac7b6
 
 
 
 
c1b5f9e
79ac7b6
c1b5f9e
 
 
 
79ac7b6
 
c1b5f9e
 
79ac7b6
c1b5f9e
 
 
 
 
 
 
79ac7b6
c1b5f9e
 
 
 
79ac7b6
 
 
ee94e30
79ac7b6
 
 
 
051fe19
c1b5f9e
01970a5
79ac7b6
 
 
 
 
01970a5
 
 
 
9194337
01970a5
c1b5f9e
ee94e30
c1b5f9e
 
8e720f5
 
 
79ac7b6
8e720f5
9194337
 
8e77934
45e9fba
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import gradio as gr
from huggingface_hub import create_repo, list_models

def show_profile(profile: gr.OAuthProfile | None) -> str:
    """Update login status."""
    if profile is None:
        return "*Not logged in.*"
    return f"✅ Logged in as **{profile.username}**"

def list_private_models(
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None
) -> str:
    """Demo: list user’s private/public models."""
    if profile is None or oauth_token is None:
        return "Please log in to see your models."
    models = [
        f"{model.id} ({'private' if model.private else 'public'})"
        for model in list_models(author=profile.username, token=oauth_token.token)
    ]
    return "No models found." if not models \
        else "Models:\n\n" + "\n - ".join(models)

def create_space(
    repo_name: str,
    sdk: str,
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None
) -> tuple[str, str, str]:
    """
    Create (or get) a Hugging Face Space with the chosen SDK template,
    returning (repo_id, logs, iframe).
    """
    if profile is None or oauth_token is None:
        return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
    repo_id = f"{profile.username}/{repo_name}"
    create_repo(
        repo_id=repo_id,
        token=oauth_token.token,
        exist_ok=True,
        repo_type="space",
        space_sdk=sdk
    )
    url    = f"https://huggingface.co/spaces/{repo_id}"
    logmsg = f"✅ Space ready: {url} (SDK: {sdk})"
    iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
    return repo_id, logmsg, iframe

with gr.Blocks(title="HF OAuth + Space Creator") as demo:
    gr.Markdown(
        "## Sign in with Hugging Face + Create a Space\n\n"
        "1. Click **Sign in**.\n"
        "2. Pick a template, enter a name, and **Create Space**.\n\n"
        "---"
    )

    # — Login UI —
    login_btn  = gr.LoginButton(variant="huggingface", size="lg")
    status_md  = gr.Markdown("*Not logged in.*")
    models_md  = gr.Markdown()

    demo.load(show_profile,             inputs=None, outputs=status_md)
    login_btn.click(show_profile,       inputs=None, outputs=status_md)
    demo.load(list_private_models,      inputs=None, outputs=models_md)
    login_btn.click(list_private_models,inputs=None, outputs=models_md)

    # — Create Space UI —
    repo_name      = gr.Textbox(label="New Space name", placeholder="my-space-name")
    sdk_selector   = gr.Radio(
        choices=["gradio", "streamlit"],
        value="gradio",
        label="Space template (SDK)"
    )
    create_btn     = gr.Button("Create Space", interactive=False)
    session_id     = gr.Textbox(visible=False)
    logs           = gr.Textbox(label="Logs", interactive=False, lines=3)
    preview_iframe = gr.HTML("<p>No Space created yet.</p>")

    def enable_create(profile: gr.OAuthProfile | None):
        return gr.update(interactive=profile is not None)

    demo.load(enable_create,       inputs=None, outputs=create_btn)
    login_btn.click(enable_create, inputs=None, outputs=create_btn)

    create_btn.click(
        fn=create_space,
        inputs=[repo_name, sdk_selector],
        outputs=[session_id, logs, preview_iframe]
    )

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