Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
83ecd5b
1
Parent(s):
33b98fb
wip
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from apscheduler.schedulers.background import BackgroundScheduler
|
@@ -177,15 +178,29 @@ def add_solution_cbk(
|
|
177 |
# return gr.update(visible=False), gr.update(visible=True)
|
178 |
|
179 |
|
180 |
-
def gate_submission(oauth_token: gr.OAuthToken | None):
|
181 |
"""
|
182 |
@brief Toggles the visibility of the login box and submission panel based on the user's login status.
|
183 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
logger.info("GATE TOKEN %s", oauth_token)
|
185 |
if oauth_token is None:
|
186 |
logger.info("GATE: NO TOKEN")
|
187 |
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
188 |
try:
|
|
|
|
|
189 |
whoami(oauth_token.token)
|
190 |
logger.info("GATE: TOKEN IS VALID")
|
191 |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
@@ -193,6 +208,16 @@ def gate_submission(oauth_token: gr.OAuthToken | None):
|
|
193 |
logger.info("GATE: TOKEN HAS EXPIRED")
|
194 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
def get_theme():
|
198 |
cyber_theme = Base(
|
@@ -288,7 +313,13 @@ with blocks:
|
|
288 |
with logout_box:
|
289 |
gr.Markdown("Your session has EXPIRED. Please sign in again")
|
290 |
# gr.Button("Manual Logout", link="/logout")
|
291 |
-
gr.HTML(JS_LOGOUT)
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
# Shown when logged IN
|
294 |
submit_panel = gr.Group(visible=False)
|
@@ -342,6 +373,8 @@ with blocks:
|
|
342 |
# blocks.load(gate_submission, inputs=None, outputs=[login_box, submit_panel])
|
343 |
blocks.load(gate_submission, inputs=None, outputs=[login_box, logout_box, submit_panel])
|
344 |
|
|
|
|
|
345 |
|
346 |
logger.info("Scheduler")
|
347 |
scheduler = BackgroundScheduler()
|
|
|
1 |
+
from http.cookies import SimpleCookie
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
178 |
# return gr.update(visible=False), gr.update(visible=True)
|
179 |
|
180 |
|
181 |
+
def gate_submission(oauth_token: gr.OAuthToken | None, request: gr.Request):
|
182 |
"""
|
183 |
@brief Toggles the visibility of the login box and submission panel based on the user's login status.
|
184 |
"""
|
185 |
+
# Log cookie sizes
|
186 |
+
cookies = request.headers.get("cookie", "")
|
187 |
+
cookie_obj = SimpleCookie()
|
188 |
+
try:
|
189 |
+
cookie_obj.load(cookies)
|
190 |
+
for key in ["spaces-jwt", "session"]:
|
191 |
+
if key in cookie_obj:
|
192 |
+
cookie_size = len(cookie_obj[key].OutputString().encode('utf-8'))
|
193 |
+
logger.info(f"Cookie {key} size: {cookie_size} bytes")
|
194 |
+
except Exception as e:
|
195 |
+
logger.error(f"Error parsing cookies: {str(e)}")
|
196 |
+
|
197 |
logger.info("GATE TOKEN %s", oauth_token)
|
198 |
if oauth_token is None:
|
199 |
logger.info("GATE: NO TOKEN")
|
200 |
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
201 |
try:
|
202 |
+
token_size = len(oauth_token.token.encode('utf-8'))
|
203 |
+
logger.info(f"Token size: {token_size} bytes")
|
204 |
whoami(oauth_token.token)
|
205 |
logger.info("GATE: TOKEN IS VALID")
|
206 |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
|
|
208 |
logger.info("GATE: TOKEN HAS EXPIRED")
|
209 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
210 |
|
211 |
+
# Custom route to force clear HttpOnly cookies
|
212 |
+
def clear_cookies():
|
213 |
+
response = gr.HTTPResponse()
|
214 |
+
response.headers["Set-Cookie"] = [
|
215 |
+
"spaces-jwt=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=" + os.environ.get("SPACE_HOST", ""),
|
216 |
+
"session=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=" + os.environ.get("SPACE_HOST", "")
|
217 |
+
]
|
218 |
+
response.redirect = "/?t=" + str(int(os.times().elapsed * 1000))
|
219 |
+
return response
|
220 |
+
|
221 |
|
222 |
def get_theme():
|
223 |
cyber_theme = Base(
|
|
|
313 |
with logout_box:
|
314 |
gr.Markdown("Your session has EXPIRED. Please sign in again")
|
315 |
# gr.Button("Manual Logout", link="/logout")
|
316 |
+
#gr.HTML(JS_LOGOUT)
|
317 |
+
# Manual logout link to server-side /logout
|
318 |
+
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>')
|
319 |
+
|
320 |
+
# Custom clear cookies link
|
321 |
+
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>')
|
322 |
+
|
323 |
|
324 |
# Shown when logged IN
|
325 |
submit_panel = gr.Group(visible=False)
|
|
|
373 |
# blocks.load(gate_submission, inputs=None, outputs=[login_box, submit_panel])
|
374 |
blocks.load(gate_submission, inputs=None, outputs=[login_box, logout_box, submit_panel])
|
375 |
|
376 |
+
blocks.route("/clear-cookies", clear_cookies)
|
377 |
+
|
378 |
|
379 |
logger.info("Scheduler")
|
380 |
scheduler = BackgroundScheduler()
|