Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
|
|
4 |
from datetime import datetime, timedelta
|
5 |
-
import subprocess
|
6 |
from huggingface_hub import HfApi
|
7 |
from pathlib import Path
|
8 |
from calendar_rag import (
|
@@ -11,6 +11,8 @@ from calendar_rag import (
|
|
11 |
PipelineConfig
|
12 |
)
|
13 |
|
|
|
|
|
14 |
# Custom CSS for enhanced styling
|
15 |
def load_custom_css():
|
16 |
st.markdown("""
|
@@ -121,9 +123,11 @@ def load_qa_history():
|
|
121 |
st.error(f"Error loading QA history: {str(e)}")
|
122 |
return []
|
123 |
|
|
|
124 |
def save_qa_history(history_entry):
|
125 |
"""Save QA history entry to local JSON file and push to Hugging Face"""
|
126 |
try:
|
|
|
127 |
history_file = Path("qa_history.json")
|
128 |
|
129 |
# Initialize or load existing history
|
@@ -156,14 +160,16 @@ def save_qa_history(history_entry):
|
|
156 |
st.error(f"Error saving QA history: {str(e)}")
|
157 |
|
158 |
def add_to_qa_history(query: str, answer: str):
|
159 |
-
"""Add new QA pair to history"""
|
160 |
history_entry = {
|
161 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
162 |
"query": query,
|
163 |
"answer": answer
|
164 |
}
|
165 |
|
166 |
-
|
|
|
|
|
167 |
return history_entry
|
168 |
|
169 |
def clear_qa_history():
|
@@ -269,7 +275,7 @@ def main():
|
|
269 |
key="query_input"
|
270 |
)
|
271 |
|
272 |
-
# Button layout
|
273 |
col1, col2, col3 = st.columns([1, 1, 4])
|
274 |
|
275 |
with col1:
|
@@ -287,12 +293,13 @@ def main():
|
|
287 |
use_container_width=True,
|
288 |
key="clear_history_button"
|
289 |
)
|
|
|
290 |
# Process query
|
291 |
if send_query and query:
|
292 |
if st.session_state.pipeline is None:
|
293 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
294 |
return
|
295 |
-
|
296 |
add_to_history("user", query)
|
297 |
|
298 |
try:
|
@@ -319,12 +326,12 @@ def main():
|
|
319 |
|
320 |
elif send_query and not query:
|
321 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
|
329 |
with info_col:
|
330 |
# System information
|
@@ -366,5 +373,14 @@ def main():
|
|
366 |
"พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"
|
367 |
), unsafe_allow_html=True)
|
368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
369 |
if __name__ == "__main__":
|
370 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
4 |
+
import time
|
5 |
from datetime import datetime, timedelta
|
|
|
6 |
from huggingface_hub import HfApi
|
7 |
from pathlib import Path
|
8 |
from calendar_rag import (
|
|
|
11 |
PipelineConfig
|
12 |
)
|
13 |
|
14 |
+
all_history = []
|
15 |
+
|
16 |
# Custom CSS for enhanced styling
|
17 |
def load_custom_css():
|
18 |
st.markdown("""
|
|
|
123 |
st.error(f"Error loading QA history: {str(e)}")
|
124 |
return []
|
125 |
|
126 |
+
|
127 |
def save_qa_history(history_entry):
|
128 |
"""Save QA history entry to local JSON file and push to Hugging Face"""
|
129 |
try:
|
130 |
+
|
131 |
history_file = Path("qa_history.json")
|
132 |
|
133 |
# Initialize or load existing history
|
|
|
160 |
st.error(f"Error saving QA history: {str(e)}")
|
161 |
|
162 |
def add_to_qa_history(query: str, answer: str):
|
163 |
+
"""Add new QA pair to history and store it in all_history"""
|
164 |
history_entry = {
|
165 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
166 |
"query": query,
|
167 |
"answer": answer
|
168 |
}
|
169 |
|
170 |
+
# Append the new history entry to the all_history list
|
171 |
+
all_history.append(history_entry)
|
172 |
+
|
173 |
return history_entry
|
174 |
|
175 |
def clear_qa_history():
|
|
|
275 |
key="query_input"
|
276 |
)
|
277 |
|
278 |
+
# Button layout
|
279 |
col1, col2, col3 = st.columns([1, 1, 4])
|
280 |
|
281 |
with col1:
|
|
|
293 |
use_container_width=True,
|
294 |
key="clear_history_button"
|
295 |
)
|
296 |
+
|
297 |
# Process query
|
298 |
if send_query and query:
|
299 |
if st.session_state.pipeline is None:
|
300 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
301 |
return
|
302 |
+
|
303 |
add_to_history("user", query)
|
304 |
|
305 |
try:
|
|
|
326 |
|
327 |
elif send_query and not query:
|
328 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
329 |
+
|
330 |
+
# Clear history
|
331 |
+
if clear_history:
|
332 |
+
st.session_state.chat_history = []
|
333 |
+
save_qa_history([]) # Clear saved history
|
334 |
+
st.rerun()
|
335 |
|
336 |
with info_col:
|
337 |
# System information
|
|
|
373 |
"พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"
|
374 |
), unsafe_allow_html=True)
|
375 |
|
376 |
+
# Handle clear history action
|
377 |
+
if clear_history:
|
378 |
+
st.session_state.chat_history = []
|
379 |
+
clear_qa_history()
|
380 |
+
st.rerun()
|
381 |
+
|
382 |
+
time.sleep(20)
|
383 |
+
save_qa_history(all_history)
|
384 |
+
|
385 |
if __name__ == "__main__":
|
386 |
main()
|