zhiminy commited on
Commit
7e91d0c
·
1 Parent(s): 71e4e1f
Files changed (1) hide show
  1. app.py +68 -30
app.py CHANGED
@@ -669,9 +669,24 @@ function() {
669
  # Function to check initial authentication status
670
  def check_auth_on_load(request: gr.Request):
671
  """Check if user is already authenticated when page loads."""
672
- if hasattr(request, 'username') and request.username:
673
- # User is already logged in
674
- token = HfApi().token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675
  return (
676
  gr.update(interactive=True), # repo_url
677
  gr.update(interactive=True), # shared_input
@@ -679,11 +694,13 @@ def check_auth_on_load(request: gr.Request):
679
  gr.update(interactive=True), # feedback
680
  gr.update(interactive=True), # submit_feedback_btn
681
  gr.update(visible=False), # hint_markdown
682
- gr.update(visible=False), # login_button (hide if already logged in)
 
683
  token, # oauth_token
684
  )
685
  else:
686
- # User not logged in - keep defaults
 
687
  return (
688
  gr.update(interactive=False), # repo_url
689
  gr.update(interactive=False), # shared_input
@@ -692,6 +709,7 @@ def check_auth_on_load(request: gr.Request):
692
  gr.update(interactive=False), # submit_feedback_btn
693
  gr.update(visible=True), # hint_markdown
694
  gr.update(visible=True), # login_button
 
695
  None, # oauth_token
696
  )
697
 
@@ -786,9 +804,16 @@ with gr.Blocks(js=clickable_links_js) as app:
786
  if SHOW_HINT_STRING:
787
  markdown_text += f"\n{HINT_STRING}"
788
  hint_markdown = gr.Markdown(markdown_text, elem_classes="markdown-text")
789
- login_button = gr.LoginButton(
790
- "Sign in with Hugging Face", elem_id="oauth-button"
791
- )
 
 
 
 
 
 
 
792
 
793
  guardrail_message = gr.Markdown("", visible=False, elem_id="guardrail-message")
794
 
@@ -1130,22 +1155,29 @@ with gr.Blocks(js=clickable_links_js) as app:
1130
  def hide_thanks_message():
1131
  return gr.update(visible=False)
1132
 
1133
- # Function to handle login - uses gr.Request to get OAuth info
1134
  def handle_login(request: gr.Request):
1135
  """
1136
  Handle user login using Hugging Face OAuth.
1137
  When deployed on HF Spaces with OAuth, request contains user info.
 
1138
  """
 
 
 
 
 
 
 
 
1139
  # Check if user is authenticated through HF Spaces OAuth
1140
- if hasattr(request, 'username') and request.username:
1141
- # User is logged in via HF Spaces OAuth
1142
- # Get the OAuth token from the request headers if available
1143
- token = request.headers.get('authorization', '').replace('Bearer ', '') if hasattr(request, 'headers') else None
1144
 
1145
- # If no token in headers, try to get it from HfApi (in case user logged in via CLI)
1146
- if not token:
1147
- token = HfApi().token
1148
 
 
 
1149
  return (
1150
  gr.update(interactive=True), # repo_url -> Enable
1151
  gr.update(interactive=True), # Enable shared_input
@@ -1153,6 +1185,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1153
  gr.update(interactive=True), # Enable feedback radio buttons
1154
  gr.update(interactive=True), # Enable submit_feedback_btn
1155
  gr.update(visible=False), # Hide the hint string
 
1156
  token, # Store the oauth token
1157
  )
1158
  else:
@@ -1163,23 +1196,27 @@ with gr.Blocks(js=clickable_links_js) as app:
1163
  gr.update(interactive=False), # Keep send_first disabled
1164
  gr.update(interactive=False), # Keep feedback radio buttons disabled
1165
  gr.update(interactive=False), # Keep submit_feedback_btn disabled
1166
- gr.update(visible=True, value="## Please sign in with Hugging Face!\nClick the button above to authenticate. You must be logged into HuggingFace.co for this to work."), # Show instructions
 
1167
  None, # Clear oauth_token
1168
  )
1169
 
1170
- # Handle the login button click
1171
- login_button.click(
1172
- handle_login,
1173
- outputs=[
1174
- repo_url, # Keep this in sync with shared_input
1175
- shared_input, # Enable shared_input
1176
- send_first, # Enable send_first button
1177
- feedback, # Enable feedback radio buttons
1178
- submit_feedback_btn, # Enable submit_feedback_btn
1179
- hint_markdown, # Hide the hint string
1180
- oauth_token, # Store the OAuth token
1181
- ],
1182
- )
 
 
 
1183
 
1184
  # First round handling
1185
  send_first.click(
@@ -1460,6 +1497,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1460
  submit_feedback_btn,
1461
  hint_markdown,
1462
  login_button,
 
1463
  oauth_token,
1464
  ],
1465
  )
 
669
  # Function to check initial authentication status
670
  def check_auth_on_load(request: gr.Request):
671
  """Check if user is already authenticated when page loads."""
