Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,37 +2,43 @@ import uuid
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import create_repo
|
4 |
|
5 |
-
# Store created spaces' info
|
6 |
state_store = {}
|
7 |
|
8 |
with gr.Blocks(title="HF Space Creator") as demo:
|
9 |
-
#
|
10 |
login_btn = gr.LoginButton(variant="huggingface", size="lg")
|
11 |
status_md = gr.Markdown("*Not logged in.*")
|
12 |
|
13 |
def show_profile(profile: gr.OAuthProfile | None) -> str:
|
14 |
-
return "*Not logged in.*" if profile is None
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
preview_iframe = gr.HTML("<p>No Space created yet.</p>")
|
26 |
|
27 |
-
# Enable create button only after login
|
28 |
def enable_create(profile: gr.OAuthProfile | None):
|
29 |
return gr.update(interactive=(profile is not None))
|
30 |
-
login_btn.click(fn=enable_create, inputs=None, outputs=[create_btn])
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
repo_id = f"{username}/{name}"
|
37 |
create_repo(
|
38 |
repo_id=repo_id,
|
@@ -41,7 +47,7 @@ with gr.Blocks(title="HF Space Creator") as demo:
|
|
41 |
repo_type="space"
|
42 |
)
|
43 |
url = f"https://huggingface.co/spaces/{repo_id}"
|
44 |
-
logs = f"Created or found Space: {url}"
|
45 |
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
46 |
return repo_id, logs, iframe
|
47 |
|
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import create_repo
|
4 |
|
|
|
5 |
state_store = {}
|
6 |
|
7 |
with gr.Blocks(title="HF Space Creator") as demo:
|
8 |
+
# --- Login UI ---
|
9 |
login_btn = gr.LoginButton(variant="huggingface", size="lg")
|
10 |
status_md = gr.Markdown("*Not logged in.*")
|
11 |
|
12 |
def show_profile(profile: gr.OAuthProfile | None) -> str:
|
13 |
+
return "*Not logged in.*" if profile is None \
|
14 |
+
else f"✅ Logged in as **{profile.username}**"
|
15 |
+
|
16 |
+
# Update status on load and on login/logout
|
17 |
+
demo.load(fn=show_profile, inputs=[login_btn], outputs=[status_md])
|
18 |
+
login_btn.click(fn=show_profile, inputs=[login_btn], outputs=[status_md])
|
19 |
+
|
20 |
+
# Enable “Create Space” button only once logged in
|
21 |
+
repo_name = gr.Textbox(label="New Space name", placeholder="your-repo-name")
|
22 |
+
create_btn = gr.Button("Create Space", interactive=False)
|
23 |
+
session_id = gr.Textbox(value="", visible=False)
|
24 |
+
logs = gr.Textbox(label="Logs", interactive=False, lines=5)
|
25 |
preview_iframe = gr.HTML("<p>No Space created yet.</p>")
|
26 |
|
|
|
27 |
def enable_create(profile: gr.OAuthProfile | None):
|
28 |
return gr.update(interactive=(profile is not None))
|
|
|
29 |
|
30 |
+
demo.load(fn=enable_create, inputs=[login_btn], outputs=[create_btn])
|
31 |
+
login_btn.click(fn=enable_create, inputs=[login_btn], outputs=[create_btn])
|
32 |
+
|
33 |
+
# Create Space callback
|
34 |
+
def create_space(
|
35 |
+
name: str,
|
36 |
+
profile: gr.OAuthProfile | None,
|
37 |
+
oauth_token: gr.OAuthToken | None
|
38 |
+
) -> tuple[str,str,str]:
|
39 |
+
if oauth_token is None or profile is None:
|
40 |
+
return "", "⚠️ Please sign in first.", "<p>No Space created yet.</p>"
|
41 |
+
username = profile.username
|
42 |
repo_id = f"{username}/{name}"
|
43 |
create_repo(
|
44 |
repo_id=repo_id,
|
|
|
47 |
repo_type="space"
|
48 |
)
|
49 |
url = f"https://huggingface.co/spaces/{repo_id}"
|
50 |
+
logs = f"✅ Created or found Space: {url}"
|
51 |
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
52 |
return repo_id, logs, iframe
|
53 |
|