zhiminy commited on
Commit
3d3b2ac
·
1 Parent(s): f48a779
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -307,7 +307,7 @@ def format_conversation_history(conversation_history):
307
  return formatted_text
308
 
309
 
310
- def save_content_to_hf(vote_data, repo_name, folder_name, file_name):
311
  """
312
  Save feedback content to Hugging Face repository.
313
  """
@@ -321,9 +321,10 @@ def save_content_to_hf(vote_data, repo_name, folder_name, file_name):
321
  filename = f"{folder_name}/{file_name}.json"
322
 
323
  # Ensure the user is authenticated with HF
324
- token = HfApi().token
325
  if token is None:
326
- raise ValueError("Please log in to Hugging Face using `huggingface-cli login`.")
 
 
327
 
328
  # Upload to Hugging Face repository
329
  upload_file(
@@ -671,6 +672,9 @@ with gr.Blocks(js=clickable_links_js) as app:
671
  models_state = gr.State({})
672
  conversation_state = gr.State({})
673
 
 
 
 
674
  with gr.Tab("🏆Leaderboard"):
675
  # Add title and description as a Markdown component
676
  leaderboard_intro = gr.Markdown(
@@ -752,7 +756,7 @@ with gr.Blocks(js=clickable_links_js) as app:
752
  if SHOW_HINT_STRING:
753
  markdown_text += f"\n{HINT_STRING}"
754
  hint_markdown = gr.Markdown(markdown_text, elem_classes="markdown-text")
755
- login_button = gr.Button(
756
  "Sign in with Hugging Face", elem_id="oauth-button"
757
  )
758
 
@@ -1097,20 +1101,30 @@ with gr.Blocks(js=clickable_links_js) as app:
1097
  return gr.update(visible=False)
1098
 
1099
  # Function to handle login
1100
- def handle_login():
1101
  """
1102
- Handle user login using Hugging Face OAuth with automatic redirection.
 
1103
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
1104
  try:
1105
- # Use Hugging Face OAuth to initiate login
1106
- HfApi()
1107
- token = HfApi().token
1108
- if not token:
1109
- raise Exception("Authentication token not found.")
1110
 
1111
  # If token is successfully retrieved, update the interface state
1112
  return (
1113
- gr.update(visible=False), # Hide the login button
1114
  gr.update(interactive=True), # repo_url -> Enable in sync
1115
  gr.update(interactive=True), # Enable shared_input
1116
  gr.update(
@@ -1119,12 +1133,12 @@ with gr.Blocks(js=clickable_links_js) as app:
1119
  gr.update(interactive=True), # Enable feedback radio buttons
1120
  gr.update(interactive=True), # Enable submit_feedback_btn
1121
  gr.update(visible=False), # Hide the hint string
 
1122
  )
1123
  except Exception as e:
1124
  # Handle login failure
1125
- print(f"Login failed: {e}")
1126
  return (
1127
- gr.update(visible=True), # Keep the login button visible
1128
  gr.update(interactive=False), # repo_url -> disable if login failed
1129
  gr.update(interactive=False), # Keep shared_input disabled
1130
  gr.update(interactive=False), # Keep send_first disabled
@@ -1133,20 +1147,20 @@ with gr.Blocks(js=clickable_links_js) as app:
1133
  ), # Keep feedback radio buttons disabled
1134
  gr.update(interactive=False), # Keep submit_feedback_btn disabled
1135
  gr.update(visible=True), # Show the hint string
 
1136
  )
1137
 
1138
- # Handle the login button click
1139
- login_button.click(
1140
  handle_login,
1141
- inputs=[],
1142
  outputs=[
1143
- login_button, # Hide the login button after successful login
1144
  repo_url, # Keep this in sync with shared_input
1145
  shared_input, # Enable shared_input
1146
  send_first, # Enable send_first button
1147
  feedback, # Enable feedback radio buttons
1148
  submit_feedback_btn, # Enable submit_feedback_btn
1149
  hint_markdown, # Hide the hint string
 
1150
  ],
1151
  )
1152
 
@@ -1301,7 +1315,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1301
  ],
1302
  )
1303
 
1304
- def submit_feedback(vote, models_state, conversation_state):
1305
  # Map vote to actual model names
1306
  match vote:
1307
  case "Model A":
@@ -1327,7 +1341,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1327
 
1328
  # Save feedback back to the Hugging Face dataset
1329
  save_content_to_hf(
1330
- vote_entry, "SWE-Arena/model_votes", folder_name, file_name
1331
  )
1332
 
1333
  conversation_state["right_chat"][0]["content"] = conversation_state[
@@ -1343,6 +1357,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1343
  "SWE-Arena/model_conversations",
1344
  folder_name,
1345
  file_name,
 
1346
  )
1347
 
1348
  # Clear state
@@ -1385,7 +1400,7 @@ with gr.Blocks(js=clickable_links_js) as app:
1385
  # Update the click event for the submit feedback button
1386
  submit_feedback_btn.click(
1387
  submit_feedback,
1388
- inputs=[feedback, models_state, conversation_state],
1389
  outputs=[
1390
  shared_input, # Reset shared_input
1391
  repo_url, # Show the repo-related URL message
 
307
  return formatted_text
308
 
309
 
310
+ def save_content_to_hf(vote_data, repo_name, folder_name, file_name, token=None):
311
  """
312
  Save feedback content to Hugging Face repository.
313
  """
 
321
  filename = f"{folder_name}/{file_name}.json"
322
 
323
  # Ensure the user is authenticated with HF
 
324
  if token is None:
325
+ token = HfApi().token
326
+ if token is None:
327
+ raise ValueError("Please log in to Hugging Face to submit votes.")
328
 
329
  # Upload to Hugging Face repository
330
  upload_file(
 
672
  models_state = gr.State({})
673
  conversation_state = gr.State({})
674
 
675
+ # Add OAuth information state to track user
676
+ oauth_token = gr.State(None)
677
+
678
  with gr.Tab("🏆Leaderboard"):
679
  # Add title and description as a Markdown component
680
  leaderboard_intro = gr.Markdown(
 
756
  if SHOW_HINT_STRING:
757
  markdown_text += f"\n{HINT_STRING}"
758
  hint_markdown = gr.Markdown(markdown_text, elem_classes="markdown-text")
759
+ login_button = gr.LoginButton(
760
  "Sign in with Hugging Face", elem_id="oauth-button"
761
  )
762
 
 
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(
 
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
 
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
1158
  shared_input, # Enable shared_input
1159
  send_first, # Enable send_first button
1160
  feedback, # Enable feedback radio buttons
1161
  submit_feedback_btn, # Enable submit_feedback_btn
1162
  hint_markdown, # Hide the hint string
1163
+ oauth_token, # Store the OAuth token
1164
  ],
1165
  )
1166
 
 
1315
  ],
1316
  )
1317
 
1318
+ def submit_feedback(vote, models_state, conversation_state, token):
1319
  # Map vote to actual model names
1320
  match vote:
1321
  case "Model A":
 
1341
 
1342
  # Save feedback back to the Hugging Face dataset
1343
  save_content_to_hf(
1344
+ vote_entry, "SWE-Arena/model_votes", folder_name, file_name, token
1345
  )
1346
 
1347
  conversation_state["right_chat"][0]["content"] = conversation_state[
 
1357
  "SWE-Arena/model_conversations",
1358
  folder_name,
1359
  file_name,
1360
+ token,
1361
  )
1362
 
1363
  # Clear state
 
1400
  # Update the click event for the submit feedback button
1401
  submit_feedback_btn.click(
1402
  submit_feedback,
1403
+ inputs=[feedback, models_state, conversation_state, oauth_token],
1404
  outputs=[
1405
  shared_input, # Reset shared_input
1406
  repo_url, # Show the repo-related URL message