JirasakJo commited on
Commit
b6b92b6
·
verified ·
1 Parent(s): 69571da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -10
app.py CHANGED
@@ -229,38 +229,32 @@ def submit():
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
 
@@ -322,6 +316,7 @@ def main():
322
  {content}
323
  </div>
324
  """, unsafe_allow_html=True)
 
325
  else:
326
  # ✅ FIX: Handle both string and dictionary content safely
327
  if isinstance(content, dict):
 
229
  st.warning("⚠️ กรุณาระบุคำถาม")
230
  return
231
 
232
+ user_query = st.session_state.query_input.strip() # Strip unnecessary spaces
233
  st.session_state.query_input = "" # Clear input
234
 
235
+ # Prevent duplicate user query from being added twice
236
+ if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
237
+ st.session_state.chat_history.append(("user", user_query))
238
 
239
  # Maintain a rolling context of past interactions
240
  if len(st.session_state.context_memory) > 5: # Limit to last 5 Q&A pairs
241
  st.session_state.context_memory.pop(0)
242
 
243
  try:
 
244
  query_with_context = "\n".join(
245
  [f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
246
  ) + f"\nQ: {user_query}"
247
 
 
248
  result = st.session_state.pipeline.process_query(query_with_context)
 
249
 
250
  assistant_answer = result["answer"]
251
 
 
252
  st.session_state.chat_history.append(("assistant", assistant_answer))
 
 
253
  st.session_state.context_memory.append({"query": user_query, "answer": assistant_answer})
254
 
 
255
  add_to_qa_history(user_query, assistant_answer)
256
 
257
  except Exception as e:
 
258
  st.session_state.chat_history.append(("assistant", f"❌ เกิดข้อผิดพลาด: {str(e)}"))
259
  st.error(f"Query processing error: {e}")
260
 
 
316
  {content}
317
  </div>
318
  """, unsafe_allow_html=True)
319
+
320
  else:
321
  # ✅ FIX: Handle both string and dictionary content safely
322
  if isinstance(content, dict):