tomerz-aai commited on
Commit
9203d52
·
1 Parent(s): 6c4809a
Files changed (2) hide show
  1. README.md +1 -2
  2. app.py +9 -88
README.md CHANGED
@@ -5,12 +5,11 @@ colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
  hf_oauth: true
8
- hf_oauth_expiration_minutes: 2
9
  app_file: app.py
10
  pinned: true
11
  license: apache-2.0
12
  short_description: FormulaOne Leaderboard
13
- sdk_version: 5.19.0
14
  ---
15
 
16
  # Start the configuration
 
5
  colorTo: indigo
6
  sdk: gradio
7
  hf_oauth: true
 
8
  app_file: app.py
9
  pinned: true
10
  license: apache-2.0
11
  short_description: FormulaOne Leaderboard
12
+ sdk_version: 5.42.0
13
  ---
14
 
15
  # Start the configuration
app.py CHANGED
@@ -1,12 +1,9 @@
1
- from http.cookies import SimpleCookie
2
- import os
3
  import gradio as gr
4
  import pandas as pd
5
  from apscheduler.schedulers.background import BackgroundScheduler
6
  from gradio.themes import Base, colors, sizes
7
  from gradio_leaderboard import Leaderboard, SelectColumns
8
  from huggingface_hub import whoami
9
- from fastapi import Response
10
 
11
  from src.about import CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, EVALUATION_QUEUE_TEXT, INTRODUCTION_TEXT, TITLE
12
  from src.datamodel.data import F1Data
@@ -87,13 +84,13 @@ def add_solution_cbk(
87
  sys_type: str,
88
  submission_path: str,
89
  profile: gr.OAuthProfile | None,
90
- token: gr.OAuthToken | None,
91
  ):
92
  logger.info("Fetching user details for submission")
93
  logger.info("PROFILE %s", profile)
94
- logger.info("TOKEN %s", token)
95
 
96
- if profile is None or token is None:
97
  return styled_error("Please sign in with Hugging Face before submitting.")
98
 
99
  # Display handle and display name (may change over time)
@@ -102,7 +99,7 @@ def add_solution_cbk(
102
  logger.info(f"Display name: {display_name}")
103
 
104
  # Stable account id
105
- user_info = fetch_user_info(token)
106
  logger.info("Logged in user info: %s", user_info)
107
  stable_id = user_info.get("id") if user_info else None
108
  logger.info(f"User stable ID: {stable_id}")
@@ -160,66 +157,21 @@ def add_solution_cbk(
160
  )
161
 
162
 
163
- # def check_valid_token(oauth_token: gr.OAuthToken | None):
164
- # logger.info("CHECK TOKEN %s", oauth_token)
165
- # if oauth_token is None:
166
- # logger.info("CHECK: NO TOKEN")
167
- # return gr.update(value="")
168
- # try:
169
- # whoami(oauth_token.token)
170
- # logger.info("CHECK: VALID TOKEN")
171
- # return gr.update(value="")
172
- # except Exception:
173
- # logger.info("CHECK: TOKEN HAS EXPIRED")
174
- # return gr.update(value='<script>window.location.href = "/logout";</script>')
175
-
176
-
177
- # def gate_submission_by_prof(profile: gr.OAuthProfile | None):
178
- # if not profile:
179
- # return gr.update(visible=True), gr.update(visible=False)
180
- # return gr.update(visible=False), gr.update(visible=True)
181
-
182
-
183
- def gate_submission(oauth_token: gr.OAuthToken | None, request: gr.Request):
184
  """
185
  @brief Toggles the visibility of the login box and submission panel based on the user's login status.
186
  """
187
- # Log cookie sizes
188
- cookies = request.headers.get("cookie", "")
189
- cookie_obj = SimpleCookie()
190
- try:
191
- cookie_obj.load(cookies)
192
- for key in ["spaces-jwt", "session"]:
193
- if key in cookie_obj:
194
- cookie_size = len(cookie_obj[key].OutputString().encode('utf-8'))
195
- logger.info(f"Cookie {key} size: {cookie_size} bytes")
196
- except Exception as e:
197
- logger.error(f"Error parsing cookies: {str(e)}")
198
-
199
  logger.info("GATE TOKEN %s", oauth_token)
200
  if oauth_token is None:
201
  logger.info("GATE: NO TOKEN")
202
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
203
  try:
204
- token_size = len(oauth_token.token.encode('utf-8'))
205
- logger.info(f"Token size: {token_size} bytes")
206
  whoami(oauth_token.token)
207
  logger.info("GATE: TOKEN IS VALID")
208
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
209
  except Exception:
210
  logger.info("GATE: TOKEN HAS EXPIRED")
211
- return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
212
-
213
- # Custom route to force clear HttpOnly cookies
214
- def clear_cookies():
215
- response = Response()
216
- response.headers["Set-Cookie"] = [
217
- "spaces-jwt=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=" + os.environ.get("SPACE_HOST", ""),
218
- "session=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=" + os.environ.get("SPACE_HOST", "")
219
- ]
220
- response.headers["Location"] = "/?t=" + str(int(os.times().elapsed * 1000))
221
- response.status_code = 302
222
- return response
223
 
224
 
225
  def get_theme():
@@ -242,17 +194,6 @@ def get_theme():
242
  )
243
  return cyber_theme
244
 
