Update app.py
Browse files
app.py
CHANGED
@@ -262,31 +262,28 @@ def main():
|
|
262 |
""", unsafe_allow_html=True)
|
263 |
|
264 |
chat_col, info_col = st.columns([7, 3])
|
265 |
-
|
266 |
-
def clear_text():
|
267 |
-
st.session_state.query_input = ''
|
268 |
|
269 |
-
def
|
270 |
-
|
271 |
-
if query:
|
272 |
if st.session_state.pipeline is None:
|
273 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
274 |
return
|
275 |
|
276 |
try:
|
277 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
278 |
-
result = st.session_state.pipeline.process_query(
|
279 |
|
280 |
# Update session state
|
281 |
-
st.session_state.chat_history.append(("user",
|
282 |
st.session_state.chat_history.append(("assistant", {
|
283 |
"answer": result["answer"],
|
284 |
"documents": result["documents"],
|
285 |
"query_info": result["query_info"]
|
286 |
}))
|
287 |
|
288 |
-
#
|
289 |
-
|
|
|
290 |
|
291 |
except Exception as e:
|
292 |
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
@@ -294,36 +291,7 @@ def main():
|
|
294 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
295 |
|
296 |
with chat_col:
|
297 |
-
#
|
298 |
-
with st.container():
|
299 |
-
st.markdown("""
|
300 |
-
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
301 |
-
<span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
|
302 |
-
โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
|
303 |
-
</span>
|
304 |
-
</label>
|
305 |
-
""", unsafe_allow_html=True)
|
306 |
-
|
307 |
-
# Text input
|
308 |
-
query = st.text_input(
|
309 |
-
"",
|
310 |
-
key="query_input",
|
311 |
-
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
312 |
-
)
|
313 |
-
|
314 |
-
# Button layout
|
315 |
-
col1, col2, col3 = st.columns([1, 1, 4])
|
316 |
-
|
317 |
-
with col1:
|
318 |
-
if st.button("📤 ส่งคำถาม", type="primary", use_container_width=True):
|
319 |
-
handle_submit()
|
320 |
-
|
321 |
-
with col2:
|
322 |
-
if st.button("🗑️ ล้างประวัติ", type="secondary", use_container_width=True):
|
323 |
-
st.session_state.chat_history = []
|
324 |
-
st.rerun()
|
325 |
-
|
326 |
-
# Chat history display
|
327 |
for i, (role, content) in enumerate(st.session_state.chat_history):
|
328 |
if role == "user":
|
329 |
st.markdown(f"""
|
@@ -353,7 +321,36 @@ def main():
|
|
353 |
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
|
354 |
st.json(content['query_info'])
|
355 |
|
356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
357 |
with info_col:
|
358 |
st.markdown("""
|
359 |
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|
|
|
262 |
""", unsafe_allow_html=True)
|
263 |
|
264 |
chat_col, info_col = st.columns([7, 3])
|
|
|
|
|
|
|
265 |
|
266 |
+
def submit():
|
267 |
+
if st.session_state.query_input:
|
|
|
268 |
if st.session_state.pipeline is None:
|
269 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
270 |
return
|
271 |
|
272 |
try:
|
273 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
274 |
+
result = st.session_state.pipeline.process_query(st.session_state.query_input)
|
275 |
|
276 |
# Update session state
|
277 |
+
st.session_state.chat_history.append(("user", st.session_state.query_input))
|
278 |
st.session_state.chat_history.append(("assistant", {
|
279 |
"answer": result["answer"],
|
280 |
"documents": result["documents"],
|
281 |
"query_info": result["query_info"]
|
282 |
}))
|
283 |
|
284 |
+
# Reset the input
|
285 |
+
st.session_state.query_input = ""
|
286 |
+
st.rerun()
|
287 |
|
288 |
except Exception as e:
|
289 |
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
|
|
291 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
292 |
|
293 |
with chat_col:
|
294 |
+
# Chat history display first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
for i, (role, content) in enumerate(st.session_state.chat_history):
|
296 |
if role == "user":
|
297 |
st.markdown(f"""
|
|
|
321 |
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
|
322 |
st.json(content['query_info'])
|
323 |
|
324 |
+
# Input section at the bottom
|
325 |
+
with st.container():
|
326 |
+
st.markdown("""
|
327 |
+
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
328 |
+
<span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
|
329 |
+
โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
|
330 |
+
</span>
|
331 |
+
</label>
|
332 |
+
""", unsafe_allow_html=True)
|
333 |
+
|
334 |
+
# Text input
|
335 |
+
st.text_input(
|
336 |
+
"",
|
337 |
+
key="query_input",
|
338 |
+
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
339 |
+
on_change=submit
|
340 |
+
)
|
341 |
+
|
342 |
+
# Button layout
|
343 |
+
col1, col2, col3 = st.columns([1, 1, 4])
|
344 |
+
|
345 |
+
with col1:
|
346 |
+
st.button("📤 ส่งคำถาม", type="primary", use_container_width=True, on_click=submit)
|
347 |
+
|
348 |
+
with col2:
|
349 |
+
if st.button("🗑️ ล้างป���ะวัติ", type="secondary", use_container_width=True):
|
350 |
+
st.session_state.chat_history = []
|
351 |
+
st.rerun()
|
352 |
+
|
353 |
+
# Info column remains the same
|
354 |
with info_col:
|
355 |
st.markdown("""
|
356 |
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|