Update app.py
Browse files
app.py
CHANGED
@@ -224,42 +224,87 @@ def display_chat_history():
|
|
224 |
if 'context_memory' not in st.session_state:
|
225 |
st.session_state.context_memory = []
|
226 |
|
227 |
-
def
|
|
|
228 |
if not st.session_state.query_input:
|
229 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
230 |
return
|
231 |
|
232 |
-
user_query = st.session_state.query_input.strip()
|
233 |
-
|
234 |
-
|
235 |
-
|
|
|
|
|
236 |
if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
|
261 |
-
|
262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
|
264 |
def main():
|
265 |
# Page config
|
@@ -307,7 +352,7 @@ def main():
|
|
307 |
chat_col, info_col = st.columns([7, 3])
|
308 |
|
309 |
with chat_col:
|
310 |
-
#
|
311 |
for i, (role, content) in enumerate(st.session_state.chat_history):
|
312 |
if role == "user":
|
313 |
st.markdown(f"""
|
@@ -316,9 +361,7 @@ def main():
|
|
316 |
{content}
|
317 |
</div>
|
318 |
""", unsafe_allow_html=True)
|
319 |
-
|
320 |
else:
|
321 |
-
# ✅ FIX: Handle both string and dictionary content safely
|
322 |
if isinstance(content, dict):
|
323 |
assistant_response = content.get('answer', '❌ ไม่มีข้อมูลคำตอบ')
|
324 |
else:
|
@@ -331,9 +374,7 @@ def main():
|
|
331 |
</div>
|
332 |
""", unsafe_allow_html=True)
|
333 |
|
334 |
-
# Add expanders right after the answer (if additional document references exist)
|
335 |
if isinstance(content, dict) and content.get('documents'):
|
336 |
-
# Add expanders right after the answer
|
337 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
338 |
for i, doc in enumerate(content['documents'], 1):
|
339 |
st.markdown(f"""
|
@@ -343,6 +384,9 @@ def main():
|
|
343 |
</div>
|
344 |
""", unsafe_allow_html=True)
|
345 |
|
|
|
|
|
|
|
346 |
# Input section at the bottom
|
347 |
with st.container():
|
348 |
st.markdown("""
|
|
|
224 |
if 'context_memory' not in st.session_state:
|
225 |
st.session_state.context_memory = []
|
226 |
|
227 |
+
def handle_submit():
|
228 |
+
"""Handle form submission logic"""
|
229 |
if not st.session_state.query_input:
|
230 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
231 |
return
|
232 |
|
233 |
+
user_query = st.session_state.query_input.strip()
|
234 |
+
|
235 |
+
# Important: Clear input immediately to prevent duplicate submissions
|
236 |
+
st.session_state.query_input = ""
|
237 |
+
|
238 |
+
# Prevent duplicate submissions by checking last message
|
239 |
if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
|
240 |
+
try:
|
241 |
+
st.session_state.processing_query = True
|
242 |
+
|
243 |
+
# Add user message to chat history
|
244 |
+
st.session_state.chat_history.append(("user", user_query))
|
245 |
+
|
246 |
+
# Maintain context memory
|
247 |
+
if len(st.session_state.context_memory) > 5:
|
248 |
+
st.session_state.context_memory.pop(0)
|
249 |
+
|
250 |
+
# Build query with context
|
251 |
+
query_with_context = "\n".join(
|
252 |
+
[f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
|
253 |
+
) + f"\nQ: {user_query}"
|
254 |
+
|
255 |
+
# Process query
|
256 |
+
result = st.session_state.pipeline.process_query(query_with_context)
|
257 |
+
assistant_answer = result["answer"]
|
258 |
+
|
259 |
+
# Update chat history and context
|
260 |
+
st.session_state.chat_history.append(("assistant", assistant_answer))
|
261 |
+
st.session_state.context_memory.append({"query": user_query, "answer": assistant_answer})
|
262 |
+
|
263 |
+
# Save to QA history
|
264 |
+
add_to_qa_history(user_query, assistant_answer)
|
265 |
+
|
266 |
+
except Exception as e:
|
267 |
+
st.session_state.chat_history.append(("assistant", f"❌ เกิดข้อผิดพลาด: {str(e)}"))
|
268 |
+
st.error(f"Query processing error: {e}")
|
269 |
+
|
270 |
+
finally:
|
271 |
+
st.session_state.processing_query = False
|
272 |
+
st.rerun() # Refresh the page to show new messages
|
273 |
|
274 |
+
def create_chat_input():
|
275 |
+
"""Create the chat input section with form handling"""
|
276 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
277 |
+
st.markdown("""
|
278 |
+
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
279 |
+
<span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
|
280 |
+
โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
|
281 |
+
</span>
|
282 |
+
</label>
|
283 |
+
""", unsafe_allow_html=True)
|
284 |
+
|
285 |
+
# Text input
|
286 |
+
st.text_input(
|
287 |
+
"",
|
288 |
+
key="query_input",
|
289 |
+
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
|
290 |
+
)
|
291 |
+
|
292 |
+
# Button layout
|
293 |
+
col1, col2, col3 = st.columns([1, 1, 4])
|
294 |
+
|
295 |
+
with col1:
|
296 |
+
submit_button = st.form_submit_button(
|
297 |
+
"📤 ส่งคำถาม",
|
298 |
+
type="primary",
|
299 |
+
use_container_width=True,
|
300 |
+
on_click=handle_submit
|
301 |
+
)
|
302 |
+
|
303 |
+
with col2:
|
304 |
+
if st.button("🗑️ ล้างประวัติ", type="secondary", use_container_width=True):
|
305 |
+
st.session_state.context_memory = []
|
306 |
+
st.session_state.chat_history = []
|
307 |
+
st.rerun()
|
308 |
|
309 |
def main():
|
310 |
# Page config
|
|
|
352 |
chat_col, info_col = st.columns([7, 3])
|
353 |
|
354 |
with chat_col:
|
355 |
+
# Display chat history
|
356 |
for i, (role, content) in enumerate(st.session_state.chat_history):
|
357 |
if role == "user":
|
358 |
st.markdown(f"""
|
|
|
361 |
{content}
|
362 |
</div>
|
363 |
""", unsafe_allow_html=True)
|
|
|
364 |
else:
|
|
|
365 |
if isinstance(content, dict):
|
366 |
assistant_response = content.get('answer', '❌ ไม่มีข้อมูลคำตอบ')
|
367 |
else:
|
|
|
374 |
</div>
|
375 |
""", unsafe_allow_html=True)
|
376 |
|
|
|
377 |
if isinstance(content, dict) and content.get('documents'):
|
|
|
378 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
379 |
for i, doc in enumerate(content['documents'], 1):
|
380 |
st.markdown(f"""
|
|
|
384 |
</div>
|
385 |
""", unsafe_allow_html=True)
|
386 |
|
387 |
+
# Create chat input using the new form-based approach
|
388 |
+
create_chat_input()
|
389 |
+
|
390 |
# Input section at the bottom
|
391 |
with st.container():
|
392 |
st.markdown("""
|