Update app.py
Browse files
app.py
CHANGED
@@ -126,7 +126,6 @@ def load_qa_history():
|
|
126 |
def save_qa_history(history_entry):
|
127 |
"""Save QA history entry to local JSON file and push to Hugging Face"""
|
128 |
try:
|
129 |
-
|
130 |
history_file = Path("qa_history.json")
|
131 |
|
132 |
# Initialize or load existing history
|
@@ -146,15 +145,19 @@ def save_qa_history(history_entry):
|
|
146 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
147 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
148 |
|
149 |
-
# Push to Hugging Face
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
158 |
except Exception as e:
|
159 |
st.error(f"Error saving QA history: {str(e)}")
|
160 |
|
@@ -216,6 +219,30 @@ def display_chat_history():
|
|
216 |
</div>
|
217 |
""", unsafe_allow_html=True)
|
218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
def main():
|
220 |
# Page config
|
221 |
st.set_page_config(
|
@@ -234,18 +261,9 @@ def main():
|
|
234 |
|
235 |
if 'chat_history' not in st.session_state:
|
236 |
st.session_state.chat_history = []
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
st.session_state.qa_history_loaded = True
|
241 |
-
load_qa_history()
|
242 |
-
|
243 |
-
# Header
|
244 |
-
st.markdown("""
|
245 |
-
<div style="text-align: center; padding: 2rem 0;">
|
246 |
-
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
247 |
-
</div>
|
248 |
-
""", unsafe_allow_html=True)
|
249 |
|
250 |
# Initialize pipeline
|
251 |
if st.session_state.pipeline is None:
|
@@ -266,6 +284,7 @@ def main():
|
|
266 |
</label>
|
267 |
""", unsafe_allow_html=True)
|
268 |
|
|
|
269 |
query = st.text_input(
|
270 |
"",
|
271 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
@@ -284,51 +303,49 @@ def main():
|
|
284 |
)
|
285 |
|
286 |
with col2:
|
287 |
-
|
288 |
"🗑️ ล้างประวัติ",
|
289 |
type="secondary",
|
290 |
use_container_width=True,
|
291 |
key="clear_history_button"
|
292 |
-
)
|
|
|
|
|
|
|
|
|
293 |
|
294 |
# Process query
|
295 |
if send_query and query:
|
296 |
if st.session_state.pipeline is None:
|
297 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
298 |
-
|
299 |
-
|
300 |
-
add_to_history("user", query)
|
301 |
-
|
302 |
-
try:
|
303 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
304 |
-
result = st.session_state.pipeline
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
except Exception as e:
|
322 |
-
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
323 |
|
324 |
elif send_query and not query:
|
325 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
326 |
|
327 |
-
# Clear history
|
328 |
-
if clear_history:
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
|
333 |
with info_col:
|
334 |
# System information
|
|
|
126 |
def save_qa_history(history_entry):
|
127 |
"""Save QA history entry to local JSON file and push to Hugging Face"""
|
128 |
try:
|
|
|
129 |
history_file = Path("qa_history.json")
|
130 |
|
131 |
# Initialize or load existing history
|
|
|
145 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
146 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
147 |
|
148 |
+
# Push to Hugging Face without triggering a reload
|
149 |
+
try:
|
150 |
+
hf_token = os.getenv('HF_TOKEN') or st.secrets['HF_TOKEN']
|
151 |
+
api = HfApi(token=hf_token)
|
152 |
+
api.upload_file(
|
153 |
+
path_or_fileobj="qa_history.json",
|
154 |
+
path_in_repo="qa_history.json",
|
155 |
+
repo_id="JirasakJo/Questions_Graduate_Studies_Calendar_2024",
|
156 |
+
repo_type="space"
|
157 |
+
)
|
158 |
+
except Exception as e:
|
159 |
+
st.warning(f"Warning: Could not sync history to Hugging Face: {str(e)}", icon="⚠️")
|
160 |
+
|
161 |
except Exception as e:
|
162 |
st.error(f"Error saving QA history: {str(e)}")
|
163 |
|
|
|
219 |
</div>
|
220 |
""", unsafe_allow_html=True)
|
221 |
|
222 |
+
|
223 |
+
def process_query(query: str, pipeline):
|
224 |
+
"""Process a query and update the chat history"""
|
225 |
+
try:
|
226 |
+
result = pipeline.process_query(query)
|
227 |
+
|
228 |
+
# Add to session state history
|
229 |
+
st.session_state.chat_history.append(("user", query))
|
230 |
+
st.session_state.chat_history.append(("assistant", result["answer"]))
|
231 |
+
|
232 |
+
# Save to QA history without reloading
|
233 |
+
history_entry = {
|
234 |
+
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
235 |
+
"query": query,
|
236 |
+
"answer": result["answer"]
|
237 |
+
}
|
238 |
+
save_qa_history(history_entry)
|
239 |
+
|
240 |
+
return result
|
241 |
+
|
242 |
+
except Exception as e:
|
243 |
+
st.error(f"Error processing query: {str(e)}")
|
244 |
+
return None
|
245 |
+
|
246 |
def main():
|
247 |
# Page config
|
248 |
st.set_page_config(
|
|
|
261 |
|
262 |
if 'chat_history' not in st.session_state:
|
263 |
st.session_state.chat_history = []
|
264 |
+
|
265 |
+
if 'current_result' not in st.session_state:
|
266 |
+
st.session_state.current_result = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
# Initialize pipeline
|
269 |
if st.session_state.pipeline is None:
|
|
|
284 |
</label>
|
285 |
""", unsafe_allow_html=True)
|
286 |
|
287 |
+
# Query input with key
|
288 |
query = st.text_input(
|
289 |
"",
|
290 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
|
|
303 |
)
|
304 |
|
305 |
with col2:
|
306 |
+
if st.button(
|
307 |
"🗑️ ล้างประวัติ",
|
308 |
type="secondary",
|
309 |
use_container_width=True,
|
310 |
key="clear_history_button"
|
311 |
+
):
|
312 |
+
st.session_state.chat_history = []
|
313 |
+
st.session_state.current_result = None
|
314 |
+
clear_qa_history()
|
315 |
+
st.rerun()
|
316 |
|
317 |
# Process query
|
318 |
if send_query and query:
|
319 |
if st.session_state.pipeline is None:
|
320 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
321 |
+
else:
|
|
|
|
|
|
|
|
|
322 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
323 |
+
result = process_query(query, st.session_state.pipeline)
|
324 |
+
if result:
|
325 |
+
st.session_state.current_result = result
|
326 |
+
|
327 |
+
# Display reference documents
|
328 |
+
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
329 |
+
for i, doc in enumerate(result["documents"], 1):
|
330 |
+
st.markdown(f"""
|
331 |
+
<div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
|
332 |
+
<strong>เอกสารที่ {i}:</strong><br>
|
333 |
+
{doc.content}
|
334 |
+
</div>
|
335 |
+
""", unsafe_allow_html=True)
|
336 |
+
|
337 |
+
# Display query analysis
|
338 |
+
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
|
339 |
+
st.json(result["query_info"])
|
|
|
|
|
340 |
|
341 |
elif send_query and not query:
|
342 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
343 |
|
344 |
+
# # Clear history
|
345 |
+
# if clear_history:
|
346 |
+
# st.session_state.chat_history = []
|
347 |
+
# save_qa_history([]) # Clear saved history
|
348 |
+
# st.rerun()
|
349 |
|
350 |
with info_col:
|
351 |
# System information
|