JirasakJo commited on
Commit
1335474
·
verified ·
1 Parent(s): 900a6a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -26
app.py CHANGED
@@ -313,35 +313,41 @@ 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
- st.session_state.processing_query = True
317
-
318
  # Add user message to chat history
319
  st.session_state.chat_history.append(("user", user_query))
320
 
321
- # Maintain context memory
322
- if len(st.session_state.context_memory) > 5:
323
- st.session_state.context_memory.pop(0)
324
-
325
- # Build query with context
326
- query_with_context = "\n".join(
327
- [f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
328
- ) + f"\nQ: {user_query}"
329
-
330
- # Process query
331
- result = st.session_state.pipeline.process_query(query_with_context)
332
-
333
- # Create response dictionary with answer and documents
334
- response_dict = {
335
- "answer": result.get("answer", ""),
336
- "documents": result.get("documents", [])
337
- }
338
-
339
- # Update chat history and context
340
- st.session_state.chat_history.append(("assistant", response_dict))
341
- st.session_state.context_memory.append({"query": user_query, "answer": response_dict})
342
-
343
- # Save to QA history
344
- add_to_qa_history(user_query, response_dict)
 
 
 
 
 
 
 
 
345
 
346
  except Exception as e:
347
  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
  # 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
+ # Add temporary loading message
322
+ temp_message = ("assistant", "🤖 กำลังค้นหาคำตอบ...")
323
+ st.session_state.chat_history.append(temp_message)
324
+ st.rerun() # Force refresh to show loading state
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
+ # Remove loading message and add real response
345
+ st.session_state.chat_history.pop() # Remove loading message
346
+ st.session_state.chat_history.append(("assistant", response_dict))
347
+ st.session_state.context_memory.append({"query": user_query, "answer": response_dict})
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)}"))