JirasakJo commited on
Commit
2836420
·
verified ·
1 Parent(s): 1192217

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -274,6 +274,10 @@ def main():
274
  </label>
275
  """, unsafe_allow_html=True)
276
 
 
 
 
 
277
  query = st.text_input(
278
  "",
279
  placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
@@ -282,7 +286,7 @@ def main():
282
 
283
  # Button layout inside main() function
284
  col1, col2, col3 = st.columns([1, 1, 4])
285
-
286
  with col1:
287
  send_query = st.button(
288
  "📤 ส่งคำถาม",
@@ -290,7 +294,7 @@ def main():
290
  use_container_width=True,
291
  key="send_query_button"
292
  )
293
-
294
  with col2:
295
  clear_history = st.button(
296
  "🗑️ ล้างประวัติ",
@@ -298,35 +302,34 @@ def main():
298
  use_container_width=True,
299
  key="clear_history_button"
300
  )
 
301
  # Process query
302
  if send_query and query:
303
  if st.session_state.pipeline is None:
304
  st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
305
- return
306
-
307
- add_to_history("user", query)
308
-
309
- try:
310
- with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
311
- result = st.session_state.pipeline.process_query(query)
312
- add_to_history("assistant", result["answer"])
313
-
314
- with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
315
- for i, doc in enumerate(result["documents"], 1):
316
- st.markdown(f"""
317
- <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
318
- <strong>เอกสารที่ {i}:</strong><br>
319
- {doc.content}
320
- </div>
321
- """, unsafe_allow_html=True)
322
-
323
- with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
324
- st.json(result["query_info"])
325
-
326
- st.rerun()
327
-
328
- except Exception as e:
329
- st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
330
 
331
  elif send_query and not query:
332
  st.warning("⚠️ กรุณาระบุคำถาม")
 
274
  </label>
275
  """, unsafe_allow_html=True)
276
 
277
+ # Initialize the session state for the input box if not set
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 คือวันที่เท่าไร?",
 
286
 
287
  # Button layout inside main() function
288
  col1, col2, col3 = st.columns([1, 1, 4])
289
+
290
  with col1:
291
  send_query = st.button(
292
  "📤 ส่งคำถาม",
 
294
  use_container_width=True,
295
  key="send_query_button"
296
  )
297
+
298
  with col2:
299
  clear_history = st.button(
300
  "🗑️ ล้างประวัติ",
 
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
+ else:
311
+ add_to_history("user", query)
312
+ try:
313
+ with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
314
+ result = st.session_state.pipeline.process_query(query)
315
+ add_to_history("assistant", result["answer"])
316
+ st.session_state.query_input = "" # Clear input after submission
317
+
318
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
319
+ for i, doc in enumerate(result["documents"], 1):
320
+ st.markdown(f"""
321
+ <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
322
+ <strong>เอกสารที่ {i}:</strong><br>
323
+ {doc.content}
324
+ </div>
325
+ """, unsafe_allow_html=True)
326
+
327
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
328
+ st.json(result["query_info"])
329
+
330
+ st.rerun()
331
+ except Exception as e:
332
+ st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
 
 
333
 
334
  elif send_query and not query:
335
  st.warning("⚠️ กรุณาระบุคำถาม")