zhiminy commited on
Commit
71e4e1f
·
1 Parent(s): 3d3b2ac
Files changed (1) hide show
  1. app.py +66 -34
app.py CHANGED
@@ -666,6 +666,36 @@ function() {
666
  """
667
  )
668
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
669
  # Gradio Interface
670
  with gr.Blocks(js=clickable_links_js) as app:
671
  user_authenticated = gr.State(False)
@@ -1100,58 +1130,45 @@ with gr.Blocks(js=clickable_links_js) as app:
1100
  def hide_thanks_message():
1101
  return gr.update(visible=False)
1102
 
1103
- # Function to handle login
1104
- def handle_login(profile: gr.OAuthProfile | None):
1105
  """
1106
  Handle user login using Hugging Face OAuth.
1107
- The LoginButton automatically provides the profile when user logs in.
1108
  """
1109
- if profile is None:
1110
- # User is not logged in
1111
- return (
1112
- gr.update(interactive=False), # repo_url -> disable if not logged in
1113
- gr.update(interactive=False), # Keep shared_input disabled
1114
- gr.update(interactive=False), # Keep send_first disabled
1115
- gr.update(interactive=False), # Keep feedback radio buttons disabled
1116
- gr.update(interactive=False), # Keep submit_feedback_btn disabled
1117
- gr.update(visible=True), # Show the hint string
1118
- None, # Clear oauth_token
1119
- )
1120
 
1121
- # User is logged in - profile contains oauth_info with access token
1122
- try:
1123
- # Store the OAuth token for later use
1124
- token = profile.oauth_info.get("access_token") if profile.oauth_info else None
1125
 
1126
- # If token is successfully retrieved, update the interface state
1127
  return (
1128
- gr.update(interactive=True), # repo_url -> Enable in sync
1129
  gr.update(interactive=True), # Enable shared_input
1130
- gr.update(
1131
- interactive=False
1132
- ), # Keep send_first button disabled initially
1133
  gr.update(interactive=True), # Enable feedback radio buttons
1134
  gr.update(interactive=True), # Enable submit_feedback_btn
1135
  gr.update(visible=False), # Hide the hint string
1136
  token, # Store the oauth token
1137
  )
1138
- except Exception as e:
1139
- # Handle login failure
1140
- print(f"Login processing failed: {e}")
1141
  return (
1142
- gr.update(interactive=False), # repo_url -> disable if login failed
1143
  gr.update(interactive=False), # Keep shared_input disabled
1144
  gr.update(interactive=False), # Keep send_first disabled
1145
- gr.update(
1146
- interactive=False
1147
- ), # Keep feedback radio buttons disabled
1148
  gr.update(interactive=False), # Keep submit_feedback_btn disabled
1149
- gr.update(visible=True), # Show the hint string
1150
  None, # Clear oauth_token
1151
  )
1152
 
1153
- # Handle the login button - LoginButton automatically passes profile
1154
- login_button.login(
1155
  handle_login,
1156
  outputs=[
1157
  repo_url, # Keep this in sync with shared_input
@@ -1424,7 +1441,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1424
  ## Terms of Service
1425
 
1426
  Users are required to agree to the following terms before using the service:
1427
-
1428
  - The service is a **research preview**. It only provides limited safety measures and may generate offensive content.
1429
  - It must not be used for any **illegal, harmful, violent, racist, or sexual** purposes.
1430
  - Please do not upload any **private** information.
@@ -1432,4 +1449,19 @@ with gr.Blocks(js=clickable_links_js) as app:
1432
  """
1433
  )
1434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1435
  app.launch()
 
666
  """
667
  )
668
 
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
678
+ gr.update(interactive=False), # send_first (disabled until text entered)
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
690
+ gr.update(interactive=False), # send_first
691
+ gr.update(interactive=False), # feedback
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
+
698
+
699
  # Gradio Interface
700
  with gr.Blocks(js=clickable_links_js) as app:
701
  user_authenticated = gr.State(False)
 
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
1152
+ gr.update(interactive=False), # Keep send_first disabled initially
 
 
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:
1159
+ # User is not logged in - instruct them to use HF login
 
1160
  return (
1161
+ gr.update(interactive=False), # repo_url -> disable
1162
  gr.update(interactive=False), # Keep shared_input disabled
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
 
1441
  ## Terms of Service
1442
 
1443
  Users are required to agree to the following terms before using the service:
1444
+
1445
  - The service is a **research preview**. It only provides limited safety measures and may generate offensive content.
1446
  - It must not be used for any **illegal, harmful, violent, racist, or sexual** purposes.
1447
  - Please do not upload any **private** information.
 
1449
  """
1450
  )
1451
 
1452
+ # Check authentication status when the app loads
1453
+ app.load(
1454
+ check_auth_on_load,
1455
+ outputs=[
1456
+ repo_url,
1457
+ shared_input,
1458
+ send_first,
1459
+ feedback,
1460
+ submit_feedback_btn,
1461
+ hint_markdown,
1462
+ login_button,
1463
+ oauth_token,
1464
+ ],
1465
+ )
1466
+
1467
  app.launch()