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