Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,6 @@ from calendar_rag import (
|
|
18 |
ProcessingConfig,
|
19 |
LocalizationConfig
|
20 |
)
|
21 |
-
|
22 |
# Custom CSS for enhanced styling
|
23 |
def load_custom_css():
|
24 |
st.markdown("""
|
@@ -106,6 +105,8 @@ def clear_conversation_context():
|
|
106 |
# Clear the context memory
|
107 |
st.session_state.context_memory = []
|
108 |
|
|
|
|
|
109 |
def initialize_pipeline():
|
110 |
"""Initialize RAG pipeline with conversation memory support"""
|
111 |
try:
|
@@ -410,7 +411,6 @@ def handle_submit(user_query: str):
|
|
410 |
|
411 |
# Convert the Streamlit chat history format to RAG format
|
412 |
rag_conversation_history = []
|
413 |
-
|
414 |
for role, content in st.session_state.chat_history:
|
415 |
# Map Streamlit's role names to the format used in the RAG system
|
416 |
rag_role = "user" if role == "user" else "assistant"
|
@@ -429,27 +429,17 @@ def handle_submit(user_query: str):
|
|
429 |
|
430 |
# Process query with conversation history
|
431 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
432 |
-
|
433 |
-
|
434 |
-
|
|
|
435 |
|
436 |
-
#
|
437 |
-
query_with_context = "\n".join(
|
438 |
-
[f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
|
439 |
-
) + f"\nQ: {user_query}"
|
440 |
-
|
441 |
-
result = st.session_state.pipeline.process_query(query_with_context)
|
442 |
-
|
443 |
-
# Create response dictionary with answer and documents
|
444 |
response_dict = {
|
445 |
"answer": result.get("answer", ""),
|
446 |
-
"documents": result.get("
|
447 |
}
|
448 |
|
449 |
-
# Update chat history and context
|
450 |
-
st.session_state.chat_history.append(("assistant", response_dict))
|
451 |
-
st.session_state.context_memory.append({"query": user_query, "answer": response_dict})
|
452 |
-
|
453 |
# Update chat history and context
|
454 |
add_to_history("assistant", response_dict)
|
455 |
|
@@ -486,7 +476,7 @@ def create_chat_input():
|
|
486 |
placeholder="เช่น: วิชาเลือกมีอะไรบ้าง?"
|
487 |
)
|
488 |
|
489 |
-
col1, col2 = st.columns([
|
490 |
|
491 |
with col1:
|
492 |
submitted = st.form_submit_button(
|
@@ -502,13 +492,23 @@ def create_chat_input():
|
|
502 |
use_container_width=True
|
503 |
)
|
504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
505 |
if submitted:
|
506 |
handle_submit(query)
|
507 |
|
508 |
if clear_button:
|
509 |
-
st.session_state.context_memory = []
|
510 |
st.session_state.chat_history = []
|
511 |
st.rerun()
|
|
|
|
|
|
|
|
|
512 |
|
513 |
def main():
|
514 |
# Page config
|
|
|
18 |
ProcessingConfig,
|
19 |
LocalizationConfig
|
20 |
)
|
|
|
21 |
# Custom CSS for enhanced styling
|
22 |
def load_custom_css():
|
23 |
st.markdown("""
|
|
|
105 |
# Clear the context memory
|
106 |
st.session_state.context_memory = []
|
107 |
|
108 |
+
# Note: We keep st.session_state.chat_history for UI display purposes
|
109 |
+
|
110 |
def initialize_pipeline():
|
111 |
"""Initialize RAG pipeline with conversation memory support"""
|
112 |
try:
|
|
|
411 |
|
412 |
# Convert the Streamlit chat history format to RAG format
|
413 |
rag_conversation_history = []
|
|
|
414 |
for role, content in st.session_state.chat_history:
|
415 |
# Map Streamlit's role names to the format used in the RAG system
|
416 |
rag_role = "user" if role == "user" else "assistant"
|
|
|
429 |
|
430 |
# Process query with conversation history
|
431 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
432 |
+
result = st.session_state.pipeline.process_query(
|
433 |
+
query=user_query,
|
434 |
+
conversation_history=rag_conversation_history
|
435 |
+
)
|
436 |
|
437 |
+
# Create response with same structure as main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
438 |
response_dict = {
|
439 |
"answer": result.get("answer", ""),
|
440 |
+
"documents": result.get("relevant_docs", [])
|
441 |
}
|
442 |
|
|
|
|
|
|
|
|
|
443 |
# Update chat history and context
|
444 |
add_to_history("assistant", response_dict)
|
445 |
|
|
|
476 |
placeholder="เช่น: วิชาเลือกมีอะไรบ้าง?"
|
477 |
)
|
478 |
|
479 |
+
col1, col2, col3 = st.columns([5, 3, 2])
|
480 |
|
481 |
with col1:
|
482 |
submitted = st.form_submit_button(
|
|
|
492 |
use_container_width=True
|
493 |
)
|
494 |
|
495 |
+
with col3:
|
496 |
+
clear_context_button = st.form_submit_button(
|
497 |
+
"🧠 ล้างบริบทสนทนา",
|
498 |
+
type="secondary",
|
499 |
+
use_container_width=True
|
500 |
+
)
|
501 |
+
|
502 |
if submitted:
|
503 |
handle_submit(query)
|
504 |
|
505 |
if clear_button:
|
|
|
506 |
st.session_state.chat_history = []
|
507 |
st.rerun()
|
508 |
+
|
509 |
+
if clear_context_button:
|
510 |
+
clear_conversation_context()
|
511 |
+
st.info("ล้างบริบทสนทนาแล้ว แต่ยังคงประวัติการแสดงสนทนา")
|
512 |
|
513 |
def main():
|
514 |
# Page config
|