Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
4 |
-
from datetime import
|
5 |
-
import subprocess
|
6 |
-
from datetime import datetime
|
7 |
from huggingface_hub import HfApi
|
|
|
8 |
from calendar_rag import (
|
9 |
create_default_config,
|
10 |
AcademicCalendarRAG,
|
@@ -17,43 +17,37 @@ def load_custom_css():
|
|
17 |
<style>
|
18 |
/* General body styling */
|
19 |
body {
|
20 |
-
font-family: "Arial", sans-serif !important;
|
21 |
-
color: #000000 !important;
|
22 |
background-color: white !important;
|
23 |
-
line-height: 1.7 !important;
|
24 |
}
|
25 |
|
26 |
/* Main container styling */
|
27 |
.main {
|
28 |
padding: 2rem;
|
29 |
-
color: #000000;
|
30 |
background-color: white;
|
31 |
}
|
32 |
|
33 |
/* Headers styling */
|
34 |
h1 {
|
35 |
-
color: #000000;
|
36 |
-
font-size: 2.8rem !important;
|
37 |
font-weight: 700 !important;
|
38 |
margin-bottom: 1.5rem !important;
|
39 |
text-align: center;
|
40 |
padding: 1rem 0;
|
41 |
-
border-bottom: 3px solid #1E3A8A;
|
42 |
}
|
43 |
|
44 |
h3, h4 {
|
45 |
-
color: #000000;
|
46 |
font-weight: 600 !important;
|
47 |
-
font-size: 1.6rem !important;
|
48 |
margin-top: 1.5rem !important;
|
49 |
}
|
50 |
|
51 |
-
/* Paragraphs and text */
|
52 |
-
p, span, li {
|
53 |
-
font-size: 1.2rem !important; /* Larger font size for regular text */
|
54 |
-
color: #000000 !important; /* Black text */
|
55 |
-
}
|
56 |
-
|
57 |
/* Chat message styling */
|
58 |
.chat-message {
|
59 |
padding: 1.5rem;
|
@@ -61,9 +55,9 @@ def load_custom_css():
|
|
61 |
margin: 1rem 0;
|
62 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
63 |
font-size: 1.1rem !important;
|
64 |
-
line-height: 1.6 !important;
|
65 |
font-family: "Arial", sans-serif !important;
|
66 |
-
color: #000000 !important;
|
67 |
}
|
68 |
|
69 |
.user-message {
|
@@ -74,50 +68,13 @@ def load_custom_css():
|
|
74 |
background-color: #EFF6FF !important;
|
75 |
}
|
76 |
|
77 |
-
/*
|
78 |
-
.stTextInput input {
|
79 |
-
border-radius: 8px;
|
80 |
-
border: 2px solid #E5E7EB;
|
81 |
-
padding: 0.75rem;
|
82 |
-
font-size: 1.1rem;
|
83 |
-
font-family: "Arial", sans-serif !important;
|
84 |
-
color: #000000; /* Black input text */
|
85 |
-
}
|
86 |
-
|
87 |
-
.stTextInput input:focus {
|
88 |
-
border-color: #1E3A8A;
|
89 |
-
box-shadow: 0 0 0 2px rgba(30, 58, 138, 0.1);
|
90 |
-
}
|
91 |
-
|
92 |
-
/* Button styling */
|
93 |
-
.stButton button {
|
94 |
-
border-radius: 8px;
|
95 |
-
font-weight: 600;
|
96 |
-
font-size: 1.2rem !important; /* Bigger buttons for better interaction */
|
97 |
-
color: #000000; /* Black text on buttons */
|
98 |
-
transition: all 0.2s ease;
|
99 |
-
}
|
100 |
-
|
101 |
-
.stButton button:hover {
|
102 |
-
transform: translateY(-1px);
|
103 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
104 |
-
}
|
105 |
-
|
106 |
-
/* List styling */
|
107 |
-
ul {
|
108 |
-
font-size: 1.2rem !important;
|
109 |
-
margin-left: 1rem;
|
110 |
-
color: #000000; /* Black text in lists */
|
111 |
-
list-style-type: disc;
|
112 |
-
}
|
113 |
-
|
114 |
-
/* Status indicator styling */
|
115 |
.status-indicator {
|
116 |
padding: 0.5rem 1rem;
|
117 |
border-radius: 6px;
|
118 |
font-weight: 500;
|
119 |
-
font-size: 1.2rem;
|
120 |
-
color: #000000;
|
121 |
}
|
122 |
|
123 |
.status-online {
|
@@ -127,32 +84,11 @@ def load_custom_css():
|
|
127 |
|
128 |
.status-offline {
|
129 |
background-color: #FDE8E8;
|
130 |
-
color:rgb(255, 255, 255);
|
131 |
}
|
132 |
</style>
|
133 |
""", unsafe_allow_html=True)
|
134 |
|
135 |
-
# Page config
|
136 |
-
st.set_page_config(
|
137 |
-
page_title="Academic Calendar Assistant",
|
138 |
-
page_icon="📅",
|
139 |
-
layout="wide",
|
140 |
-
initial_sidebar_state="collapsed"
|
141 |
-
)
|
142 |
-
|
143 |
-
# Load custom CSS
|
144 |
-
load_custom_css()
|
145 |
-
|
146 |
-
# Initialize session state
|
147 |
-
if 'pipeline' not in st.session_state:
|
148 |
-
st.session_state.pipeline = None
|
149 |
-
|
150 |
-
if 'chat_history' not in st.session_state:
|
151 |
-
st.session_state.chat_history = []
|
152 |
-
|
153 |
-
if 'feedback_data' not in st.session_state:
|
154 |
-
st.session_state.feedback_data = []
|
155 |
-
|
156 |
def initialize_pipeline():
|
157 |
"""Initialize RAG pipeline with configurations"""
|
158 |
try:
|
@@ -173,43 +109,6 @@ def initialize_pipeline():
|
|
173 |
st.error(f"Error initializing pipeline: {str(e)}")
|
174 |
return None
|
175 |
|
176 |
-
def add_feedback(query: str, answer: str, is_correct: bool):
|
177 |
-
"""Add feedback entry to session state and save to JSON and GitHub"""
|
178 |
-
feedback_entry = {
|
179 |
-
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
180 |
-
"query": query,
|
181 |
-
"answer": answer,
|
182 |
-
"is_correct": is_correct
|
183 |
-
}
|
184 |
-
st.session_state.feedback_data.append(feedback_entry)
|
185 |
-
|
186 |
-
def display_chat_history():
|
187 |
-
"""Display chat history with enhanced styling"""
|
188 |
-
for i, (role, message) in enumerate(st.session_state.chat_history):
|
189 |
-
if role == "user":
|
190 |
-
st.markdown(f"""
|
191 |
-
<div class="chat-message user-message">
|
192 |
-
<strong>🧑 คำถาม:</strong><br>
|
193 |
-
{message}
|
194 |
-
</div>
|
195 |
-
""", unsafe_allow_html=True)
|
196 |
-
else:
|
197 |
-
st.markdown(f"""
|
198 |
-
<div class="chat-message assistant-message">
|
199 |
-
<strong>🤖 คำตอบ:</strong><br>
|
200 |
-
{message}
|
201 |
-
</div>
|
202 |
-
""", unsafe_allow_html=True)
|
203 |
-
|
204 |
-
# Add evaluation buttons after each assistant response
|
205 |
-
if i == len(st.session_state.chat_history) - 1: # Only for the latest answer
|
206 |
-
user_query = st.session_state.chat_history[i-1][1] # Get the corresponding user query
|
207 |
-
evaluate_answer(user_query, message)
|
208 |
-
|
209 |
-
def add_to_history(role: str, message: str):
|
210 |
-
"""Add message to chat history"""
|
211 |
-
st.session_state.chat_history.append((role, message))
|
212 |
-
|
213 |
def load_qa_history():
|
214 |
"""Load QA history from local JSON file"""
|
215 |
try:
|
@@ -230,7 +129,8 @@ def save_qa_history(history_data):
|
|
230 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
231 |
|
232 |
# Push to Hugging Face
|
233 |
-
|
|
|
234 |
api.upload_file(
|
235 |
path_or_fileobj="qa_history.json",
|
236 |
path_in_repo="qa_history.json",
|
@@ -258,7 +158,6 @@ def add_to_qa_history(query: str, answer: str):
|
|
258 |
save_qa_history(history_data)
|
259 |
return history_data
|
260 |
|
261 |
-
# Modify the main code to include these changes
|
262 |
def add_to_history(role: str, message: str):
|
263 |
"""Add message to chat history and save if it's a complete QA pair"""
|
264 |
st.session_state.chat_history.append((role, message))
|
@@ -269,20 +168,49 @@ def add_to_history(role: str, message: str):
|
|
269 |
user_query = st.session_state.chat_history[-2][1]
|
270 |
add_to_qa_history(user_query, message)
|
271 |
|
272 |
-
|
273 |
-
|
274 |
-
st.session_state.
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
def main():
|
285 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
st.markdown("""
|
287 |
<div style="text-align: center; padding: 2rem 0;">
|
288 |
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
@@ -297,26 +225,24 @@ def main():
|
|
297 |
chat_col, info_col = st.columns([7, 3])
|
298 |
|
299 |
with chat_col:
|
300 |
-
# Add a subtle container for the chat interface
|
301 |
with st.container():
|
302 |
# Display chat history
|
303 |
display_chat_history()
|
304 |
|
305 |
-
#
|
306 |
st.markdown("""
|
307 |
<label for="query_input" style="font-size: 1.2rem; font-weight: bold; color: black;">
|
308 |
<span style="color: white; background-color: yellow; padding: 0 0.2rem;">โปรดระบุคำถามเกี่ยวกับ ปฏิทินการศึกษา:</span>
|
309 |
</label>
|
310 |
""", unsafe_allow_html=True)
|
311 |
|
312 |
-
# The input box without a label
|
313 |
query = st.text_input(
|
314 |
"",
|
315 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
316 |
key="query_input"
|
317 |
)
|
318 |
|
319 |
-
# Button layout
|
320 |
col1, col2, col3 = st.columns([1, 1, 4])
|
321 |
|
322 |
with col1:
|
@@ -326,14 +252,6 @@ def main():
|
|
326 |
use_container_width=True,
|
327 |
key="send_query_button"
|
328 |
)
|
329 |
-
st.markdown("""
|
330 |
-
<style>
|
331 |
-
#send_query_button button {
|
332 |
-
background-color: #28a745 !important; /* Green background */
|
333 |
-
color: white !important;
|
334 |
-
}
|
335 |
-
</style>
|
336 |
-
""", unsafe_allow_html=True)
|
337 |
|
338 |
with col2:
|
339 |
clear_history = st.button(
|
@@ -342,15 +260,6 @@ def main():
|
|
342 |
use_container_width=True,
|
343 |
key="clear_history_button"
|
344 |
)
|
345 |
-
st.markdown("""
|
346 |
-
<style>
|
347 |
-
#clear_history_button button {
|
348 |
-
background-color: #dc3545 !important; /* Red background */
|
349 |
-
color: white !important;
|
350 |
-
}
|
351 |
-
</style>
|
352 |
-
""", unsafe_allow_html=True)
|
353 |
-
|
354 |
|
355 |
# Process query
|
356 |
if send_query and query:
|
@@ -365,7 +274,6 @@ def main():
|
|
365 |
result = st.session_state.pipeline.process_query(query)
|
366 |
add_to_history("assistant", result["answer"])
|
367 |
|
368 |
-
# Enhanced expandable sections
|
369 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
370 |
for i, doc in enumerate(result["documents"], 1):
|
371 |
st.markdown(f"""
|
@@ -385,9 +293,15 @@ def main():
|
|
385 |
|
386 |
elif send_query and not query:
|
387 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
|
389 |
with info_col:
|
390 |
-
#
|
391 |
st.markdown("""
|
392 |
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|
393 |
<h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
|
@@ -406,7 +320,7 @@ def main():
|
|
406 |
</div>
|
407 |
""", unsafe_allow_html=True)
|
408 |
|
409 |
-
#
|
410 |
st.markdown("""
|
411 |
<div style="background-color: #f9fafb; padding: 1.5rem; border-radius: 12px;">
|
412 |
<h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>
|
|
|
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 (
|
9 |
create_default_config,
|
10 |
AcademicCalendarRAG,
|
|
|
17 |
<style>
|
18 |
/* General body styling */
|
19 |
body {
|
20 |
+
font-family: "Arial", sans-serif !important;
|
21 |
+
color: #000000 !important;
|
22 |
background-color: white !important;
|
23 |
+
line-height: 1.7 !important;
|
24 |
}
|
25 |
|
26 |
/* Main container styling */
|
27 |
.main {
|
28 |
padding: 2rem;
|
29 |
+
color: #000000;
|
30 |
background-color: white;
|
31 |
}
|
32 |
|
33 |
/* Headers styling */
|
34 |
h1 {
|
35 |
+
color: #000000;
|
36 |
+
font-size: 2.8rem !important;
|
37 |
font-weight: 700 !important;
|
38 |
margin-bottom: 1.5rem !important;
|
39 |
text-align: center;
|
40 |
padding: 1rem 0;
|
41 |
+
border-bottom: 3px solid #1E3A8A;
|
42 |
}
|
43 |
|
44 |
h3, h4 {
|
45 |
+
color: #000000;
|
46 |
font-weight: 600 !important;
|
47 |
+
font-size: 1.6rem !important;
|
48 |
margin-top: 1.5rem !important;
|
49 |
}
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
/* Chat message styling */
|
52 |
.chat-message {
|
53 |
padding: 1.5rem;
|
|
|
55 |
margin: 1rem 0;
|
56 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
57 |
font-size: 1.1rem !important;
|
58 |
+
line-height: 1.6 !important;
|
59 |
font-family: "Arial", sans-serif !important;
|
60 |
+
color: #000000 !important;
|
61 |
}
|
62 |
|
63 |
.user-message {
|
|
|
68 |
background-color: #EFF6FF !important;
|
69 |
}
|
70 |
|
71 |
+
/* Status indicators */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
.status-indicator {
|
73 |
padding: 0.5rem 1rem;
|
74 |
border-radius: 6px;
|
75 |
font-weight: 500;
|
76 |
+
font-size: 1.2rem;
|
77 |
+
color: #000000;
|
78 |
}
|
79 |
|
80 |
.status-online {
|
|
|
84 |
|
85 |
.status-offline {
|
86 |
background-color: #FDE8E8;
|
87 |
+
color: rgb(255, 255, 255);
|
88 |
}
|
89 |
</style>
|
90 |
""", unsafe_allow_html=True)
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
def initialize_pipeline():
|
93 |
"""Initialize RAG pipeline with configurations"""
|
94 |
try:
|
|
|
109 |
st.error(f"Error initializing pipeline: {str(e)}")
|
110 |
return None
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
def load_qa_history():
|
113 |
"""Load QA history from local JSON file"""
|
114 |
try:
|
|
|
129 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
130 |
|
131 |
# Push to Hugging Face
|
132 |
+
hf_token = os.getenv('HF_TOKEN') or st.secrets['HF_TOKEN']
|
133 |
+
api = HfApi(token=hf_token)
|
134 |
api.upload_file(
|
135 |
path_or_fileobj="qa_history.json",
|
136 |
path_in_repo="qa_history.json",
|
|
|
158 |
save_qa_history(history_data)
|
159 |
return history_data
|
160 |
|
|
|
161 |
def add_to_history(role: str, message: str):
|
162 |
"""Add message to chat history and save if it's a complete QA pair"""
|
163 |
st.session_state.chat_history.append((role, message))
|
|
|
168 |
user_query = st.session_state.chat_history[-2][1]
|
169 |
add_to_qa_history(user_query, message)
|
170 |
|
171 |
+
def display_chat_history():
|
172 |
+
"""Display chat history with enhanced styling"""
|
173 |
+
for i, (role, message) in enumerate(st.session_state.chat_history):
|
174 |
+
if role == "user":
|
175 |
+
st.markdown(f"""
|
176 |
+
<div class="chat-message user-message">
|
177 |
+
<strong>🧑 คำถาม:</strong><br>
|
178 |
+
{message}
|
179 |
+
</div>
|
180 |
+
""", unsafe_allow_html=True)
|
181 |
+
else:
|
182 |
+
st.markdown(f"""
|
183 |
+
<div class="chat-message assistant-message">
|
184 |
+
<strong>🤖 คำตอบ:</strong><br>
|
185 |
+
{message}
|
186 |
+
</div>
|
187 |
+
""", unsafe_allow_html=True)
|
188 |
|
189 |
def main():
|
190 |
+
# Page config
|
191 |
+
st.set_page_config(
|
192 |
+
page_title="Academic Calendar Assistant",
|
193 |
+
page_icon="📅",
|
194 |
+
layout="wide",
|
195 |
+
initial_sidebar_state="collapsed"
|
196 |
+
)
|
197 |
+
|
198 |
+
# Load custom CSS
|
199 |
+
load_custom_css()
|
200 |
+
|
201 |
+
# Initialize session state
|
202 |
+
if 'pipeline' not in st.session_state:
|
203 |
+
st.session_state.pipeline = None
|
204 |
+
|
205 |
+
if 'chat_history' not in st.session_state:
|
206 |
+
st.session_state.chat_history = []
|
207 |
+
|
208 |
+
# Load QA history at startup
|
209 |
+
if 'qa_history_loaded' not in st.session_state:
|
210 |
+
st.session_state.qa_history_loaded = True
|
211 |
+
load_qa_history()
|
212 |
+
|
213 |
+
# Header
|
214 |
st.markdown("""
|
215 |
<div style="text-align: center; padding: 2rem 0;">
|
216 |
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
|
|
225 |
chat_col, info_col = st.columns([7, 3])
|
226 |
|
227 |
with chat_col:
|
|
|
228 |
with st.container():
|
229 |
# Display chat history
|
230 |
display_chat_history()
|
231 |
|
232 |
+
# Query input section
|
233 |
st.markdown("""
|
234 |
<label for="query_input" style="font-size: 1.2rem; font-weight: bold; color: black;">
|
235 |
<span style="color: white; background-color: yellow; padding: 0 0.2rem;">โปรดระบุคำถามเกี่ยวกับ ปฏิทินการศึกษา:</span>
|
236 |
</label>
|
237 |
""", unsafe_allow_html=True)
|
238 |
|
|
|
239 |
query = st.text_input(
|
240 |
"",
|
241 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
242 |
key="query_input"
|
243 |
)
|
244 |
|
245 |
+
# Button layout
|
246 |
col1, col2, col3 = st.columns([1, 1, 4])
|
247 |
|
248 |
with col1:
|
|
|
252 |
use_container_width=True,
|
253 |
key="send_query_button"
|
254 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
with col2:
|
257 |
clear_history = st.button(
|
|
|
260 |
use_container_width=True,
|
261 |
key="clear_history_button"
|
262 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
|
264 |
# Process query
|
265 |
if send_query and query:
|
|
|
274 |
result = st.session_state.pipeline.process_query(query)
|
275 |
add_to_history("assistant", result["answer"])
|
276 |
|
|
|
277 |
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
278 |
for i, doc in enumerate(result["documents"], 1):
|
279 |
st.markdown(f"""
|
|
|
293 |
|
294 |
elif send_query and not query:
|
295 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
296 |
+
|
297 |
+
# Clear history
|
298 |
+
if clear_history:
|
299 |
+
st.session_state.chat_history = []
|
300 |
+
save_qa_history([]) # Clear saved history
|
301 |
+
st.rerun()
|
302 |
|
303 |
with info_col:
|
304 |
+
# System information
|
305 |
st.markdown("""
|
306 |
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|
307 |
<h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
|
|
|
320 |
</div>
|
321 |
""", unsafe_allow_html=True)
|
322 |
|
323 |
+
# System status
|
324 |
st.markdown("""
|
325 |
<div style="background-color: #f9fafb; padding: 1.5rem; border-radius: 12px;">
|
326 |
<h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>
|