Spaces:
Sleeping
Sleeping
added conversation history download button
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ import tiktoken
|
|
| 13 |
import os
|
| 14 |
import uuid
|
| 15 |
import json
|
|
|
|
| 16 |
|
| 17 |
# -----------------------
|
| 18 |
# Configuration and Setup
|
|
@@ -72,7 +73,7 @@ logger.info("Curriculum loading complete.")
|
|
| 72 |
# Set up model, retriever chunks, conversation db
|
| 73 |
MODEL_NAME = "gpt-4o-mini"
|
| 74 |
TOP_K = 3 # number of chunks to retrieve
|
| 75 |
-
DB_NAME = "./
|
| 76 |
|
| 77 |
# -----------------------
|
| 78 |
# Helper Functions
|
|
@@ -308,9 +309,10 @@ def build_prompt(user_query, retrieved_chunks, messages):
|
|
| 308 |
if retrieved_chunks:
|
| 309 |
chunks_text = "\n".join([f"Page {c['page_num']}:\n{c['content']}" for c in retrieved_chunks])
|
| 310 |
retrieved_content_instruction = (
|
| 311 |
-
"
|
| 312 |
-
"
|
| 313 |
-
"
|
|
|
|
| 314 |
"Retrieved chunks:"
|
| 315 |
)
|
| 316 |
prompt_messages.append({"role": "system", "content": retrieved_content_instruction + "\n" + chunks_text})
|
|
@@ -443,6 +445,51 @@ try:
|
|
| 443 |
|
| 444 |
retriever = load_retriever()
|
| 445 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
st.title("Prometheus v0.3 - Personal Tutor")
|
| 447 |
|
| 448 |
# Instructions
|
|
|
|
| 13 |
import os
|
| 14 |
import uuid
|
| 15 |
import json
|
| 16 |
+
import pandas as pd
|
| 17 |
|
| 18 |
# -----------------------
|
| 19 |
# Configuration and Setup
|
|
|
|
| 73 |
# Set up model, retriever chunks, conversation db
|
| 74 |
MODEL_NAME = "gpt-4o-mini"
|
| 75 |
TOP_K = 3 # number of chunks to retrieve
|
| 76 |
+
DB_NAME = "./tmp/conversations.db"
|
| 77 |
|
| 78 |
# -----------------------
|
| 79 |
# Helper Functions
|
|
|
|
| 309 |
if retrieved_chunks:
|
| 310 |
chunks_text = "\n".join([f"Page {c['page_num']}:\n{c['content']}" for c in retrieved_chunks])
|
| 311 |
retrieved_content_instruction = (
|
| 312 |
+
"guide students toward understanding through thought-provoking questions while recognizing when they need factual clarity or binary responses to build foundational knowledge."
|
| 313 |
+
"Your challenge is to discern when a student requires immediate factual support and when they are ready for open-ended exploration that fosters critical thinking."
|
| 314 |
+
"You must use the retrieved content to guide and position your responses and follow-up questions, relying solely on this information as your source of truth."
|
| 315 |
+
"Do not use any external knowledge or make assumptions beyond the retrieved content, and if it is insufficient to answer the question, explain that no answer can be provided because the question is beyond the scope of the course. This dynamic approach ensures students develop both essential knowledge and the ability to reason critically and independently."
|
| 316 |
"Retrieved chunks:"
|
| 317 |
)
|
| 318 |
prompt_messages.append({"role": "system", "content": retrieved_content_instruction + "\n" + chunks_text})
|
|
|
|
| 445 |
|
| 446 |
retriever = load_retriever()
|
| 447 |
|
| 448 |
+
# -----------------------
|
| 449 |
+
# Download Chat History as CSV button
|
| 450 |
+
# -----------------------
|
| 451 |
+
def get_db_contents_as_csv():
|
| 452 |
+
"""
|
| 453 |
+
Fetches the database contents for the current session as a CSV-formatted byte string.
|
| 454 |
+
"""
|
| 455 |
+
session_id = get_session_id()
|
| 456 |
+
logger.info(f"Fetching chat history for session_id={session_id} as CSV.")
|
| 457 |
+
|
| 458 |
+
try:
|
| 459 |
+
conn = sqlite3.connect(DB_NAME)
|
| 460 |
+
query = "SELECT role, content, timestamp FROM conversations WHERE session_id = ? ORDER BY id ASC"
|
| 461 |
+
df = pd.read_sql_query(query, conn, params=(session_id,))
|
| 462 |
+
conn.close()
|
| 463 |
+
|
| 464 |
+
if not df.empty:
|
| 465 |
+
logger.info(f"Successfully fetched {len(df)} rows for session_id={session_id}.")
|
| 466 |
+
return df.to_csv(index=False).encode('utf-8')
|
| 467 |
+
else:
|
| 468 |
+
logger.warning(f"No data found for session_id={session_id}. Returning empty result.")
|
| 469 |
+
return None
|
| 470 |
+
except sqlite3.Error as e:
|
| 471 |
+
logger.error(f"Error reading database: {e}")
|
| 472 |
+
st.error(f"Error reading database: {e}")
|
| 473 |
+
return None
|
| 474 |
+
except Exception as e:
|
| 475 |
+
logger.error(f"Unexpected error while fetching database contents: {e}")
|
| 476 |
+
st.error(f"Unexpected error: {e}")
|
| 477 |
+
return None
|
| 478 |
+
|
| 479 |
+
st.sidebar.header("Download Your Chat History")
|
| 480 |
+
if st.sidebar.button("Download History as CSV"):
|
| 481 |
+
csv = get_db_contents_as_csv()
|
| 482 |
+
if csv:
|
| 483 |
+
st.sidebar.download_button(
|
| 484 |
+
label="Download as CSV",
|
| 485 |
+
data=csv,
|
| 486 |
+
file_name="chat_history.csv",
|
| 487 |
+
mime="text/csv"
|
| 488 |
+
)
|
| 489 |
+
else:
|
| 490 |
+
st.sidebar.warning("No chat history available for download.")
|
| 491 |
+
# -----------------------
|
| 492 |
+
|
| 493 |
st.title("Prometheus v0.3 - Personal Tutor")
|
| 494 |
|
| 495 |
# Instructions
|