JirasakJo commited on
Commit
1df6513
·
verified ·
1 Parent(s): 867ae3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -64
app.py CHANGED
@@ -274,76 +274,92 @@ def main():
274
  </label>
275
  """, unsafe_allow_html=True)
276
 
277
- # Initialize the query key in session state if it doesn't exist
278
  if 'query' not in st.session_state:
279
  st.session_state.query = ''
280
 
281
- # Create the text input with a key
282
- query = st.text_input(
283
- "",
284
- value=st.session_state.query,
285
- placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
286
- key="query_input"
287
- )
288
-
289
- # Button layout
290
- col1, col2, col3 = st.columns([1, 1, 4])
291
-
292
- with col1:
293
- send_query = st.button(
294
- "📤 ส่งคำถาม",
295
- type="primary",
296
- use_container_width=True,
297
- key="send_query_button"
298
- )
299
-
300
- with col2:
301
- clear_history = st.button(
302
- "🗑️ ล้างประวัติ",
303
- type="secondary",
304
- use_container_width=True,
305
- key="clear_history_button"
306
- )
307
-
308
- # Process query
309
- if send_query and query:
310
- if st.session_state.pipeline is None:
311
- st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
312
- return
313
-
314
- add_to_history("user", query)
315
-
316
- try:
317
- with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
318
- result = st.session_state.pipeline.process_query(query)
319
- add_to_history("assistant", result["answer"])
320
-
321
- with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
322
- for i, doc in enumerate(result["documents"], 1):
323
- st.markdown(f"""
324
- <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
325
- <strong>เอกสารที่ {i}:</strong><br>
326
- {doc.content}
327
- </div>
328
- """, unsafe_allow_html=True)
329
-
330
- with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
331
- st.json(result["query_info"])
332
 
333
- # Clear the input after processing
334
- st.session_state.query = ''
335
- st.rerun()
336
 
337
- except Exception as e:
338
- st.error(f" เกิดข้อผิดพลาด: {str(e)}")
339
-
340
- elif send_query and not query:
341
- st.warning("⚠️ กรุณาระบุคำถาม")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
- # Handle clear history action
344
- if clear_history:
345
- st.session_state.chat_history = []
346
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
  with info_col:
349
  # System information
 
274
  </label>
275
  """, unsafe_allow_html=True)
276
 
277
+ # Initialize session states
278
  if 'query' not in st.session_state:
279
  st.session_state.query = ''
280
 
281
+ def clear_text():
282
+ st.session_state.query = ''
283
+ st.session_state.query_input = ''
284
+
285
+ def handle_submit():
286
+ # Get the current query value
287
+ query = st.session_state.query_input
288
+ if query:
289
+ if st.session_state.pipeline is None:
290
+ st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
291
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
+ add_to_history("user", query)
 
 
294
 
295
+ try:
296
+ with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
297
+ result = st.session_state.pipeline.process_query(query)
298
+ add_to_history("assistant", result["answer"])
299
+
300
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
301
+ for i, doc in enumerate(result["documents"], 1):
302
+ st.markdown(f"""
303
+ <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
304
+ <strong>เอกสารที่ {i}:</strong><br>
305
+ {doc.content}
306
+ </div>
307
+ """, unsafe_allow_html=True)
308
+
309
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
310
+ st.json(result["query_info"])
311
+
312
+ # Clear the input
313
+ clear_text()
314
+ st.rerun()
315
+
316
+ except Exception as e:
317
+ st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
318
+ else:
319
+ st.warning("⚠️ กรุณาระบุคำถาม")
320
 
321
+ chat_col, info_col = st.columns([7, 3])
322
+
323
+ with chat_col:
324
+ with st.container():
325
+ # Display chat history
326
+ display_chat_history()
327
+
328
+ # Query input section
329
+ st.markdown("""
330
+ <label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
331
+ <span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
332
+ โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
333
+ </span>
334
+ </label>
335
+ """, unsafe_allow_html=True)
336
+
337
+ # Text input with callback
338
+ query = st.text_input(
339
+ "",
340
+ key="query_input",
341
+ placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
342
+ on_change=handle_submit
343
+ )
344
+
345
+ # Button layout
346
+ col1, col2, col3 = st.columns([1, 1, 4])
347
+
348
+ with col1:
349
+ st.button(
350
+ "📤 ส่งคำถาม",
351
+ type="primary",
352
+ use_container_width=True,
353
+ on_click=handle_submit
354
+ )
355
+
356
+ with col2:
357
+ st.button(
358
+ "🗑️ ล้างประวัติ",
359
+ type="secondary",
360
+ use_container_width=True,
361
+ on_click=lambda: setattr(st.session_state, 'chat_history', []) or st.rerun()
362
+ )
363
 
364
  with info_col:
365
  # System information