JirasakJo commited on
Commit
82a4f42
·
verified ·
1 Parent(s): d6658b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -225,23 +225,19 @@ if 'context_memory' not in st.session_state:
225
  st.session_state.context_memory = []
226
 
227
  def submit():
228
- if st.session_state.get("processing_query", False):
229
- return
230
-
231
- st.session_state.processing_query = True # ป้องกันการเรียกซ้ำ
232
-
233
  if not st.session_state.query_input:
234
  st.warning("⚠️ กรุณาระบุคำถาม")
235
- st.session_state.processing_query = False
236
  return
237
 
238
- user_query = st.session_state.query_input.strip()
239
- st.session_state.query_input = ""
240
 
 
241
  if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
242
  st.session_state.chat_history.append(("user", user_query))
243
 
244
- if len(st.session_state.context_memory) > 5:
 
245
  st.session_state.context_memory.pop(0)
246
 
247
  try:
@@ -250,6 +246,7 @@ def submit():
250
  ) + f"\nQ: {user_query}"
251
 
252
  result = st.session_state.pipeline.process_query(query_with_context)
 
253
  assistant_answer = result["answer"]
254
 
255
  st.session_state.chat_history.append(("assistant", assistant_answer))
@@ -262,7 +259,7 @@ def submit():
262
  st.error(f"Query processing error: {e}")
263
 
264
  finally:
265
- st.session_state.processing_query = False # รีเซ็ต flag
266
 
267
  def main():
268
  # Page config
 
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.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:
 
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))
 
259
  st.error(f"Query processing error: {e}")
260
 
261
  finally:
262
+ st.session_state.processing_query = False # Reset flag
263
 
264
  def main():
265
  # Page config