Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import create_repo
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
login_btn.click(show_profile, inputs=None, outputs=status_md)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
session_id = gr.Textbox(value="", visible=False)
|
18 |
-
logs = gr.Textbox(label="Logs", interactive=False, lines=5)
|
19 |
-
preview_iframe = gr.HTML("<p>No Space created yet.</p>")
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
oauth_token: gr.OAuthToken | None
|
30 |
-
) -> tuple[str,str,str]:
|
31 |
-
if oauth_token is None:
|
32 |
-
return "", "⚠️ Please sign in first.", "<p>No Space created yet.</p>"
|
33 |
-
username = oauth_token.user_info["username"]
|
34 |
-
repo_id = f"{username}/{name}"
|
35 |
-
create_repo(
|
36 |
-
repo_id=repo_id,
|
37 |
-
token=oauth_token.token,
|
38 |
-
exist_ok=True,
|
39 |
-
repo_type="space"
|
40 |
-
)
|
41 |
-
url = f"https://huggingface.co/spaces/{repo_id}"
|
42 |
-
logmsg = f"✅ Created or found Space: {url}"
|
43 |
-
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
44 |
-
return repo_id, logmsg, iframe
|
45 |
|
|
|
46 |
create_btn.click(
|
47 |
fn=create_space,
|
48 |
inputs=[repo_name],
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import create_repo, list_models
|
3 |
|
4 |
+
def show_profile(profile: gr.OAuthProfile | None) -> str:
|
5 |
+
"""Update login status."""
|
6 |
+
if profile is None:
|
7 |
+
return "*Not logged in.*"
|
8 |
+
return f"✅ Logged in as **{profile.username}**"
|
9 |
|
10 |
+
def list_private_models(
|
11 |
+
profile: gr.OAuthProfile | None,
|
12 |
+
oauth_token: gr.OAuthToken | None
|
13 |
+
) -> str:
|
14 |
+
"""Demo: list user’s private/public models."""
|
15 |
+
if oauth_token is None or profile is None:
|
16 |
+
return "Please log in to see your models."
|
17 |
+
models = [
|
18 |
+
f"{model.id} ({'private' if model.private else 'public'})"
|
19 |
+
for model in list_models(author=profile.username, token=oauth_token.token)
|
20 |
+
]
|
21 |
+
if not models:
|
22 |
+
return "No models found."
|
23 |
+
return "Models:\n\n" + "\n - ".join(models)
|
24 |
|
25 |
+
def create_space(
|
26 |
+
repo_name: str,
|
27 |
+
oauth_token: gr.OAuthToken | None
|
28 |
+
) -> tuple[str, str, str]:
|
29 |
+
"""Create (or get) a Hugging Face Space and return its URL/logs/iframe."""
|
30 |
+
if oauth_token is None:
|
31 |
+
return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
|
32 |
+
username = oauth_token.user_info["username"]
|
33 |
+
repo_id = f"{username}/{repo_name}"
|
34 |
+
create_repo(
|
35 |
+
repo_id=repo_id,
|
36 |
+
token=oauth_token.token,
|
37 |
+
exist_ok=True,
|
38 |
+
repo_type="space"
|
39 |
+
)
|
40 |
+
url = f"https://huggingface.co/spaces/{repo_id}"
|
41 |
+
logmsg = f"✅ Space ready: {url}"
|
42 |
+
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
43 |
+
return repo_id, logmsg, iframe
|
44 |
+
|
45 |
+
with gr.Blocks(title="HF OAuth + Space Creator") as demo:
|
46 |
+
gr.Markdown(
|
47 |
+
"## Sign in with Hugging Face + Create a Space\n\n"
|
48 |
+
"1. Click **Sign in**.\n"
|
49 |
+
"2. Enter a name and **Create Space**.\n\n"
|
50 |
+
"---"
|
51 |
+
)
|
52 |
+
|
53 |
+
# — Login UI —
|
54 |
+
login_btn = gr.LoginButton(variant="huggingface", size="lg")
|
55 |
+
status_md = gr.Markdown("*Not logged in.*")
|
56 |
+
models_md = gr.Markdown()
|
57 |
+
|
58 |
+
# Status & model list on load & after login/logout
|
59 |
+
demo.load(show_profile, inputs=None, outputs=status_md)
|
60 |
login_btn.click(show_profile, inputs=None, outputs=status_md)
|
61 |
|
62 |
+
demo.load(list_private_models, inputs=None, outputs=models_md)
|
63 |
+
login_btn.click(list_private_models, inputs=None, outputs=models_md)
|
|
|
|
|
|
|
64 |
|
65 |
+
# — Create Space UI —
|
66 |
+
repo_name = gr.Textbox(label="New Space name", placeholder="my-space-name")
|
67 |
+
create_btn = gr.Button("Create Space", interactive=False)
|
68 |
+
session_id = gr.Textbox(visible=False)
|
69 |
+
logs = gr.Textbox(label="Logs", interactive=False, lines=3)
|
70 |
+
preview_iframe= gr.HTML("<p>No Space created yet.</p>")
|
71 |
|
72 |
+
# Enable the Create button once logged in
|
73 |
+
def enable_create(profile: gr.OAuthProfile | None) -> gr.Update:
|
74 |
+
return gr.update(interactive=profile is not None)
|
75 |
|
76 |
+
demo.load(enable_create, inputs=None, outputs=create_btn)
|
77 |
+
login_btn.click(enable_create, inputs=None, outputs=create_btn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
# Wire up create_space; Gradio injects the token for you
|
80 |
create_btn.click(
|
81 |
fn=create_space,
|
82 |
inputs=[repo_name],
|