Update app.py
Browse files
app.py
CHANGED
|
@@ -222,47 +222,58 @@ def display_chat_history():
|
|
| 222 |
""", unsafe_allow_html=True)
|
| 223 |
|
| 224 |
def submit():
|
| 225 |
-
if st.session_state.query_input:
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
return
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
|
|
|
| 240 |
|
| 241 |
-
|
| 242 |
-
st.session_state.chat_history.append(("assistant", {"answer": "🔍 กำลังค้นหาคำตอบ...", "documents": [], "query_info": {}}))
|
| 243 |
-
st.rerun() # Trigger UI update for spinner
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
"documents": result["documents"],
|
| 253 |
-
"query_info": result["query_info"]
|
| 254 |
-
})
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
st.rerun()
|
| 264 |
|
| 265 |
-
|
| 266 |
def main():
|
| 267 |
# Page config
|
| 268 |
st.set_page_config(
|
|
|
|
| 222 |
""", unsafe_allow_html=True)
|
| 223 |
|
| 224 |
def submit():
|
| 225 |
+
if not st.session_state.query_input:
|
| 226 |
+
st.warning("⚠️ กรุณาระบุคำถาม")
|
| 227 |
+
return
|
|
|
|
| 228 |
|
| 229 |
+
if st.session_state.pipeline is None:
|
| 230 |
+
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
| 231 |
+
return
|
| 232 |
|
| 233 |
+
# Store user query
|
| 234 |
+
user_query = st.session_state.query_input
|
| 235 |
+
st.session_state.chat_history.append(("user", user_query))
|
| 236 |
|
| 237 |
+
# Avoid infinite reruns
|
| 238 |
+
if "processing_query" in st.session_state and st.session_state.processing_query:
|
| 239 |
+
return
|
| 240 |
|
| 241 |
+
st.session_state.processing_query = True
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
# Display a loading message
|
| 244 |
+
st.session_state.chat_history.append(("assistant", {
|
| 245 |
+
"answer": "🔍 กำลังค้นหาคำตอบ...",
|
| 246 |
+
"documents": [],
|
| 247 |
+
"query_info": {}
|
| 248 |
+
}))
|
| 249 |
+
|
| 250 |
+
# **No `st.rerun()` here yet! Just update chat**
|
| 251 |
+
st.session_state.query_input = "" # Clear input after submission
|
| 252 |
+
st.experimental_rerun() # This will force a refresh once
|
| 253 |
|
| 254 |
+
try:
|
| 255 |
+
# Process the query
|
| 256 |
+
result = st.session_state.pipeline.process_query(user_query)
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
# Replace loading message with actual answer
|
| 259 |
+
st.session_state.chat_history[-1] = ("assistant", {
|
| 260 |
+
"answer": result["answer"],
|
| 261 |
+
"documents": result["documents"],
|
| 262 |
+
"query_info": result["query_info"]
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
+
except Exception as e:
|
| 266 |
+
# Replace with an error message if query fails
|
| 267 |
+
st.session_state.chat_history[-1] = ("assistant", {
|
| 268 |
+
"answer": f"❌ เกิดข้อผิดพลาด: {str(e)}",
|
| 269 |
+
"documents": [],
|
| 270 |
+
"query_info": {}
|
| 271 |
+
})
|
| 272 |
|
| 273 |
+
# Reset processing flag
|
| 274 |
+
st.session_state.processing_query = False
|
| 275 |
+
st.experimental_rerun() # Refresh UI once processing is done
|
|
|
|
| 276 |
|
|
|
|
| 277 |
def main():
|
| 278 |
# Page config
|
| 279 |
st.set_page_config(
|