245
- # JavaScript to clear cookies and reload
246
- JS_LOGOUT = """
247
- <a href="#" onclick="
248
- var domain = window.location.hostname;
249
- document.cookie = 'spaces-jwt=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=' + domain;
250
- document.cookie = 'session=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=' + domain;
251
- console.log('Cookies cleared: spaces-jwt, session on domain: ' + domain);
252
- window.location.href='/?t='+new Date().getTime();
253
- return false;
254
- " style="display: inline-block; padding: 10px; background: #ff4d4f; color: white; text-decoration: none; border-radius: 5px;">Manual Logout</a>
255
- """
256
 
257
  blocks = gr.Blocks(css=custom_css, theme=get_theme())
258
  with blocks:
@@ -311,20 +252,6 @@ with blocks:
311
  gr.Markdown("Please sign in to continue:")
312
  gr.LoginButton()
313
 
314
- # Shown when login token is EXPIRED
315
- logout_box = gr.Group(visible=False)
316
- with logout_box:
317
- gr.Markdown("Your session has already EXPIRED. Please sign in again")
318
- gr.LoginButton()
319
- # gr.Button("Manual Logout", link="/logout")
320
- # gr.HTML(JS_LOGOUT)
321
- # Manual logout link to server-side /logout
322
- # gr.HTML('<a href="/logout?redirect=/" style="display: inline-block; padding: 10px; background: #ff4d4f; color: white; text-decoration: none; border-radius: 5px;">Logout (via /logout)</a>')
323
-
324
- # Custom clear cookies link
325
- # gr.HTML('<a href="/clear-cookies" style="display: inline-block; padding: 10px; background: #ff6666; color: white; text-decoration: none; border-radius: 5px; margin-left: 10px;">Clear Cookies</a>')
326
-
327
-
328
  # Shown when logged IN
329
  submit_panel = gr.Group(visible=False)
330
  with submit_panel:
@@ -366,18 +293,12 @@ with blocks:
366
  elem_id="citation-block",
367
  )
368
 
369
- # Trigger reload if token is expired
370
- # blocks.load(check_valid_token, outputs=reload_html)
371
-
372
  # UI refresh triggers latest data swap.
373
  # The work already happened in the background - refresh_leaderboard_data().
374
  blocks.load(lambda: leaderboard_df, inputs=[], outputs=[leaderboard_component])
375
 
376
  # On initial load (and after OAuth redirect), toggle the UI based on login status.
377
- # blocks.load(gate_submission, inputs=None, outputs=[login_box, submit_panel])
378
- blocks.load(gate_submission, inputs=None, outputs=[login_box, logout_box, submit_panel])
379
-
380
- # blocks.app.add_api_route("/clear-cookies", clear_cookies, methods=["GET"])
381
 
382
 
383
  logger.info("Scheduler")
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  from apscheduler.schedulers.background import BackgroundScheduler
4
  from gradio.themes import Base, colors, sizes
5
  from gradio_leaderboard import Leaderboard, SelectColumns
6
  from huggingface_hub import whoami
 
7
 
8
  from src.about import CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, EVALUATION_QUEUE_TEXT, INTRODUCTION_TEXT, TITLE
9
  from src.datamodel.data import F1Data
 
84
  sys_type: str,
85
  submission_path: str,
86
  profile: gr.OAuthProfile | None,
87
+ oauth_token: gr.OAuthToken | None,
88
  ):
89
  logger.info("Fetching user details for submission")
90
  logger.info("PROFILE %s", profile)
91
+ logger.info("TOKEN %s", oauth_token)
92
 
93
+ if profile is None or oauth_token is None:
94
  return styled_error("Please sign in with Hugging Face before submitting.")
95
 
96
  # Display handle and display name (may change over time)
 
99
  logger.info(f"Display name: {display_name}")
100
 
101
  # Stable account id
102
+ user_info = fetch_user_info(oauth_token)
103
  logger.info("Logged in user info: %s", user_info)
104
  stable_id = user_info.get("id") if user_info else None
105
  logger.info(f"User stable ID: {stable_id}")
 
157
  )
158
 
159
 
160
+ def gate_submission(oauth_token: gr.OAuthToken | None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  """
162
  @brief Toggles the visibility of the login box and submission panel based on the user's login status.
163
  """
 
 
 
 
 
 
 
 
 
 
 
 
164
  logger.info("GATE TOKEN %s", oauth_token)
165
  if oauth_token is None:
166
  logger.info("GATE: NO TOKEN")
167
+ return gr.update(visible=True), gr.update(visible=False)
168
  try:
 
 
169
  whoami(oauth_token.token)
170
  logger.info("GATE: TOKEN IS VALID")
171
+ return gr.update(visible=False), gr.update(visible=True)
172
  except Exception:
173
  logger.info("GATE: TOKEN HAS EXPIRED")
174
+ return gr.update(visible=True), gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
175
 
176
 
177
  def get_theme():
 
194
  )
195
  return cyber_theme
196
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  blocks = gr.Blocks(css=custom_css, theme=get_theme())
199
  with blocks:
 
252
  gr.Markdown("Please sign in to continue:")
253
  gr.LoginButton()
254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
  # Shown when logged IN
256
  submit_panel = gr.Group(visible=False)
257
  with submit_panel:
 
293
  elem_id="citation-block",
294
  )
295
 
 
 
 
296
  # UI refresh triggers latest data swap.
297
  # The work already happened in the background - refresh_leaderboard_data().
298
  blocks.load(lambda: leaderboard_df, inputs=[], outputs=[leaderboard_component])
299
 
300
  # On initial load (and after OAuth redirect), toggle the UI based on login status.
301
+ blocks.load(gate_submission, inputs=None, outputs=[login_box, submit_panel])
 
 
 
302
 
303
 
304
  logger.info("Scheduler")