JirasakJo commited on
Commit
5594779
·
verified ·
1 Parent(s): 470221a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -67
app.py CHANGED
@@ -274,79 +274,76 @@ def main():
274
  </label>
275
  """, unsafe_allow_html=True)
276
 
277
- # Initialize query_input in session state if not present
278
- if 'query_input' not in st.session_state:
279
- st.session_state.query_input = ""
280
-
281
- query = st.text_input(
282
- "",
283
- placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
284
- key="query_input"
285
- )
286
-
287
- # Button layout
288
- col1, col2, col3 = st.columns([1, 1, 4])
289
-
290
- with col1:
291
- send_query = st.button(
292
- "📤 ส่งคำถาม",
293
- type="primary",
294
- use_container_width=True,
295
- key="send_query_button"
296
- )
297
-
298
- with col2:
299
- clear_history = st.button(
300
- "🗑️ ล้างประวัติ",
301
- type="secondary",
302
- use_container_width=True,
303
- key="clear_history_button"
304
  )
305
-
306
- # Process query
307
- if send_query and query:
308
- if st.session_state.pipeline is None:
309
- st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
310
- return
311
 
312
- # Clear previous chat history
313
- st.session_state.chat_history = []
314
 
315
- # Add new query to history
316
- add_to_history("user", query)
 
 
 
 
 
317
 
318
- try:
319
- with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
320
- result = st.session_state.pipeline.process_query(query)
321
- add_to_history("assistant", result["answer"])
322
-
323
- with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
324
- for i, doc in enumerate(result["documents"], 1):
325
- st.markdown(f"""
326
- <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
327
- <strong>เอกสารที่ {i}:</strong><br>
328
- {doc.content}
329
- </div>
330
- """, unsafe_allow_html=True)
331
-
332
- with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
333
- st.json(result["query_info"])
334
-
335
- # Clear the input field
336
- st.session_state.query_input = ""
337
- st.rerun()
338
-
339
- except Exception as e:
340
- st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
 
342
- elif send_query and not query:
343
  st.warning("⚠️ กรุณาระบุคำถาม")
344
-
345
- # Handle clear history action
346
- if clear_history:
347
- st.session_state.chat_history = []
348
- st.session_state.query_input = ""
349
- st.rerun()
350
 
351
  with info_col:
352
  # System information
 
274
  </label>
275
  """, unsafe_allow_html=True)
276
 
277
+ # Function to handle form submission
278
+ def on_submit():
279
+ st.session_state.chat_history = []
280
+ st.session_state.submitted = True
281
+
282
+ # Initialize the form submit state if not present
283
+ if 'submitted' not in st.session_state:
284
+ st.session_state.submitted = False
285
+
286
+ # Create form
287
+ with st.form(key='query_form'):
288
+ query = st.text_input(
289
+ "",
290
+ placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
291
+ key="query_input"
 
 
 
 
 
 
 
 
 
 
 
 
292
  )
 
 
 
 
 
 
293
 
294
+ col1, col2, col3 = st.columns([1, 1, 4])
 
295
 
296
+ with col1:
297
+ submit_button = st.form_submit_button(
298
+ "📤 ส่งคำถาม",
299
+ type="primary",
300
+ on_click=on_submit,
301
+ use_container_width=True
302
+ )
303
 
304
+ with col2:
305
+ clear_button = st.form_submit_button(
306
+ "🗑️ ล้างประวัติ",
307
+ type="secondary",
308
+ on_click=lambda: setattr(st.session_state, 'chat_history', []),
309
+ use_container_width=True
310
+ )
311
+
312
+ # Process query when form is submitted
313
+ if st.session_state.submitted and query:
314
+ if st.session_state.pipeline is None:
315
+ st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
316
+ else:
317
+ try:
318
+ with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
319
+ # Add new query to history
320
+ add_to_history("user", query)
321
+
322
+ result = st.session_state.pipeline.process_query(query)
323
+ add_to_history("assistant", result["answer"])
324
+
325
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
326
+ for i, doc in enumerate(result["documents"], 1):
327
+ st.markdown(f"""
328
+ <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
329
+ <strong>เอกสารที่ {i}:</strong><br>
330
+ {doc.content}
331
+ </div>
332
+ """, unsafe_allow_html=True)
333
+
334
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
335
+ st.json(result["query_info"])
336
+
337
+ # Reset submit state
338
+ st.session_state.submitted = False
339
+ st.rerun()
340
+
341
+ except Exception as e:
342
+ st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
343
 
344
+ elif st.session_state.submitted and not query:
345
  st.warning("⚠️ กรุณาระบุคำถาม")
346
+ st.session_state.submitted = False
 
 
 
 
 
347
 
348
  with info_col:
349
  # System information