JirasakJo commited on
Commit
5eda752
·
verified ·
1 Parent(s): 807291f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -87
app.py CHANGED
@@ -226,33 +226,38 @@ def submit():
226
  if st.session_state.pipeline is None:
227
  st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
228
  return
229
-
 
 
 
230
  try:
231
- st.session_state.loading = True
232
- st.rerun() # Trigger rerun to show loading state
233
-
234
- result = st.session_state.pipeline.process_query(st.session_state.query_input)
235
-
236
- # Update session state
237
- st.session_state.chat_history.append(("user", st.session_state.query_input))
 
 
 
 
 
 
238
  st.session_state.chat_history.append(("assistant", {
239
  "answer": result["answer"],
240
  "documents": result["documents"],
241
  "query_info": result["query_info"]
242
  }))
243
-
244
- # Reset states
245
  st.session_state.query_input = ""
246
- st.session_state.loading = False
247
-
248
- # Rerun to update the UI
249
- st.rerun()
250
-
251
  except Exception as e:
252
  st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
253
- st.session_state.loading = False
254
  else:
255
  st.warning("⚠️ กรุณาระบุคำถาม")
 
256
 
257
  def main():
258
  # Page config
@@ -275,9 +280,6 @@ def main():
275
 
276
  if 'query' not in st.session_state:
277
  st.session_state.query = ''
278
-
279
- if 'loading' not in st.session_state:
280
- st.session_state.loading = False
281
 
282
  # Load QA history at startup
283
  if 'qa_history_loaded' not in st.session_state:
@@ -300,74 +302,55 @@ def main():
300
  chat_col, info_col = st.columns([7, 3])
301
 
302
  with chat_col:
303
- # Chat container for displaying messages
304
- chat_container = st.container()
305
-
306
- # Input container at the bottom
307
- input_container = st.container()
308
-
309
- # Display chat history in chat container
310
- with chat_container:
311
- for i, (role, content) in enumerate(st.session_state.chat_history):
312
- if role == "user":
313
- st.markdown(f"""
314
- <div class="chat-message user-message">
315
- <strong>🧑 คำถาม:</strong><br>
316
- {content}
317
- </div>
318
- """, unsafe_allow_html=True)
319
- else:
320
- st.markdown(f"""
321
- <div class="chat-message assistant-message">
322
- <strong>🤖 คำตอบ:</strong><br>
323
- {content['answer']}
324
- </div>
325
- """, unsafe_allow_html=True)
326
-
327
- # Add expanders right after the answer
328
- with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
329
- for i, doc in enumerate(content['documents'], 1):
330
- st.markdown(f"""
331
- <div style="padding: 1rem; background-color: #F3F4F6; border-radius: 8px; margin: 0.5rem 0;">
332
- <strong>เอกสารที่ {i}:</strong><br>
333
- {doc.content}
334
- </div>
335
- """, unsafe_allow_html=True)
336
-
337
- with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
338
- st.json(content['query_info'])
339
 
340
- # Input section
341
- with input_container:
 
342
  st.markdown("""
343
- <div style="margin-top: 2rem;">
344
- <label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
345
- <span style="color: #1F2937; border-left: 4px solid #2563EB; padding-left: 0.8rem;">
346
- โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
347
- </span>
348
- </label>
349
- </div>
350
  """, unsafe_allow_html=True)
351
 
352
- # Loading indicator placeholder
353
- loading_placeholder = st.empty()
354
-
355
  # Text input with enter key handling
356
- query = st.text_input(
357
  "",
358
  key="query_input",
359
  placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
360
  on_change=submit
361
  )
362
 
363
- # Show loading indicator in the correct position
364
- if st.session_state.get('loading', False):
365
- loading_placeholder.markdown("""
366
- <div style="color: #2563EB; margin: 0.5rem 0;">
367
- 🔍 กำลังค้นหาคำตอบ...
368
- </div>
369
- """, unsafe_allow_html=True)
370
-
371
  # Button layout
372
  col1, col2, col3 = st.columns([1, 1, 4])
373
 
@@ -379,33 +362,33 @@ def main():
379
  st.session_state.chat_history = []
380
  st.rerun()
381
 
382
- # Info column
383
  with info_col:
384
  st.markdown("""
385
  <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
386
  <h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
387
- <p style="color: #1F2937;">
388
  ระบบนี้ใช้เทคโนโลยี <strong>RAG (Retrieval-Augmented Generation)</strong>
389
  ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
390
  </p>
391
  <h4 style="color: #1E3A8A; margin-top: 1rem;">สามารถสอบถามข้อมูลเกี่ยวกับ:</h4>
392
  <ul style="list-style-type: none; padding-left: 0;">
393
- <li style="color: #1F2937; margin-bottom: 0.5rem;">📅 กำหนดการต่างๆ ในปฏิทินการศึกษา</li>
394
- <li style="color: #1F2937; margin-bottom: 0.5rem;">🎯 วันสำคัญและกิจกรรม</li>
395
- <li style="color: #1F2937; margin-bottom: 0.5rem;">📝 การลงทะเบียนเรียน</li>
396
- <li style="color: #1F2937; margin-bottom: 0.5rem;">📚 กำหนดการสอบ</li>
397
- <li style="color: #1F2937; margin-bottom: 0.5rem;">🏖️ วันหยุดการศึกษา</li>
398
  </ul>
399
  </div>
400
  """, unsafe_allow_html=True)
401
 
