Spaces:
Sleeping
Sleeping
deploy at 2024-08-25 16:00:42.797545
Browse files
main.py
CHANGED
|
@@ -64,7 +64,7 @@ from urllib.parse import quote
|
|
| 64 |
import uuid
|
| 65 |
import secrets
|
| 66 |
|
| 67 |
-
DEV_MODE =
|
| 68 |
|
| 69 |
if DEV_MODE:
|
| 70 |
print("Running in DEV_MODE - Hot reload enabled")
|
|
@@ -121,7 +121,7 @@ login_redir = RedirectResponse("/login", status_code=303)
|
|
| 121 |
|
| 122 |
|
| 123 |
def user_auth_before(req, sess):
|
| 124 |
-
|
| 125 |
|
| 126 |
|
| 127 |
spinner_css = Style("""
|
|
@@ -308,9 +308,8 @@ def spinner_div(hidden: bool = False):
|
|
| 308 |
|
| 309 |
|
| 310 |
@app.route("/")
|
| 311 |
-
def get(
|
| 312 |
-
|
| 313 |
-
auth = request.session.get("auth", False)
|
| 314 |
queries = [
|
| 315 |
"Breast Cancer Cells Feed on Cholesterol",
|
| 316 |
"Treating Asthma With Plants vs. Pills",
|
|
@@ -416,8 +415,8 @@ class Login:
|
|
| 416 |
|
| 417 |
|
| 418 |
@app.get("/login")
|
| 419 |
-
def get_login_form(
|
| 420 |
-
auth =
|
| 421 |
frm = Form(
|
| 422 |
Input(id="name", placeholder="Name"),
|
| 423 |
Input(id="pwd", type="password", placeholder="Password"),
|
|
@@ -438,21 +437,21 @@ def get_login_form(request: Request, error: bool = False):
|
|
| 438 |
|
| 439 |
|
| 440 |
@app.post("/login")
|
| 441 |
-
async def login(request: Request):
|
| 442 |
form = await request.form()
|
| 443 |
username = form.get("name")
|
| 444 |
password = form.get("pwd")
|
| 445 |
|
| 446 |
if username == ADMIN_NAME and compare_digest(ADMIN_PWD.encode("utf-8"), password.encode("utf-8")):
|
| 447 |
-
|
| 448 |
return RedirectResponse("/admin", status_code=303)
|
| 449 |
|
| 450 |
return RedirectResponse("/login?error=True", status_code=303)
|
| 451 |
|
| 452 |
|
| 453 |
@app.route("/logout")
|
| 454 |
-
async def logout(
|
| 455 |
-
|
| 456 |
return RedirectResponse("/")
|
| 457 |
|
| 458 |
|
|
@@ -472,25 +471,24 @@ def replace_hi_with_strong(text):
|
|
| 472 |
return elements
|
| 473 |
|
| 474 |
|
| 475 |
-
def log_query_to_db(query, ranking,
|
| 476 |
queries.insert(
|
| 477 |
Query(query=query, ranking=ranking, sess_id=sesskey, timestamp=int(time.time()))
|
| 478 |
)
|
| 479 |
-
if 'user_id' not in
|
| 480 |
-
|
| 481 |
|
| 482 |
-
|
| 483 |
-
request.session['queries'] = []
|
| 484 |
|
| 485 |
query_data = {
|
| 486 |
'query': query,
|
| 487 |
'ranking': ranking,
|
| 488 |
'timestamp': int(time.time())
|
| 489 |
}
|
| 490 |
-
|
| 491 |
|
| 492 |
# Limit the number of queries stored in the session to prevent it from growing too large
|
| 493 |
-
|
| 494 |
|
| 495 |
return query_data
|
| 496 |
|
|
@@ -580,10 +578,10 @@ def get_yql(ranking: RankProfile, userquery: str) -> T[str, dict]:
|
|
| 580 |
|
| 581 |
|
| 582 |
@app.get("/search")
|
| 583 |
-
async def search(
|
| 584 |
-
print(
|
| 585 |
quoted = quote(userquery) + "&ranking=" + ranking
|
| 586 |
-
log_query_to_db(userquery, ranking,
|
| 587 |
yql, body = get_yql(ranking, userquery)
|
| 588 |
async with vespa_app.asyncio() as session:
|
| 589 |
resp = await session.query(
|
|
@@ -651,8 +649,8 @@ def download_csv(request: Request):
|
|
| 651 |
|
| 652 |
|
| 653 |
@app.route("/admin")
|
| 654 |
-
async def admin(
|
| 655 |
-
auth =
|
| 656 |
if not auth:
|
| 657 |
print(f"Not authenticated: {auth}")
|
| 658 |
return RedirectResponse("/login", status_code=303)
|
|
|
|
| 64 |
import uuid
|
| 65 |
import secrets
|
| 66 |
|
| 67 |
+
DEV_MODE = False
|
| 68 |
|
| 69 |
if DEV_MODE:
|
| 70 |
print("Running in DEV_MODE - Hot reload enabled")
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
def user_auth_before(req, sess):
|
| 124 |
+
sess.setdefault('auth', False)
|
| 125 |
|
| 126 |
|
| 127 |
spinner_css = Style("""
|
|
|
|
| 308 |
|
| 309 |
|
| 310 |
@app.route("/")
|
| 311 |
+
def get(sess):
|
| 312 |
+
auth = sess.get('auth', False)
|
|
|
|
| 313 |
queries = [
|
| 314 |
"Breast Cancer Cells Feed on Cholesterol",
|
| 315 |
"Treating Asthma With Plants vs. Pills",
|
|
|
|
| 415 |
|
| 416 |
|
| 417 |
@app.get("/login")
|
| 418 |
+
def get_login_form(sess, error: bool = False):
|
| 419 |
+
auth = sess.get('auth', False)
|
| 420 |
frm = Form(
|
| 421 |
Input(id="name", placeholder="Name"),
|
| 422 |
Input(id="pwd", type="password", placeholder="Password"),
|
|
|
|
| 437 |
|
| 438 |
|
| 439 |
@app.post("/login")
|
| 440 |
+
async def login(sess, request: Request):
|
| 441 |
form = await request.form()
|
| 442 |
username = form.get("name")
|
| 443 |
password = form.get("pwd")
|
| 444 |
|
| 445 |
if username == ADMIN_NAME and compare_digest(ADMIN_PWD.encode("utf-8"), password.encode("utf-8")):
|
| 446 |
+
sess['auth'] = True
|
| 447 |
return RedirectResponse("/admin", status_code=303)
|
| 448 |
|
| 449 |
return RedirectResponse("/login?error=True", status_code=303)
|
| 450 |
|
| 451 |
|
| 452 |
@app.route("/logout")
|
| 453 |
+
async def logout(sess):
|
| 454 |
+
sess.clear()
|
| 455 |
return RedirectResponse("/")
|
| 456 |
|
| 457 |
|
|
|
|
| 471 |
return elements
|
| 472 |
|
| 473 |
|
| 474 |
+
def log_query_to_db(query, ranking, sess):
|
| 475 |
queries.insert(
|
| 476 |
Query(query=query, ranking=ranking, sess_id=sesskey, timestamp=int(time.time()))
|
| 477 |
)
|
| 478 |
+
if 'user_id' not in sess:
|
| 479 |
+
sess['user_id'] = str(uuid.uuid4())
|
| 480 |
|
| 481 |
+
sess.setdefault('queries', [])
|
|
|
|
| 482 |
|
| 483 |
query_data = {
|
| 484 |
'query': query,
|
| 485 |
'ranking': ranking,
|
| 486 |
'timestamp': int(time.time())
|
| 487 |
}
|
| 488 |
+
sess['queries'].append(query_data)
|
| 489 |
|
| 490 |
# Limit the number of queries stored in the session to prevent it from growing too large
|
| 491 |
+
sess['queries'] = sess['queries'][-100:] # Keep only the last 100 queries
|
| 492 |
|
| 493 |
return query_data
|
| 494 |
|
|
|
|
| 578 |
|
| 579 |
|
| 580 |
@app.get("/search")
|
| 581 |
+
async def search(sess, userquery: str, ranking: str):
|
| 582 |
+
print(sess)
|
| 583 |
quoted = quote(userquery) + "&ranking=" + ranking
|
| 584 |
+
log_query_to_db(userquery, ranking, sess)
|
| 585 |
yql, body = get_yql(ranking, userquery)
|
| 586 |
async with vespa_app.asyncio() as session:
|
| 587 |
resp = await session.query(
|
|
|
|
| 649 |
|
| 650 |
|
| 651 |
@app.route("/admin")
|
| 652 |
+
async def admin(sess):
|
| 653 |
+
auth = sess.get('auth', False)
|
| 654 |
if not auth:
|
| 655 |
print(f"Not authenticated: {auth}")
|
| 656 |
return RedirectResponse("/login", status_code=303)
|