JirasakJo commited on
Commit
58c536d
·
verified ·
1 Parent(s): 1df6513

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -85
app.py CHANGED
@@ -239,6 +239,9 @@ def main():
239
 
240
  if 'chat_history' not in st.session_state:
241
  st.session_state.chat_history = []
 
 
 
242
 
243
  # Load QA history at startup
244
  if 'qa_history_loaded' not in st.session_state:
@@ -258,6 +261,46 @@ def main():
258
  with st.spinner("กำลังเริ่มต้นระบบ..."):
259
  st.session_state.pipeline = initialize_pipeline()
260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  chat_col, info_col = st.columns([7, 3])
262
 
263
  with chat_col:
@@ -274,92 +317,32 @@ def main():
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
@@ -395,7 +378,7 @@ def main():
395
  </div>
396
  </div>
397
  """.format(
398
- (datetime.now() + timedelta(hours=6)).strftime('%Y-%m-%d %H:%M:%S'),
399
  "status-online" if st.session_state.pipeline else "status-offline",
400
  "🟢" if st.session_state.pipeline else "🔴",
401
  "พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"
 
239
 
240
  if 'chat_history' not in st.session_state:
241
  st.session_state.chat_history = []
242
+
243
+ if 'query' not in st.session_state:
244
+ st.session_state.query = ''
245
 
246
  # Load QA history at startup
247
  if 'qa_history_loaded' not in st.session_state:
 
261
  with st.spinner("กำลังเริ่มต้นระบบ..."):
262
  st.session_state.pipeline = initialize_pipeline()
263
 
264
+ def clear_text():
265
+ st.session_state.query = ''
266
+ st.session_state.query_input = ''
267
+
268
+ def handle_submit():
269
+ # Get the current query value
270
+ query = st.session_state.query_input
271
+ if query:
272
+ if st.session_state.pipeline is None:
273
+ st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
274
+ return
275
+
276
+ add_to_history("user", query)
277
+
278
+ try:
279
+ with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
280
+ result = st.session_state.pipeline.process_query(query)
281
+ add_to_history("assistant", result["answer"])
282
+
283
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
284
+ for i, doc in enumerate(result["documents"], 1):
285
+ st.markdown(f"""
286
+ <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
287
+ <strong>เอกสารที่ {i}:</strong><br>
288
+ {doc.content}
289
+ </div>
290
+ """, unsafe_allow_html=True)
291
+
292
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
293
+ st.json(result["query_info"])
294
+
295
+ # Clear the input
296
+ clear_text()
297
+ st.rerun()
298
+
299
+ except Exception as e:
300
+ st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
301
+ else:
302
+ st.warning("⚠️ กรุณาระบุคำถาม")
303
+
304
  chat_col, info_col = st.columns([7, 3])
305
 
306
  with chat_col:
 
317
  </label>
318
  """, unsafe_allow_html=True)
319
 
320
+ # Create the text input with callbacks
321
+ query = st.text_input(
322
+ "",
323
+ key="query_input",
324
+ placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
325
+ on_change=handle_submit
326
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
+ # Button layout
329
+ col1, col2, col3 = st.columns([1, 1, 4])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
 
331
+ with col1:
332
+ st.button(
333
+ "📤 ส่งคำถาม",
334
+ type="primary",
335
+ use_container_width=True,
336
+ on_click=handle_submit
337
+ )
338
+
339
+ with col2:
340
+ st.button(
341
+ "🗑️ ล้างประวัติ",
342
+ type="secondary",
343
+ use_container_width=True,
344
+ on_click=lambda: setattr(st.session_state, 'chat_history', []) or st.rerun()
345
+ )
 
 
 
346
 
347
  with info_col:
348
  # System information
 
378
  </div>
379
  </div>
380
  """.format(
381
+ (datetime.now() + timedelta(hours=7)).strftime('%Y-%m-%d %H:%M:%S'),
382
  "status-online" if st.session_state.pipeline else "status-offline",
383
  "🟢" if st.session_state.pipeline else "🔴",
384
  "พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"