Spaces:
Running
Running
deploy at 2024-08-24 15:22:52.475046
Browse files
main.py
CHANGED
@@ -171,7 +171,6 @@ middlewares = [
|
|
171 |
SessionMiddleware,
|
172 |
secret_key=get_key(fname=sess_key_path),
|
173 |
max_age=3600,
|
174 |
-
same_site='lax', # This allows cookies to be sent in top-level navigations
|
175 |
),
|
176 |
Middleware(XFrameOptionsMiddleware),
|
177 |
]
|
@@ -601,23 +600,16 @@ async def search(userquery: str, ranking: str, sess):
|
|
601 |
|
602 |
|
603 |
@app.get("/download_csv")
|
604 |
-
def download_csv(auth
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
all_queries = []
|
609 |
-
for user_id, user_sess in app.state.sessions.items():
|
610 |
-
user_queries = user_sess.get('queries', [])
|
611 |
-
for query in user_queries:
|
612 |
-
query['user_id'] = user_id
|
613 |
-
all_queries.extend(user_queries)
|
614 |
|
615 |
# Create CSV in memory
|
616 |
csv_file = StringIO()
|
617 |
csv_writer = csv.writer(csv_file)
|
618 |
-
csv_writer.writerow(["Query", "
|
619 |
-
for query in
|
620 |
-
csv_writer.writerow([query
|
621 |
|
622 |
# Move to the beginning of the StringIO object
|
623 |
csv_file.seek(0)
|
@@ -636,27 +628,21 @@ def download_csv(auth, sess):
|
|
636 |
|
637 |
|
638 |
@app.get("/admin")
|
639 |
-
def get_admin(auth,
|
640 |
-
if not auth:
|
641 |
-
return RedirectResponse("/login", status_code=303)
|
642 |
-
|
643 |
limit = 15
|
644 |
offset = (page - 1) * limit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
645 |
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
for query in user_queries:
|
650 |
-
query['user_id'] = user_id
|
651 |
-
all_queries.extend(user_queries)
|
652 |
-
|
653 |
-
# Sort queries by timestamp in descending order
|
654 |
-
all_queries.sort(key=lambda x: x['timestamp'], reverse=True)
|
655 |
-
|
656 |
-
total_queries = len(all_queries)
|
657 |
-
queries = all_queries[offset:offset+limit]
|
658 |
-
|
659 |
-
total_pages = (total_queries + limit - 1) // limit
|
660 |
|
661 |
# Define the range of pages to display
|
662 |
page_window = 5 # Number of pages to display at once
|
@@ -736,16 +722,16 @@ def get_admin(auth, sess, page: int = 1):
|
|
736 |
Thead(
|
737 |
Tr(
|
738 |
Th("Query"),
|
739 |
-
Th("
|
740 |
Th("Datetime"),
|
741 |
)
|
742 |
),
|
743 |
Tbody(
|
744 |
*[
|
745 |
Tr(
|
746 |
-
Td(query
|
747 |
-
Td(query
|
748 |
-
Td(
|
749 |
)
|
750 |
for query in queries
|
751 |
],
|
@@ -830,12 +816,13 @@ def get_document(docid: str, sess):
|
|
830 |
resp = vespa_app.get_data(data_id=docid, schema="doc", namespace="tutorial")
|
831 |
doc = resp.json
|
832 |
# Link with Back to search results at top of page
|
|
|
833 |
return Main(
|
834 |
Div(
|
835 |
A(
|
836 |
I(cls="fa fa-arrow-left"),
|
837 |
"Back to search results",
|
838 |
-
hx_get=f"/search?userquery={
|
839 |
hx_target="#results",
|
840 |
style="margin: 10px;",
|
841 |
),
|
@@ -850,17 +837,4 @@ if not DEV_MODE:
|
|
850 |
setup_hf_backup(app)
|
851 |
except Exception as e:
|
852 |
print(f"Error setting up hf backup: {e}")
|
853 |
-
|
854 |
-
# Add this at the end of your file, before serve()
|
855 |
-
app.state.sessions = {}
|
856 |
-
|
857 |
-
@app.middleware("http")
|
858 |
-
async def add_session_to_app_state(request, call_next):
|
859 |
-
response = await call_next(request)
|
860 |
-
if 'session' in request.scope:
|
861 |
-
session = request.scope['session']
|
862 |
-
if 'user_id' in session:
|
863 |
-
app.state.sessions[session['user_id']] = session
|
864 |
-
return response
|
865 |
-
|
866 |
serve()
|
|
|
171 |
SessionMiddleware,
|
172 |
secret_key=get_key(fname=sess_key_path),
|
173 |
max_age=3600,
|
|
|
174 |
),
|
175 |
Middleware(XFrameOptionsMiddleware),
|
176 |
]
|
|
|
600 |
|
601 |
|
602 |
@app.get("/download_csv")
|
603 |
+
def download_csv(auth):
|
604 |
+
queries_dict = list(db.query("SELECT * FROM queries"))
|
605 |
+
queries = [Query(**query) for query in queries_dict]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
606 |
|
607 |
# Create CSV in memory
|
608 |
csv_file = StringIO()
|
609 |
csv_writer = csv.writer(csv_file)
|
610 |
+
csv_writer.writerow(["Query", "Session ID", "Timestamp"])
|
611 |
+
for query in queries:
|
612 |
+
csv_writer.writerow([query.query, query.sess_id, query.timestamp])
|
613 |
|
614 |
# Move to the beginning of the StringIO object
|
615 |
csv_file.seek(0)
|
|
|
628 |
|
629 |
|
630 |
@app.get("/admin")
|
631 |
+
def get_admin(auth, page: int = 1):
|
|
|
|
|
|
|
632 |
limit = 15
|
633 |
offset = (page - 1) * limit
|
634 |
+
total_queries_result = list(
|
635 |
+
db.query("SELECT COUNT(*) AS count FROM queries ORDER BY timestamp DESC")
|
636 |
+
)
|
637 |
+
total_queries = total_queries_result[0]["count"]
|
638 |
+
queries_dict = list(
|
639 |
+
db.query(f"SELECT * FROM queries LIMIT {limit} OFFSET {offset}")
|
640 |
+
)
|
641 |
+
queries = [Query(**query) for query in queries_dict]
|
642 |
|
643 |
+
total_pages = (
|
644 |
+
total_queries + limit - 1
|
645 |
+
) // limit # Calculate total number of pages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
646 |
|
647 |
# Define the range of pages to display
|
648 |
page_window = 5 # Number of pages to display at once
|
|
|
722 |
Thead(
|
723 |
Tr(
|
724 |
Th("Query"),
|
725 |
+
Th("Session ID"),
|
726 |
Th("Datetime"),
|
727 |
)
|
728 |
),
|
729 |
Tbody(
|
730 |
*[
|
731 |
Tr(
|
732 |
+
Td(query.query),
|
733 |
+
Td(query.sess_id),
|
734 |
+
Td(query.get_datetime()),
|
735 |
)
|
736 |
for query in queries
|
737 |
],
|
|
|
816 |
resp = vespa_app.get_data(data_id=docid, schema="doc", namespace="tutorial")
|
817 |
doc = resp.json
|
818 |
# Link with Back to search results at top of page
|
819 |
+
last_query = sess.get('queries', [{}])[-1].get('query', '')
|
820 |
return Main(
|
821 |
Div(
|
822 |
A(
|
823 |
I(cls="fa fa-arrow-left"),
|
824 |
"Back to search results",
|
825 |
+
hx_get=f"/search?userquery={last_query}",
|
826 |
hx_target="#results",
|
827 |
style="margin: 10px;",
|
828 |
),
|
|
|
837 |
setup_hf_backup(app)
|
838 |
except Exception as e:
|
839 |
print(f"Error setting up hf backup: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
840 |
serve()
|