Update app.py
Browse files
app.py
CHANGED
@@ -273,53 +273,60 @@ def main():
|
|
273 |
</span>
|
274 |
</label>
|
275 |
""", unsafe_allow_html=True)
|
276 |
-
|
277 |
-
#
|
278 |
-
def
|
|
|
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 |
-
#
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
313 |
-
if
|
314 |
if st.session_state.pipeline is None:
|
315 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
316 |
else:
|
317 |
try:
|
318 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
319 |
-
#
|
320 |
-
|
321 |
|
|
|
322 |
result = st.session_state.pipeline.process_query(query)
|
|
|
|
|
|
|
|
|
323 |
add_to_history("assistant", result["answer"])
|
324 |
|
325 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
@@ -334,17 +341,15 @@ def main():
|
|
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
|
345 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
346 |
-
st.session_state.
|
347 |
-
|
348 |
with info_col:
|
349 |
# System information
|
350 |
st.markdown("""
|
|
|
273 |
</span>
|
274 |
</label>
|
275 |
""", unsafe_allow_html=True)
|
276 |
+
|
277 |
+
# Callback function to clear input
|
278 |
+
def clear_input():
|
279 |
+
st.session_state.query_input = ""
|
280 |
st.session_state.chat_history = []
|
|
|
|
|
|
|
|
|
|
|
281 |
|
282 |
+
# Initialize the submit state if not present
|
283 |
+
if 'submit_state' not in st.session_state:
|
284 |
+
st.session_state.submit_state = False
|
285 |
+
|
286 |
+
# Text input with callback
|
287 |
+
query = st.text_input(
|
288 |
+
"",
|
289 |
+
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
290 |
+
key="query_input",
|
291 |
+
on_change=clear_input if st.session_state.get('submit_state', False) else None
|
292 |
+
)
|
293 |
+
|
294 |
+
# Button layout
|
295 |
+
col1, col2, col3 = st.columns([1, 1, 4])
|
296 |
+
|
297 |
+
with col1:
|
298 |
+
send_query = st.button(
|
299 |
+
"📤 ส่งคำถาม",
|
300 |
+
type="primary",
|
301 |
+
use_container_width=True,
|
302 |
+
key="send_query_button"
|
303 |
+
)
|
304 |
+
|
305 |
+
with col2:
|
306 |
+
clear_history = st.button(
|
307 |
+
"🗑️ ล้างประวัติ",
|
308 |
+
type="secondary",
|
309 |
+
use_container_width=True,
|
310 |
+
key="clear_history_button",
|
311 |
+
on_click=clear_input
|
312 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
|
314 |
+
# Process query
|
315 |
+
if send_query and query:
|
316 |
if st.session_state.pipeline is None:
|
317 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
318 |
else:
|
319 |
try:
|
320 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
321 |
+
# Set submit state to trigger input clearing
|
322 |
+
st.session_state.submit_state = True
|
323 |
|
324 |
+
# Process the query
|
325 |
result = st.session_state.pipeline.process_query(query)
|
326 |
+
|
327 |
+
# Clear previous history and add new QA pair
|
328 |
+
st.session_state.chat_history = []
|
329 |
+
add_to_history("user", query)
|
330 |
add_to_history("assistant", result["answer"])
|
331 |
|
332 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
|
|
341 |
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
|
342 |
st.json(result["query_info"])
|
343 |
|
|
|
|
|
344 |
st.rerun()
|
345 |
|
346 |
except Exception as e:
|
347 |
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
348 |
|
349 |
+
elif send_query and not query:
|
350 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
351 |
+
st.session_state.submit_state = False
|
352 |
+
|
353 |
with info_col:
|
354 |
# System information
|
355 |
st.markdown("""
|