File size: 2,084 Bytes
76a7609
2b6e504
8e720f5
9194337
8e720f5
 
9194337
8e720f5
 
 
0e8ba2c
316e88f
0e8ba2c
5985660
 
8e720f5
17e2ef6
 
2b6e504
8e720f5
 
 
 
 
 
051fe19
8e720f5
 
 
 
9194337
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
import uuid
import gradio as gr
from huggingface_hub import create_repo

# Store created spaces' info
state_store = {}

with gr.Blocks(title="HF Space Creator") as demo:
    # 1) OAuth button + login status
    login_btn = gr.LoginButton(variant="huggingface", size="lg")
    status_md = gr.Markdown("*Not logged in.*")

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

    # Update status on page load and after click
    demo.load(fn=show_profile, inputs=None, outputs=[status_md])
    login_btn.click(fn=show_profile, inputs=None, outputs=[status_md])

    # 2) Repo creation UI
    repo_name = gr.Textbox(label="New Space name", placeholder="your-repo-name")
    create_btn = gr.Button("Create Space", interactive=False)
    session_id = gr.Textbox(value="", visible=False)
    logs = gr.Textbox(label="Logs", interactive=False, lines=5)
    preview_iframe = gr.HTML("<p>No Space created yet.</p>")

    # Enable create button only after login
    def enable_create(profile: gr.OAuthProfile | None):
        return gr.update(interactive=(profile is not None))
    login_btn.click(fn=enable_create, inputs=None, outputs=[create_btn])

    def create_space(name: str, oauth_token: gr.OAuthToken | None):
        if oauth_token is None:
            return "", "Please sign in first.", "<p>No Space created yet.</p>"
        username = oauth_token.user_info["username"]
        repo_id = f"{username}/{name}"
        create_repo(
            repo_id=repo_id,
            token=oauth_token.token,
            exist_ok=True,
            repo_type="space"
        )
        url = f"https://huggingface.co/spaces/{repo_id}"
        logs = f"Created or found Space: {url}"
        iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
        return repo_id, logs, iframe

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

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