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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -47
app.py CHANGED
@@ -233,7 +233,7 @@ def main():
233
  # Load custom CSS
234
  load_custom_css()
235
 
236
- # Initialize session state
237
  if 'pipeline' not in st.session_state:
238
  st.session_state.pipeline = None
239
 
@@ -248,6 +248,11 @@ def main():
248
  st.session_state.qa_history_loaded = True
249
  load_qa_history()
250
 
 
 
 
 
 
251
  # Header
252
  st.markdown("""
253
  <div style="text-align: center; padding: 2rem 0;">
@@ -256,59 +261,41 @@ def main():
256
  </div>
257
  """, unsafe_allow_html=True)
258
 
259
- # Initialize pipeline
260
- if st.session_state.pipeline is None:
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:
 
307
  with st.container():
308
- # Display chat history
309
- display_chat_history()
310
-
311
- # Query input section
312
  st.markdown("""
313
  <label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
314
  <span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
@@ -317,35 +304,57 @@ def main():
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
349
  st.markdown("""
350
  <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
351
  <h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
@@ -364,7 +373,6 @@ def main():
364
  </div>
365
  """, unsafe_allow_html=True)
366
 
367
- # System status
368
  st.markdown("""
369
  <div style="background-color: #f9fafb; padding: 1.5rem; border-radius: 12px;">
370
  <h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>
 
233
  # Load custom CSS
234
  load_custom_css()
235
 
236
+ # Initialize session states
237
  if 'pipeline' not in st.session_state:
238
  st.session_state.pipeline = None
239
 
 
248
  st.session_state.qa_history_loaded = True
249
  load_qa_history()
250
 
251
+ # Initialize pipeline
252
+ if st.session_state.pipeline is None:
253
+ with st.spinner("กำลังเริ่มต้นระบบ..."):
254
+ st.session_state.pipeline = initialize_pipeline()
255
+
256
  # Header
257
  st.markdown("""
258
  <div style="text-align: center; padding: 2rem 0;">
 
261
  </div>
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 handle_submit():
 
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
  try:
277
  with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
278
  result = st.session_state.pipeline.process_query(query)
 
 
 
 
 
 
 
 
 
 
279
 
280
+ # Update session state
281
+ st.session_state.chat_history.append(("user", query))
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
+ # Clear input
289
  clear_text()
 
290
 
291
  except Exception as e:
292
  st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
293
  else:
294
  st.warning("⚠️ กรุณาระบุคำถาม")
295
 
 
 
296
  with chat_col:
297
+ # Input section at the top
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;">
 
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"""
330
+ <div class="chat-message user-message">
331
+ <strong>🧑 คำถาม:</strong><br>
332
+ {content}
333
+ </div>
334
+ """, unsafe_allow_html=True)
335
+ else:
336
+ st.markdown(f"""
337
+ <div class="chat-message assistant-message">
338
+ <strong>🤖 คำตอบ:</strong><br>
339
+ {content['answer']}
340
+ </div>
341
+ """, unsafe_allow_html=True)
342
+
343
+ # Add expanders right after the answer
344
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
345
+ for i, doc in enumerate(content['documents'], 1):
346
+ st.markdown(f"""
347
+ <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
348
+ <strong>เอกสารที่ {i}:</strong><br>
349
+ {doc.content}
350
+ </div>
351
+ """, unsafe_allow_html=True)
352
+
353
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
354
+ st.json(content['query_info'])
355
 
356
+ # Info column content remains the same
357
  with info_col:
 
358
  st.markdown("""
359
  <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
360
  <h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
 
373
  </div>
374
  """, unsafe_allow_html=True)
375
 
 
376
  st.markdown("""
377
  <div style="background-color: #f9fafb; padding: 1.5rem; border-radius: 12px;">
378
  <h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>