Spaces:
Running
Running
deploy at 2024-08-25 07:13:18.754224
Browse files
main.py
CHANGED
@@ -314,9 +314,9 @@ def spinner_div(hidden: bool = False):
|
|
314 |
|
315 |
|
316 |
@app.route("/")
|
317 |
-
def get(
|
318 |
# Can not get auth directly, as it is skipped in beforeware
|
319 |
-
auth =
|
320 |
queries = [
|
321 |
"Breast Cancer Cells Feed on Cholesterol",
|
322 |
"Treating Asthma With Plants vs. Pills",
|
@@ -422,8 +422,8 @@ class Login:
|
|
422 |
|
423 |
|
424 |
@app.get("/login")
|
425 |
-
def get_login_form(
|
426 |
-
auth =
|
427 |
frm = Form(
|
428 |
Input(id="name", placeholder="Name"),
|
429 |
Input(id="pwd", type="password", placeholder="Password"),
|
@@ -444,30 +444,22 @@ def get_login_form(sess, error: bool = False):
|
|
444 |
|
445 |
|
446 |
@app.post("/login")
|
447 |
-
async def login(request):
|
448 |
form = await request.form()
|
449 |
username = form.get("name")
|
450 |
password = form.get("pwd")
|
451 |
|
452 |
if username == ADMIN_NAME and compare_digest(ADMIN_PWD.encode("utf-8"), password.encode("utf-8")):
|
453 |
request.session["auth"] = True
|
454 |
-
#
|
455 |
response = RedirectResponse("/admin", status_code=303)
|
456 |
-
response.set_cookie(
|
457 |
-
key="session",
|
458 |
-
value=request.session,
|
459 |
-
httponly=True,
|
460 |
-
secure=True,
|
461 |
-
samesite="Lax"
|
462 |
-
)
|
463 |
return response
|
464 |
|
465 |
return RedirectResponse("/login?error=True", status_code=303)
|
466 |
|
467 |
|
468 |
@app.route("/logout")
|
469 |
-
async def logout(request):
|
470 |
-
request.session.
|
471 |
return RedirectResponse("/")
|
472 |
|
473 |
|
@@ -487,25 +479,25 @@ def replace_hi_with_strong(text):
|
|
487 |
return elements
|
488 |
|
489 |
|
490 |
-
def log_query_to_db(query, ranking,
|
491 |
queries.insert(
|
492 |
Query(query=query, ranking=ranking, sess_id=sesskey, timestamp=int(time.time()))
|
493 |
)
|
494 |
-
if 'user_id' not in
|
495 |
-
|
496 |
|
497 |
-
if 'queries' not in
|
498 |
-
|
499 |
|
500 |
query_data = {
|
501 |
'query': query,
|
502 |
'ranking': ranking,
|
503 |
'timestamp': int(time.time())
|
504 |
}
|
505 |
-
|
506 |
|
507 |
# Limit the number of queries stored in the session to prevent it from growing too large
|
508 |
-
|
509 |
|
510 |
return query_data
|
511 |
|
@@ -595,10 +587,10 @@ def get_yql(ranking: RankProfile, userquery: str) -> T[str, dict]:
|
|
595 |
|
596 |
|
597 |
@app.get("/search")
|
598 |
-
async def search(userquery: str, ranking: str
|
599 |
-
print(
|
600 |
quoted = quote(userquery) + "&ranking=" + ranking
|
601 |
-
log_query_to_db(userquery, ranking,
|
602 |
yql, body = get_yql(ranking, userquery)
|
603 |
async with vespa_app.asyncio() as session:
|
604 |
resp = await session.query(
|
@@ -638,7 +630,7 @@ async def search(userquery: str, ranking: str, sess):
|
|
638 |
|
639 |
|
640 |
@app.get("/download_csv")
|
641 |
-
def download_csv(
|
642 |
queries_dict = list(db.query("SELECT * FROM queries"))
|
643 |
queries = [Query(**query) for query in queries_dict]
|
644 |
|
@@ -666,10 +658,8 @@ def download_csv(auth):
|
|
666 |
|
667 |
|
668 |
@app.route("/admin")
|
669 |
-
async def admin(request):
|
670 |
-
|
671 |
-
if not sess.get("auth", False):
|
672 |
-
print(sess)
|
673 |
return RedirectResponse("/login", status_code=303)
|
674 |
|
675 |
page = int(request.query_params.get("page", 1))
|
|
|
314 |
|
315 |
|
316 |
@app.route("/")
|
317 |
+
def get(request: Request):
|
318 |
# Can not get auth directly, as it is skipped in beforeware
|
319 |
+
auth = request.session.get("auth", False)
|
320 |
queries = [
|
321 |
"Breast Cancer Cells Feed on Cholesterol",
|
322 |
"Treating Asthma With Plants vs. Pills",
|
|
|
422 |
|
423 |
|
424 |
@app.get("/login")
|
425 |
+
def get_login_form(request: Request, error: bool = False):
|
426 |
+
auth = request.session.get("auth", False)
|
427 |
frm = Form(
|
428 |
Input(id="name", placeholder="Name"),
|
429 |
Input(id="pwd", type="password", placeholder="Password"),
|
|
|
444 |
|
445 |
|
446 |
@app.post("/login")
|
447 |
+
async def login(request: Request):
|
448 |
form = await request.form()
|
449 |
username = form.get("name")
|
450 |
password = form.get("pwd")
|
451 |
|
452 |
if username == ADMIN_NAME and compare_digest(ADMIN_PWD.encode("utf-8"), password.encode("utf-8")):
|
453 |
request.session["auth"] = True
|
|
|
454 |
response = RedirectResponse("/admin", status_code=303)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
455 |
return response
|
456 |
|
457 |
return RedirectResponse("/login?error=True", status_code=303)
|
458 |
|
459 |
|
460 |
@app.route("/logout")
|
461 |
+
async def logout(request: Request):
|
462 |
+
request.session.clear()
|
463 |
return RedirectResponse("/")
|
464 |
|
465 |
|
|
|
479 |
return elements
|
480 |
|
481 |
|
482 |
+
def log_query_to_db(query, ranking, request):
|
483 |
queries.insert(
|
484 |
Query(query=query, ranking=ranking, sess_id=sesskey, timestamp=int(time.time()))
|
485 |
)
|
486 |
+
if 'user_id' not in request.session:
|
487 |
+
request.session['user_id'] = str(uuid.uuid4())
|
488 |
|
489 |
+
if 'queries' not in request.session:
|
490 |
+
request.session['queries'] = []
|
491 |
|
492 |
query_data = {
|
493 |
'query': query,
|
494 |
'ranking': ranking,
|
495 |
'timestamp': int(time.time())
|
496 |
}
|
497 |
+
request.session['queries'].append(query_data)
|
498 |
|
499 |
# Limit the number of queries stored in the session to prevent it from growing too large
|
500 |
+
request.session['queries'] = request.session['queries'][-100:] # Keep only the last 100 queries
|
501 |
|
502 |
return query_data
|
503 |
|
|
|
587 |
|
588 |
|
589 |
@app.get("/search")
|
590 |
+
async def search(request: Request, userquery: str, ranking: str):
|
591 |
+
print(request.session)
|
592 |
quoted = quote(userquery) + "&ranking=" + ranking
|
593 |
+
log_query_to_db(userquery, ranking, request)
|
594 |
yql, body = get_yql(ranking, userquery)
|
595 |
async with vespa_app.asyncio() as session:
|
596 |
resp = await session.query(
|
|
|
630 |
|
631 |
|
632 |
@app.get("/download_csv")
|
633 |
+
def download_csv(request: Request):
|
634 |
queries_dict = list(db.query("SELECT * FROM queries"))
|
635 |
queries = [Query(**query) for query in queries_dict]
|
636 |
|
|
|
658 |
|
659 |
|
660 |
@app.route("/admin")
|
661 |
+
async def admin(request: Request):
|
662 |
+
if not request.session.get("auth", False):
|
|
|
|
|
663 |
return RedirectResponse("/login", status_code=303)
|
664 |
|
665 |
page = int(request.query_params.get("page", 1))
|