Update app.py
Browse files
app.py
CHANGED
@@ -274,79 +274,76 @@ def main():
|
|
274 |
</label>
|
275 |
""", unsafe_allow_html=True)
|
276 |
|
277 |
-
#
|
278 |
-
|
279 |
-
st.session_state.
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
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 |
-
|
313 |
-
st.session_state.chat_history = []
|
314 |
|
315 |
-
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
317 |
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
with st.
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
341 |
|
342 |
-
elif
|
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
|