Update app.py
Browse files
app.py
CHANGED
@@ -124,10 +124,13 @@ def load_qa_history():
|
|
124 |
st.error(f"Error loading QA history: {str(e)}")
|
125 |
return []
|
126 |
|
127 |
-
|
128 |
def save_qa_history(history_entry):
|
129 |
-
"""
|
|
|
|
|
|
|
130 |
try:
|
|
|
131 |
history_file = Path("qa_history.json")
|
132 |
|
133 |
# Initialize or load existing history
|
@@ -143,29 +146,36 @@ def save_qa_history(history_entry):
|
|
143 |
# Append new entry
|
144 |
history_data.append(history_entry)
|
145 |
|
146 |
-
# Save updated history
|
147 |
with open(history_file, "w", encoding="utf-8") as f:
|
148 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
149 |
|
150 |
-
# GitHub
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
except Exception as e:
|
168 |
-
|
|
|
169 |
|
170 |
def add_to_qa_history(query: str, answer: str):
|
171 |
"""Add new QA pair to history and store it in all_history"""
|
@@ -227,6 +237,40 @@ def display_chat_history():
|
|
227 |
</div>
|
228 |
""", unsafe_allow_html=True)
|
229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
def main():
|
231 |
# Page config
|
232 |
st.set_page_config(
|
@@ -245,6 +289,9 @@ def main():
|
|
245 |
|
246 |
if 'chat_history' not in st.session_state:
|
247 |
st.session_state.chat_history = []
|
|
|
|
|
|
|
248 |
|
249 |
# Load QA history at startup
|
250 |
if 'qa_history_loaded' not in st.session_state:
|
@@ -255,6 +302,7 @@ def main():
|
|
255 |
st.markdown("""
|
256 |
<div style="text-align: center; padding: 2rem 0;">
|
257 |
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
|
|
258 |
</div>
|
259 |
""", unsafe_allow_html=True)
|
260 |
|
@@ -263,6 +311,7 @@ def main():
|
|
263 |
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
264 |
st.session_state.pipeline = initialize_pipeline()
|
265 |
|
|
|
266 |
chat_col, info_col = st.columns([7, 3])
|
267 |
|
268 |
with chat_col:
|
@@ -313,7 +362,16 @@ def main():
|
|
313 |
try:
|
314 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
315 |
result = st.session_state.pipeline.process_query(query)
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
|
318 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
319 |
for i, doc in enumerate(result["documents"], 1):
|
@@ -337,7 +395,6 @@ def main():
|
|
337 |
|
338 |
# Clear history
|
339 |
if clear_history:
|
340 |
-
time.sleep(20)
|
341 |
st.session_state.chat_history = []
|
342 |
save_qa_history([]) # Clear saved history
|
343 |
st.rerun()
|
@@ -373,17 +430,21 @@ def main():
|
|
373 |
<span class="status-indicator {}">
|
374 |
{} {}
|
375 |
</span></p>
|
|
|
|
|
|
|
|
|
376 |
</div>
|
377 |
</div>
|
378 |
""".format(
|
379 |
-
(datetime.now() + timedelta(hours=
|
380 |
"status-online" if st.session_state.pipeline else "status-offline",
|
381 |
"🟢" if st.session_state.pipeline else "🔴",
|
382 |
-
"พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"
|
|
|
|
|
|
|
383 |
), unsafe_allow_html=True)
|
384 |
-
|
385 |
-
time.sleep(20)
|
386 |
-
save_qa_history(all_history)
|
387 |
|
388 |
if __name__ == "__main__":
|
389 |
main()
|
|
|
124 |
st.error(f"Error loading QA history: {str(e)}")
|
125 |
return []
|
126 |
|
|
|
127 |
def save_qa_history(history_entry):
|
128 |
+
"""
|
129 |
+
Save QA history entry to local JSON file and push to GitHub with improved error handling
|
130 |
+
and configurable paths.
|
131 |
+
"""
|
132 |
try:
|
133 |
+
# Save to local JSON file first
|
134 |
history_file = Path("qa_history.json")
|
135 |
|
136 |
# Initialize or load existing history
|
|
|
146 |
# Append new entry
|
147 |
history_data.append(history_entry)
|
148 |
|
149 |
+
# Save updated history locally
|
150 |
with open(history_file, "w", encoding="utf-8") as f:
|
151 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
152 |
|
153 |
+
# Try to push to GitHub if configured
|
154 |
+
try:
|
155 |
+
# Get repository path from environment variable or streamlit secrets
|
156 |
+
repo_path = os.getenv('GITHUB_REPO_PATH') or st.secrets.get('GITHUB_REPO_PATH')
|
157 |
+
|
158 |
+
if repo_path and Path(repo_path).exists():
|
159 |
+
repo = Repo(repo_path)
|
160 |
+
repo.git.add("qa_history.json")
|
161 |
+
if repo.is_dirty():
|
162 |
+
repo.index.commit("Update QA history")
|
163 |
+
origin = repo.remote(name="origin")
|
164 |
+
origin.push()
|
165 |
+
else:
|
166 |
+
print("No changes to commit.")
|
167 |
+
else:
|
168 |
+
print("GitHub sync skipped - repository path not configured or doesn't exist")
|
169 |
+
|
170 |
+
except Exception as git_error:
|
171 |
+
print(f"GitHub sync failed (continuing without GitHub push): {str(git_error)}")
|
172 |
+
# Continue execution - GitHub sync is optional
|
173 |
+
|
174 |
+
return True
|
175 |
+
|
176 |
except Exception as e:
|
177 |
+
print(f"Error saving QA history: {str(e)}")
|
178 |
+
return False
|
179 |
|
180 |
def add_to_qa_history(query: str, answer: str):
|
181 |
"""Add new QA pair to history and store it in all_history"""
|
|
|
237 |
</div>
|
238 |
""", unsafe_allow_html=True)
|
239 |
|
240 |
+
def initialize_github_sync():
|
241 |
+
"""
|
242 |
+
Initialize GitHub repository connection and validate configuration.
|
243 |
+
Returns True if GitHub sync is properly configured, False otherwise.
|
244 |
+
"""
|
245 |
+
try:
|
246 |
+
repo_path = os.getenv('GITHUB_REPO_PATH') or st.secrets.get('GITHUB_REPO_PATH')
|
247 |
+
|
248 |
+
if not repo_path:
|
249 |
+
print("GitHub repository path not configured")
|
250 |
+
return False
|
251 |
+
|
252 |
+
repo_dir = Path(repo_path)
|
253 |
+
if not repo_dir.exists():
|
254 |
+
print(f"Repository directory does not exist: {repo_path}")
|
255 |
+
return False
|
256 |
+
|
257 |
+
# Validate Git repository
|
258 |
+
try:
|
259 |
+
repo = Repo(repo_dir)
|
260 |
+
# Check if remote 'origin' exists
|
261 |
+
if 'origin' not in [remote.name for remote in repo.remotes]:
|
262 |
+
print("Remote 'origin' not configured in repository")
|
263 |
+
return False
|
264 |
+
except Exception as e:
|
265 |
+
print(f"Error validating Git repository: {str(e)}")
|
266 |
+
return False
|
267 |
+
|
268 |
+
return True
|
269 |
+
|
270 |
+
except Exception as e:
|
271 |
+
print(f"Error initializing GitHub sync: {str(e)}")
|
272 |
+
return False
|
273 |
+
|
274 |
def main():
|
275 |
# Page config
|
276 |
st.set_page_config(
|
|
|
289 |
|
290 |
if 'chat_history' not in st.session_state:
|
291 |
st.session_state.chat_history = []
|
292 |
+
|
293 |
+
if 'github_sync_enabled' not in st.session_state:
|
294 |
+
st.session_state.github_sync_enabled = initialize_github_sync()
|
295 |
|
296 |
# Load QA history at startup
|
297 |
if 'qa_history_loaded' not in st.session_state:
|
|
|
302 |
st.markdown("""
|
303 |
<div style="text-align: center; padding: 2rem 0;">
|
304 |
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
305 |
+
<p style="font-size: 1.2rem; color: #666;">บัณฑิตวิทยาลัย มหาวิทยาลัยศรีนครินทรวิโรฒ</p>
|
306 |
</div>
|
307 |
""", unsafe_allow_html=True)
|
308 |
|
|
|
311 |
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
312 |
st.session_state.pipeline = initialize_pipeline()
|
313 |
|
314 |
+
# Layout setup
|
315 |
chat_col, info_col = st.columns([7, 3])
|
316 |
|
317 |
with chat_col:
|
|
|
362 |
try:
|
363 |
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
364 |
result = st.session_state.pipeline.process_query(query)
|
365 |
+
answer = result["answer"]
|
366 |
+
add_to_history("assistant", answer)
|
367 |
+
|
368 |
+
# Save QA pair to history
|
369 |
+
history_entry = add_to_qa_history(query, answer)
|
370 |
+
if st.session_state.github_sync_enabled:
|
371 |
+
if save_qa_history(history_entry):
|
372 |
+
print("Successfully saved QA history to GitHub")
|
373 |
+
else:
|
374 |
+
print("Failed to save QA history to GitHub")
|
375 |
|
376 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
377 |
for i, doc in enumerate(result["documents"], 1):
|
|
|
395 |
|
396 |
# Clear history
|
397 |
if clear_history:
|
|
|
398 |
st.session_state.chat_history = []
|
399 |
save_qa_history([]) # Clear saved history
|
400 |
st.rerun()
|
|
|
430 |
<span class="status-indicator {}">
|
431 |
{} {}
|
432 |
</span></p>
|
433 |
+
<p><strong>💾 สถานะ GitHub Sync:</strong><br>
|
434 |
+
<span class="status-indicator {}">
|
435 |
+
{} {}
|
436 |
+
</span></p>
|
437 |
</div>
|
438 |
</div>
|
439 |
""".format(
|
440 |
+
(datetime.now() + timedelta(hours=7)).strftime('%Y-%m-%d %H:%M:%S'),
|
441 |
"status-online" if st.session_state.pipeline else "status-offline",
|
442 |
"🟢" if st.session_state.pipeline else "🔴",
|
443 |
+
"พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน",
|
444 |
+
"status-online" if st.session_state.github_sync_enabled else "status-offline",
|
445 |
+
"🟢" if st.session_state.github_sync_enabled else "🔴",
|
446 |
+
"เชื่อมต่อแล้ว" if st.session_state.github_sync_enabled else "ไม่ได้เชื่อมต่อ"
|
447 |
), unsafe_allow_html=True)
|
|
|
|
|
|
|
448 |
|
449 |
if __name__ == "__main__":
|
450 |
main()
|