Update app.py
Browse files
app.py
CHANGED
|
@@ -274,13 +274,16 @@ def main():
|
|
| 274 |
</label>
|
| 275 |
""", unsafe_allow_html=True)
|
| 276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
query = st.text_input(
|
| 278 |
"",
|
| 279 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
| 280 |
-
key="query_input"
|
| 281 |
-
value="" if 'clear_input' in st.session_state and st.session_state.clear_input else st.session_state.get('query_input', '')
|
| 282 |
)
|
| 283 |
-
|
| 284 |
# Button layout
|
| 285 |
col1, col2, col3 = st.columns([1, 1, 4])
|
| 286 |
|
|
@@ -299,15 +302,19 @@ def main():
|
|
| 299 |
use_container_width=True,
|
| 300 |
key="clear_history_button"
|
| 301 |
)
|
| 302 |
-
|
| 303 |
# Process query
|
| 304 |
if send_query and query:
|
| 305 |
if st.session_state.pipeline is None:
|
| 306 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
| 307 |
return
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
add_to_history("user", query)
|
| 310 |
-
|
| 311 |
try:
|
| 312 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
| 313 |
result = st.session_state.pipeline.process_query(query)
|
|
@@ -325,25 +332,21 @@ def main():
|
|
| 325 |
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
|
| 326 |
st.json(result["query_info"])
|
| 327 |
|
| 328 |
-
# Clear the input
|
| 329 |
-
st.session_state.
|
| 330 |
st.rerun()
|
| 331 |
|
| 332 |
except Exception as e:
|
| 333 |
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
| 334 |
-
|
| 335 |
elif send_query and not query:
|
| 336 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
| 337 |
-
|
| 338 |
# Handle clear history action
|
| 339 |
if clear_history:
|
| 340 |
st.session_state.chat_history = []
|
| 341 |
-
st.session_state.
|
| 342 |
st.rerun()
|
| 343 |
-
|
| 344 |
-
# Reset clear_input flag at the end of each render
|
| 345 |
-
if 'clear_input' in st.session_state:
|
| 346 |
-
del st.session_state.clear_input
|
| 347 |
|
| 348 |
with info_col:
|
| 349 |
# System information
|
|
|
|
| 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 |
|
|
|
|
| 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)
|
|
|
|
| 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
|