JirasakJo commited on
Commit
6fa1483
·
verified ·
1 Parent(s): 267ccd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -221,54 +221,52 @@ def display_chat_history():
221
  </div>
222
  """, unsafe_allow_html=True)
223
 
 
 
 
224
  def submit():
225
  if not st.session_state.query_input:
226
  st.warning("⚠️ กรุณาระบุคำถาม")
227
  return
228
 
229
- st.session_state.processing_query = True
230
-
231
  user_query = st.session_state.query_input
 
 
232
  st.session_state.chat_history.append(("user", user_query))
233
 
234
- # Show loading message
235
- st.session_state.chat_history.append(("assistant", {
236
- "answer": "🔍 กำลังค้นหาคำตอบ...",
237
- "documents": [],
238
- "query_info": {}
239
- }))
240
-
241
- st.session_state.query_input = "" # Clear input
242
 
243
  try:
244
- print("Processing query:", user_query) # Debugging
245
- result = st.session_state.pipeline.process_query(user_query)
 
 
 
 
 
246
  print("Query processed successfully") # Debugging
247
 
248
- # Update the answer
249
- st.session_state.chat_history[-1] = ("assistant", {
250
- "answer": result["answer"],
251
- "documents": result["documents"],
252
- "query_info": result["query_info"]
253
- })
254
 
255
- # Save QA history
256
- add_to_qa_history(user_query, result["answer"])
 
 
 
257
 
258
  except Exception as e:
259
  print("Error processing query:", str(e)) # Debugging
260
- st.session_state.chat_history[-1] = ("assistant", {
261
- "answer": f"❌ เกิดข้อผิดพลาด: {str(e)}",
262
- "documents": [],
263
- "query_info": {}
264
- })
265
  st.error(f"Query processing error: {e}")
266
 
267
  finally:
268
  st.session_state.processing_query = False # Reset flag
269
 
270
- # st.rerun() # Refresh UI
271
-
272
  def main():
273
  # Page config
274
  st.set_page_config(
@@ -369,6 +367,7 @@ def main():
369
 
370
  with col2:
371
  if st.button("🗑️ ล้างประวัติ", type="secondary", use_container_width=True):
 
372
  st.session_state.chat_history = []
373
  st.rerun()
374
 
 
221
  </div>
222
  """, unsafe_allow_html=True)
223
 
224
+ if 'context_memory' not in st.session_state:
225
+ st.session_state.context_memory = []
226
+
227
  def submit():
228
  if not st.session_state.query_input:
229
  st.warning("⚠️ กรุณาระบุคำถาม")
230
  return
231
 
 
 
232
  user_query = st.session_state.query_input
233
+ st.session_state.query_input = "" # Clear input
234
+
235
  st.session_state.chat_history.append(("user", user_query))
236
 
237
+ # Maintain a rolling context of past interactions
238
+ if len(st.session_state.context_memory) > 5: # Limit to last 5 Q&A pairs
239
+ st.session_state.context_memory.pop(0)
 
 
 
 
 
240
 
241
  try:
242
+ # Include past interactions for better understanding
243
+ query_with_context = "\n".join(
244
+ [f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
245
+ ) + f"\nQ: {user_query}"
246
+
247
+ print("Processing query with context:", query_with_context) # Debugging
248
+ result = st.session_state.pipeline.process_query(query_with_context)
249
  print("Query processed successfully") # Debugging
250
 
251
+ assistant_answer = result["answer"]
252
+
253
+ # Store in chat history
254
+ st.session_state.chat_history.append(("assistant", assistant_answer))
 
 
255
 
256
+ # Save the new QA pair into context memory
257
+ st.session_state.context_memory.append({"query": user_query, "answer": assistant_answer})
258
+
259
+ # Save the QA history
260
+ add_to_qa_history(user_query, assistant_answer)
261
 
262
  except Exception as e:
263
  print("Error processing query:", str(e)) # Debugging
264
+ st.session_state.chat_history.append(("assistant", f"❌ เกิดข้อผิดพลาด: {str(e)}"))
 
 
 
 
265
  st.error(f"Query processing error: {e}")
266
 
267
  finally:
268
  st.session_state.processing_query = False # Reset flag
269
 
 
 
270
  def main():
271
  # Page config
272
  st.set_page_config(
 
367
 
368
  with col2:
369
  if st.button("🗑️ ล้างประวัติ", type="secondary", use_container_width=True):
370
+ st.session_state.context_memory = []
371
  st.session_state.chat_history = []
372
  st.rerun()
373