Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
# π§ Step 1: Greeting function
|
| 4 |
def greet_user(profile: gr.OAuthProfile | None):
|
| 5 |
if profile is None:
|
| 6 |
-
return
|
| 7 |
-
"You are not logged in yet.\n\n"
|
| 8 |
-
"Please use the **Sign In** button above."
|
| 9 |
-
)
|
| 10 |
return (
|
| 11 |
-
f"Welcome, **{profile.name}**!\n\n"
|
| 12 |
-
f"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
# π§ Step 2: UI Structure
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
-
gr.Markdown("## Hugging Face OAuth Login Demo", elem_id="title")
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
login_btn = gr.LoginButton()
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# π Logic when user logs in
|
| 30 |
-
login_btn.click(fn=greet_user, inputs=login_btn, outputs=welcome_text)
|
| 31 |
-
|
| 32 |
-
# π Simple logout (reload)
|
| 33 |
-
logout_btn.click(fn=None, inputs=None, outputs=None, _js="() => location.reload()")
|
| 34 |
-
|
| 35 |
-
# π JavaScript to show/hide buttons based on login
|
| 36 |
-
demo.load(
|
| 37 |
-
None,
|
| 38 |
-
inputs=None,
|
| 39 |
-
outputs=None,
|
| 40 |
-
_js="""
|
| 41 |
-
() => {
|
| 42 |
-
const token = window.localStorage.getItem('hf_oauth_access_token');
|
| 43 |
-
const loginBtn = document.querySelector('[data-testid="login-button"]');
|
| 44 |
-
const logoutBtn = document.querySelector('button:contains("Log Out")');
|
| 45 |
-
|
| 46 |
if (token) {
|
| 47 |
-
|
| 48 |
-
if (logoutBtn) logoutBtn.style.display = "inline-block";
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
|
|
|
| 3 |
def greet_user(profile: gr.OAuthProfile | None):
|
| 4 |
if profile is None:
|
| 5 |
+
return "π You are not logged in yet.\n\nPlease use the **Sign In** button above."
|
|
|
|
|
|
|
|
|
|
| 6 |
return (
|
| 7 |
+
f"π Welcome, **{profile.name}**!\n\n"
|
| 8 |
+
f"π Your unique Hugging Face ID is:\n`{profile.sub}`"
|
| 9 |
)
|
| 10 |
|
|
|
|
|
|
|
| 11 |
with gr.Blocks() as demo:
|
| 12 |
+
gr.Markdown("## π Hugging Face OAuth Login Demo", elem_id="title")
|
| 13 |
|
| 14 |
with gr.Row():
|
| 15 |
login_btn = gr.LoginButton()
|
| 16 |
+
logout_html = gr.HTML(
|
| 17 |
+
"""
|
| 18 |
+
<button id='logoutBtn' style="display:none;padding:8px 16px;background:#e74c3c;color:#fff;border:none;border-radius:6px;cursor:pointer;">
|
| 19 |
+
πͺ Log Out
|
| 20 |
+
</button>
|
| 21 |
+
<script>
|
| 22 |
+
const token = localStorage.getItem("hf_oauth_access_token");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
if (token) {
|
| 24 |
+
document.getElementById("logoutBtn").style.display = "inline-block";
|
|
|
|
| 25 |
}
|
| 26 |
+
document.getElementById("logoutBtn").onclick = () => {
|
| 27 |
+
localStorage.removeItem("hf_oauth_access_token");
|
| 28 |
+
location.reload();
|
| 29 |
+
};
|
| 30 |
+
</script>
|
| 31 |
+
"""
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
output = gr.Markdown("Checking login status...")
|
| 35 |
+
|
| 36 |
+
demo.load(fn=greet_user, inputs=None, outputs=output)
|
| 37 |
|
| 38 |
demo.launch()
|