sanjanb commited on
Commit
d84b32e
Β·
verified Β·
1 Parent(s): 308fbf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -39
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"our unique Hugging Face ID is:\n`{profile.sub}`"
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
- logout_btn = gr.Button("Log Out", visible=False)
23
-
24
- welcome_text = gr.Markdown("Checking login status...", elem_id="welcome")
25
-
26
- # πŸ‘‡ Automatically greet the user on load
27
- demo.load(greet_user, inputs=None, outputs=welcome_text)
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
- if (loginBtn) loginBtn.style.display = "none";
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()