JirasakJo commited on
Commit
6e45461
·
verified ·
1 Parent(s): 53e7e31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -224,16 +224,13 @@ def display_chat_history():
224
  if 'context_memory' not in st.session_state:
225
  st.session_state.context_memory = []
226
 
227
- def handle_submit():
228
  """Handle form submission logic"""
229
- if not st.session_state.query_input:
230
  st.warning("⚠️ กรุณาระบุคำถาม")
231
  return
232
 
233
- user_query = st.session_state.query_input.strip()
234
-
235
- # Important: Clear input immediately to prevent duplicate submissions
236
- st.session_state.query_input = ""
237
 
238
  # Prevent duplicate submissions by checking last message
239
  if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
@@ -269,7 +266,7 @@ def handle_submit():
269
 
270
  finally:
271
  st.session_state.processing_query = False
272
- st.rerun() # Refresh the page to show new messages
273
 
274
  def create_chat_input():
275
  """Create the chat input section with form handling"""
@@ -288,7 +285,7 @@ def create_chat_input():
288
  """, unsafe_allow_html=True)
289
 
290
  # Text input
291
- st.text_input(
292
  "",
293
  key="query_input",
294
  placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
@@ -302,7 +299,7 @@ def create_chat_input():
302
  )
303
 
304
  if submitted:
305
- handle_submit()
306
 
307
  # Clear history button outside the form
308
  with clear_col:
@@ -333,8 +330,8 @@ def main():
333
  if 'context_memory' not in st.session_state:
334
  st.session_state.context_memory = []
335
 
336
- if 'query' not in st.session_state:
337
- st.session_state.query = ''
338
 
339
  # Load QA history at startup
340
  if 'qa_history_loaded' not in st.session_state:
 
224
  if 'context_memory' not in st.session_state:
225
  st.session_state.context_memory = []
226
 
227
+ def handle_submit(user_query: str):
228
  """Handle form submission logic"""
229
+ if not user_query:
230
  st.warning("⚠️ กรุณาระบุคำถาม")
231
  return
232
 
233
+ user_query = user_query.strip()
 
 
 
234
 
235
  # Prevent duplicate submissions by checking last message
236
  if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
 
266
 
267
  finally:
268
  st.session_state.processing_query = False
269
+ st.rerun()
270
 
271
  def create_chat_input():
272
  """Create the chat input section with form handling"""
 
285
  """, unsafe_allow_html=True)
286
 
287
  # Text input
288
+ query = st.text_input(
289
  "",
290
  key="query_input",
291
  placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
 
299
  )
300
 
301
  if submitted:
302
+ handle_submit(query)
303
 
304
  # Clear history button outside the form
305
  with clear_col:
 
330
  if 'context_memory' not in st.session_state:
331
  st.session_state.context_memory = []
332
 
333
+ if 'processing_query' not in st.session_state:
334
+ st.session_state.processing_query = False
335
 
336
  # Load QA history at startup
337
  if 'qa_history_loaded' not in st.session_state: