Update app.py
Browse files
app.py
CHANGED
@@ -225,19 +225,23 @@ if 'context_memory' not in st.session_state:
|
|
225 |
st.session_state.context_memory = []
|
226 |
|
227 |
def submit():
|
|
|
|
|
|
|
|
|
|
|
228 |
if not st.session_state.query_input:
|
229 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
|
|
230 |
return
|
231 |
|
232 |
-
user_query = st.session_state.query_input.strip()
|
233 |
-
st.session_state.query_input = ""
|
234 |
|
235 |
-
# Prevent duplicate user query from being added twice
|
236 |
if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
|
237 |
st.session_state.chat_history.append(("user", user_query))
|
238 |
|
239 |
-
|
240 |
-
if len(st.session_state.context_memory) > 5: # Limit to last 5 Q&A pairs
|
241 |
st.session_state.context_memory.pop(0)
|
242 |
|
243 |
try:
|
@@ -246,7 +250,6 @@ def submit():
|
|
246 |
) + f"\nQ: {user_query}"
|
247 |
|
248 |
result = st.session_state.pipeline.process_query(query_with_context)
|
249 |
-
|
250 |
assistant_answer = result["answer"]
|
251 |
|
252 |
st.session_state.chat_history.append(("assistant", assistant_answer))
|
@@ -259,7 +262,7 @@ def submit():
|
|
259 |
st.error(f"Query processing error: {e}")
|
260 |
|
261 |
finally:
|
262 |
-
st.session_state.processing_query = False #
|
263 |
|
264 |
def main():
|
265 |
# Page config
|
@@ -357,7 +360,8 @@ def main():
|
|
357 |
st.text_input(
|
358 |
"",
|
359 |
key="query_input",
|
360 |
-
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
|
|
|
361 |
)
|
362 |
|
363 |
# Button layout
|
|
|
225 |
st.session_state.context_memory = []
|
226 |
|
227 |
def submit():
|
228 |
+
if st.session_state.get("processing_query", False):
|
229 |
+
return
|
230 |
+
|
231 |
+
st.session_state.processing_query = True # ป้องกันการเรียกซ้ำ
|
232 |
+
|
233 |
if not st.session_state.query_input:
|
234 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
235 |
+
st.session_state.processing_query = False
|
236 |
return
|
237 |
|
238 |
+
user_query = st.session_state.query_input.strip()
|
239 |
+
st.session_state.query_input = ""
|
240 |
|
|
|
241 |
if not st.session_state.chat_history or st.session_state.chat_history[-1][1] != user_query:
|
242 |
st.session_state.chat_history.append(("user", user_query))
|
243 |
|
244 |
+
if len(st.session_state.context_memory) > 5:
|
|
|
245 |
st.session_state.context_memory.pop(0)
|
246 |
|
247 |
try:
|
|
|
250 |
) + f"\nQ: {user_query}"
|
251 |
|
252 |
result = st.session_state.pipeline.process_query(query_with_context)
|
|
|
253 |
assistant_answer = result["answer"]
|
254 |
|
255 |
st.session_state.chat_history.append(("assistant", assistant_answer))
|
|
|
262 |
st.error(f"Query processing error: {e}")
|
263 |
|
264 |
finally:
|
265 |
+
st.session_state.processing_query = False # รีเซ็ต flag
|
266 |
|
267 |
def main():
|
268 |
# Page config
|
|
|
360 |
st.text_input(
|
361 |
"",
|
362 |
key="query_input",
|
363 |
+
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
364 |
+
on_change=submit
|
365 |
)
|
366 |
|
367 |
# Button layout
|