Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,33 +12,37 @@ def list_private_models(
|
|
| 12 |
oauth_token: gr.OAuthToken | None
|
| 13 |
) -> str:
|
| 14 |
"""Demo: list user’s private/public models."""
|
| 15 |
-
if
|
| 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 |
-
|
| 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 |
-
"""
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
|
| 32 |
-
|
| 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 |
|
|
@@ -46,40 +50,41 @@ 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.
|
| 50 |
"---"
|
| 51 |
)
|
| 52 |
|
| 53 |
# — Login UI —
|
| 54 |
-
login_btn
|
| 55 |
-
status_md
|
| 56 |
-
models_md
|
| 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(
|
| 63 |
-
login_btn.click(
|
|
|
|
|
|
|
| 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):
|
| 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],
|
| 83 |
outputs=[session_id, logs, preview_iframe]
|
| 84 |
)
|
| 85 |
|
|
|
|
| 12 |
oauth_token: gr.OAuthToken | None
|
| 13 |
) -> str:
|
| 14 |
"""Demo: list user’s private/public models."""
|
| 15 |
+
if profile is None or oauth_token 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 |
+
return "No models found." if not models \
|
| 22 |
+
else "Models:\n\n" + "\n - ".join(models)
|
|
|
|
| 23 |
|
| 24 |
def create_space(
|
| 25 |
repo_name: str,
|
| 26 |
+
sdk: str,
|
| 27 |
+
profile: gr.OAuthProfile | None,
|
| 28 |
oauth_token: gr.OAuthToken | None
|
| 29 |
) -> tuple[str, str, str]:
|
| 30 |
+
"""
|
| 31 |
+
Create (or get) a Hugging Face Space with the chosen SDK template,
|
| 32 |
+
returning (repo_id, logs, iframe).
|
| 33 |
+
"""
|
| 34 |
+
if profile is None or oauth_token is None:
|
| 35 |
return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
|
| 36 |
+
repo_id = f"{profile.username}/{repo_name}"
|
|
|
|
| 37 |
create_repo(
|
| 38 |
repo_id=repo_id,
|
| 39 |
token=oauth_token.token,
|
| 40 |
exist_ok=True,
|
| 41 |
+
repo_type="space",
|
| 42 |
+
space_sdk=sdk
|
| 43 |
)
|
| 44 |
url = f"https://huggingface.co/spaces/{repo_id}"
|
| 45 |
+
logmsg = f"✅ Space ready: {url} (SDK: {sdk})"
|
| 46 |
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
| 47 |
return repo_id, logmsg, iframe
|
| 48 |
|
|
|
|
| 50 |
gr.Markdown(
|
| 51 |
"## Sign in with Hugging Face + Create a Space\n\n"
|
| 52 |
"1. Click **Sign in**.\n"
|
| 53 |
+
"2. Pick a template, enter a name, and **Create Space**.\n\n"
|
| 54 |
"---"
|
| 55 |
)
|
| 56 |
|
| 57 |
# — Login UI —
|
| 58 |
+
login_btn = gr.LoginButton(variant="huggingface", size="lg")
|
| 59 |
+
status_md = gr.Markdown("*Not logged in.*")
|
| 60 |
+
models_md = gr.Markdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
demo.load(show_profile, inputs=None, outputs=status_md)
|
| 63 |
+
login_btn.click(show_profile, inputs=None, outputs=status_md)
|
| 64 |
+
demo.load(list_private_models, inputs=None, outputs=models_md)
|
| 65 |
+
login_btn.click(list_private_models,inputs=None, outputs=models_md)
|
| 66 |
|
| 67 |
# — Create Space UI —
|
| 68 |
repo_name = gr.Textbox(label="New Space name", placeholder="my-space-name")
|
| 69 |
+
sdk_selector = gr.Radio(
|
| 70 |
+
choices=["gradio", "streamlit"],
|
| 71 |
+
value="gradio",
|
| 72 |
+
label="Space template (SDK)"
|
| 73 |
+
)
|
| 74 |
create_btn = gr.Button("Create Space", interactive=False)
|
| 75 |
session_id = gr.Textbox(visible=False)
|
| 76 |
logs = gr.Textbox(label="Logs", interactive=False, lines=3)
|
| 77 |
preview_iframe = gr.HTML("<p>No Space created yet.</p>")
|
| 78 |
|
|
|
|
| 79 |
def enable_create(profile: gr.OAuthProfile | None):
|
| 80 |
return gr.update(interactive=profile is not None)
|
| 81 |
|
| 82 |
demo.load(enable_create, inputs=None, outputs=create_btn)
|
| 83 |
login_btn.click(enable_create, inputs=None, outputs=create_btn)
|
| 84 |
|
|
|
|
| 85 |
create_btn.click(
|
| 86 |
fn=create_space,
|
| 87 |
+
inputs=[repo_name, sdk_selector],
|
| 88 |
outputs=[session_id, logs, preview_iframe]
|
| 89 |
)
|
| 90 |
|