Update app.py
Browse files
app.py
CHANGED
|
@@ -313,41 +313,40 @@ def handle_submit(user_query: str):
|
|
| 313 |
# Prevent duplicate submissions by checking last message
|
| 314 |
if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
|
| 315 |
try:
|
|
|
|
| 316 |
# Add user message to chat history
|
| 317 |
st.session_state.chat_history.append(("user", user_query))
|
| 318 |
|
| 319 |
# Show loading message
|
| 320 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
# Save to QA history
|
| 350 |
-
add_to_qa_history(user_query, response_dict)
|
| 351 |
|
| 352 |
except Exception as e:
|
| 353 |
st.session_state.chat_history.append(("assistant", f"❌ เกิดข้อผิดพลาด: {str(e)}"))
|
|
|
|
| 313 |
# Prevent duplicate submissions by checking last message
|
| 314 |
if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
|
| 315 |
try:
|
| 316 |
+
st.session_state.processing_query = True
|
| 317 |
# Add user message to chat history
|
| 318 |
st.session_state.chat_history.append(("user", user_query))
|
| 319 |
|
| 320 |
# Show loading message
|
| 321 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
| 322 |
+
|
| 323 |
+
# Add user message to chat history
|
| 324 |
+
st.session_state.chat_history.append(("user", user_query))
|
| 325 |
+
|
| 326 |
+
# Maintain context memory
|
| 327 |
+
if len(st.session_state.context_memory) > 5:
|
| 328 |
+
st.session_state.context_memory.pop(0)
|
| 329 |
+
|
| 330 |
+
# Build query with context
|
| 331 |
+
query_with_context = "\n".join(
|
| 332 |
+
[f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
|
| 333 |
+
) + f"\nQ: {user_query}"
|
| 334 |
+
|
| 335 |
+
# Process query
|
| 336 |
+
result = st.session_state.pipeline.process_query(query_with_context)
|
| 337 |
+
|
| 338 |
+
# Create response dictionary with answer and documents
|
| 339 |
+
response_dict = {
|
| 340 |
+
"answer": result.get("answer", ""),
|
| 341 |
+
"documents": result.get("documents", [])
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
# Update chat history and context
|
| 345 |
+
st.session_state.chat_history.append(("assistant", response_dict))
|
| 346 |
+
st.session_state.context_memory.append({"query": user_query, "answer": response_dict})
|
| 347 |
+
|
| 348 |
+
# Save to QA history
|
| 349 |
+
add_to_qa_history(user_query, response_dict)
|
|
|
|
|
|
|
| 350 |
|
| 351 |
except Exception as e:
|
| 352 |
st.session_state.chat_history.append(("assistant", f"❌ เกิดข้อผิดพลาด: {str(e)}"))
|