672
+ print(f"DEBUG: Checking auth on load")
673
+ print(f"DEBUG: Has username attr: {hasattr(request, 'username')}")
674
+ if hasattr(request, 'username'):
675
+ print(f"DEBUG: Username: {request.username}")
676
+
677
+ # Try to get token from environment (for Spaces) or HfApi (for local)
678
+ token = os.getenv("HF_TOKEN") or HfApi().token
679
+
680
+ # Check if user is authenticated via OAuth
681
+ is_authenticated = (hasattr(request, 'username') and request.username is not None and request.username != "")
682
+
683
+ print(f"DEBUG: Is authenticated: {is_authenticated}")
684
+ print(f"DEBUG: Token available: {token is not None}")
685
+
686
+ if is_authenticated or token:
687
+ # User is logged in OR we have a token available
688
+ username = request.username if hasattr(request, 'username') else "local_user"
689
+ print(f"DEBUG: Enabling interface for user: {username}")
690
  return (
691
  gr.update(interactive=True), # repo_url
692
  gr.update(interactive=True), # shared_input
 
694
  gr.update(interactive=True), # feedback
695
  gr.update(interactive=True), # submit_feedback_btn
696
  gr.update(visible=False), # hint_markdown
697
+ gr.update(visible=False if is_authenticated else True), # login_button
698
+ gr.update(visible=False), # refresh_auth_button (hide when authenticated)
699
  token, # oauth_token
700
  )
701
  else:
702
+ # User not logged in
703
+ print(f"DEBUG: User not authenticated, keeping interface disabled")
704
  return (
705
  gr.update(interactive=False), # repo_url
706
  gr.update(interactive=False), # shared_input
 
709
  gr.update(interactive=False), # submit_feedback_btn
710
  gr.update(visible=True), # hint_markdown
711
  gr.update(visible=True), # login_button
712
+ gr.update(visible=True), # refresh_auth_button (show when not authenticated)
713
  None, # oauth_token
714
  )
715
 
 
804
  if SHOW_HINT_STRING:
805
  markdown_text += f"\n{HINT_STRING}"
806
  hint_markdown = gr.Markdown(markdown_text, elem_classes="markdown-text")
807
+ with gr.Column():
808
+ login_button = gr.LoginButton(
809
+ "Sign in with Hugging Face", elem_id="oauth-button"
810
+ )
811
+ refresh_auth_button = gr.Button(
812
+ "Refresh Login Status",
813
+ variant="secondary",
814
+ size="sm",
815
+ visible=True
816
+ )
817
 
818
  guardrail_message = gr.Markdown("", visible=False, elem_id="guardrail-message")
819
 
 
1155
  def hide_thanks_message():
1156
  return gr.update(visible=False)
1157
 
1158
+ # Function to handle login/refresh - uses gr.Request to get OAuth info
1159
  def handle_login(request: gr.Request):
1160
  """
1161
  Handle user login using Hugging Face OAuth.
1162
  When deployed on HF Spaces with OAuth, request contains user info.
1163
+ This is also used by the refresh button to re-check auth status.
1164
  """
1165
+ print(f"DEBUG: handle_login called")
1166
+ print(f"DEBUG: Has username: {hasattr(request, 'username')}")
1167
+ if hasattr(request, 'username'):
1168
+ print(f"DEBUG: Username in handle_login: {request.username}")
1169
+
1170
+ # Try to get token from environment (for Spaces) or HfApi (for local)
1171
+ token = os.getenv("HF_TOKEN") or HfApi().token
1172
+
1173
  # Check if user is authenticated through HF Spaces OAuth
1174
+ is_authenticated = hasattr(request, 'username') and request.username
 
 
 
1175
 
1176
+ print(f"DEBUG: Is authenticated in handle_login: {is_authenticated}")
1177
+ print(f"DEBUG: Token available in handle_login: {token is not None}")
 
1178
 
1179
+ if is_authenticated or token:
1180
+ # User is logged in
1181
  return (
1182
  gr.update(interactive=True), # repo_url -> Enable
1183
  gr.update(interactive=True), # Enable shared_input
 
1185
  gr.update(interactive=True), # Enable feedback radio buttons
1186
  gr.update(interactive=True), # Enable submit_feedback_btn
1187
  gr.update(visible=False), # Hide the hint string
1188
+ gr.update(visible=False), # Hide refresh button when authenticated
1189
  token, # Store the oauth token
1190
  )
1191
  else:
 
1196
  gr.update(interactive=False), # Keep send_first disabled
1197
  gr.update(interactive=False), # Keep feedback radio buttons disabled
1198
  gr.update(interactive=False), # Keep submit_feedback_btn disabled
1199
+ gr.update(visible=True, value="## Please sign in with Hugging Face!\nClick the 'Sign in with Hugging Face' button above, then click 'Refresh Login Status' after you return from the auth page."), # Show instructions
1200
+ gr.update(visible=True), # Show refresh button
1201
  None, # Clear oauth_token
1202
  )
1203
 
1204
+ # Handle the login button click and refresh button click
1205
+ # Both use the same handler to check auth status
1206
+ for button in [login_button, refresh_auth_button]:
1207
+ button.click(
1208
+ handle_login,
1209
+ outputs=[
1210
+ repo_url, # Keep this in sync with shared_input
1211
+ shared_input, # Enable shared_input
1212
+ send_first, # Enable send_first button
1213
+ feedback, # Enable feedback radio buttons
1214
+ submit_feedback_btn, # Enable submit_feedback_btn
1215
+ hint_markdown, # Hide the hint string
1216
+ refresh_auth_button, # Control refresh button visibility
1217
+ oauth_token, # Store the OAuth token
1218
+ ],
1219
+ )
1220
 
1221
  # First round handling
1222
  send_first.click(
 
1497
  submit_feedback_btn,
1498
  hint_markdown,
1499
  login_button,
1500
+ refresh_auth_button,
1501
  oauth_token,
1502
  ],
1503
  )