402
  st.markdown("""
403
- <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px;">
404
  <h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>
405
  <div style="margin-top: 1rem;">
406
- <p><strong style="color: #1F2937;">⏰ เวลาปัจจุบัน:</strong><br>
407
- <span style="color: #1F2937;">{}</span></p>
408
- <p><strong style="color: #1F2937;">📡 สถานะระบบ:</strong><br>
409
  <span class="status-indicator {}">
410
  {} {}
411
  </span></p>
 
226
  if st.session_state.pipeline is None:
227
  st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
228
  return
229
+
230
+ # Add user query to chat history first (so the spinner appears after it)
231
+ st.session_state.chat_history.append(("user", st.session_state.query_input))
232
+
233
  try:
234
+ # Display spinner in the correct position (after user input)
235
+ with st.container():
236
+ st.markdown(f"""
237
+ <div class="chat-message user-message">
238
+ <strong>🧑 คำถาม:</strong><br>
239
+ {st.session_state.query_input}
240
+ </div>
241
+ """, unsafe_allow_html=True)
242
+
243
+ with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
244
+ result = st.session_state.pipeline.process_query(st.session_state.query_input)
245
+
246
+ # Append the assistant's response
247
  st.session_state.chat_history.append(("assistant", {
248
  "answer": result["answer"],
249
  "documents": result["documents"],
250
  "query_info": result["query_info"]
251
  }))
252
+
253
+ # Reset input
254
  st.session_state.query_input = ""
255
+
 
 
 
 
256
  except Exception as e:
257
  st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
 
258
  else:
259
  st.warning("⚠️ กรุณาระบุคำถาม")
260
+
261
 
262
  def main():
263
  # Page config
 
280
 
281
  if 'query' not in st.session_state:
282
  st.session_state.query = ''
 
 
 
283
 
284
  # Load QA history at startup
285
  if 'qa_history_loaded' not in st.session_state:
 
302
  chat_col, info_col = st.columns([7, 3])
303
 
304
  with chat_col:
305
+ # Chat history display first
306
+ for i, (role, content) in enumerate(st.session_state.chat_history):
307
+ if role == "user":
308
+ st.markdown(f"""
309
+ <div class="chat-message user-message">
310
+ <strong>🧑 คำถาม:</strong><br>
311
+ {content}
312
+ </div>
313
+ """, unsafe_allow_html=True)
314
+ else:
315
+ st.markdown(f"""
316
+ <div class="chat-message assistant-message">
317
+ <strong>🤖 คำตอบ:</strong><br>
318
+ {content['answer']}
319
+ </div>
320
+ """, unsafe_allow_html=True)
321
+
322
+ # Add expanders right after the answer
323
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
324
+ for i, doc in enumerate(content['documents'], 1):
325
+ st.markdown(f"""
326
+ <div style="padding: 1rem; background-color: #000000; border-radius: 8px; margin: 0.5rem 0;">
327
+ <strong>เอกสารที่ {i}:</strong><br>
328
+ {doc.content}
329
+ </div>
330
+ """, unsafe_allow_html=True)
331
+
332
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
333
+ st.json(content['query_info'])
 
 
 
 
 
 
 
334
 
335
+ # Input section at the bottom
336
+ with st.container():
337
+ # Replace this section in your code:
338
  st.markdown("""
339
+ <label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
340
+ <span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
341
+ โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
342
+ </span>
343
+ </label>
 
 
344
  """, unsafe_allow_html=True)
345
 
 
 
 
346
  # Text input with enter key handling
347
+ st.text_input(
348
  "",
349
  key="query_input",
350
  placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
351
  on_change=submit
352
  )
353
 
 
 
 
 
 
 
 
 
354
  # Button layout
355
  col1, col2, col3 = st.columns([1, 1, 4])
356
 
 
362
  st.session_state.chat_history = []
363
  st.rerun()
364
 
365
+ # Info column remains the same
366
  with info_col:
367
  st.markdown("""
368
  <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
369
  <h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
370
+ <p style="color: #000000;">
371
  ระบบนี้ใช้เทคโนโลยี <strong>RAG (Retrieval-Augmented Generation)</strong>
372
  ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
373
  </p>
374
  <h4 style="color: #1E3A8A; margin-top: 1rem;">สามารถสอบถามข้อมูลเกี่ยวกับ:</h4>
375
  <ul style="list-style-type: none; padding-left: 0;">
376
+ <li style="color: #000000; margin-bottom: 0.5rem;">📅 กำ���นดการต่างๆ ในปฏิทินการศึกษา</li>
377
+ <li style="color: #000000; margin-bottom: 0.5rem;">🎯 วันสำคัญและกิจกรรม</li>
378
+ <li style="color: #000000; margin-bottom: 0.5rem;">📝 การลงทะเบียนเรียน</li>
379
+ <li style="color: #000000; margin-bottom: 0.5rem;">📚 กำหนดการสอบ</li>
380
+ <li style="color: #000000; margin-bottom: 0.5rem;">🏖️ วันหยุดการศึกษา</li>
381
  </ul>
382
  </div>
383
  """, unsafe_allow_html=True)
384
 
385
  st.markdown("""
386
+ <div style="background-color: #f9fafb; padding: 1.5rem; border-radius: 12px;">
387
  <h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>
388
  <div style="margin-top: 1rem;">
389
+ <p><strong style="color: #000000;">⏰ เวลาปัจจุบัน:</strong><br>
390
+ <span style="color: #000000;">{}</span></p>
391
+ <p><strong style="color: #000000;">📡 สถานะระบบ:</strong><br>
392
  <span class="status-indicator {}">
393
  {} {}
394
  </span></p>