Update app.py
Browse files
app.py
CHANGED
@@ -8,36 +8,151 @@ from calendar_rag import (
|
|
8 |
PipelineConfig
|
9 |
)
|
10 |
|
11 |
-
#
|
12 |
st.set_page_config(
|
13 |
page_title="Academic Calendar Assistant",
|
14 |
page_icon="📅",
|
15 |
layout="wide"
|
16 |
)
|
17 |
|
18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
if 'pipeline' not in st.session_state:
|
20 |
st.session_state.pipeline = None
|
21 |
|
22 |
if 'chat_history' not in st.session_state:
|
23 |
st.session_state.chat_history = []
|
|
|
|
|
|
|
24 |
|
25 |
def initialize_pipeline():
|
26 |
"""Initialize RAG pipeline with configurations"""
|
27 |
try:
|
28 |
-
# Get OpenAI API key from environment variable or Streamlit secrets
|
29 |
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
|
30 |
-
|
31 |
-
# Create config
|
32 |
config = create_default_config(openai_api_key)
|
33 |
config.localization.enable_thai_normalization = True
|
34 |
config.retriever.top_k = 5
|
35 |
config.model.temperature = 0.3
|
36 |
|
37 |
-
# Initialize pipeline
|
38 |
pipeline = AcademicCalendarRAG(config)
|
39 |
|
40 |
-
# Load calendar data
|
41 |
with open("calendar.json", "r", encoding="utf-8") as f:
|
42 |
calendar_data = json.load(f)
|
43 |
pipeline.load_data(calendar_data)
|
@@ -49,120 +164,161 @@ def initialize_pipeline():
|
|
49 |
return None
|
50 |
|
51 |
def display_chat_history():
|
52 |
-
"""Display chat history with
|
53 |
-
for
|
54 |
if role == "user":
|
55 |
-
st.markdown(f"
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
else:
|
58 |
-
st.markdown(f"
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.session_state.chat_history.append((role, message))
|
65 |
|
66 |
def process_query(query: str):
|
67 |
"""Process a query and update the chat history"""
|
68 |
if not query:
|
69 |
-
st.warning("กรุณาระบุคำถาม")
|
70 |
return
|
71 |
|
72 |
if st.session_state.pipeline is None:
|
73 |
-
st.error("ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
74 |
return
|
75 |
|
76 |
-
|
77 |
-
add_to_history("user", query)
|
78 |
|
79 |
try:
|
80 |
-
with st.spinner("กำลังค้นหาคำตอบ..."):
|
81 |
-
# Process query
|
82 |
result = st.session_state.pipeline.process_query(query)
|
|
|
83 |
|
84 |
-
|
85 |
-
add_to_history("assistant", result["answer"])
|
86 |
-
|
87 |
-
# Create containers for additional information
|
88 |
-
with st.expander("📚 แสดงข้อมูลอ้างอิง"):
|
89 |
for i, doc in enumerate(result["documents"], 1):
|
90 |
-
st.markdown(f"
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
93 |
|
94 |
-
with st.expander("🔍
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
except Exception as e:
|
98 |
-
st.error(f"เกิดข้อผิดพลาด: {str(e)}")
|
99 |
|
100 |
def main():
|
101 |
-
# Header
|
102 |
-
st.
|
103 |
-
|
|
|
|
|
|
|
|
|
104 |
|
105 |
# Initialize pipeline if needed
|
106 |
if st.session_state.pipeline is None:
|
107 |
-
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
108 |
st.session_state.pipeline = initialize_pipeline()
|
109 |
|
110 |
-
#
|
111 |
chat_col, info_col = st.columns([2, 1])
|
112 |
|
113 |
with chat_col:
|
114 |
-
|
115 |
display_chat_history()
|
116 |
|
117 |
-
# Initialize query input in session state if not present
|
118 |
-
if 'current_query' not in st.session_state:
|
119 |
-
st.session_state.current_query = ''
|
120 |
-
|
121 |
-
# Main query interface
|
122 |
query = st.text_input(
|
123 |
-
"
|
124 |
value=st.session_state.current_query,
|
125 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
126 |
key="query_input"
|
127 |
)
|
128 |
-
|
129 |
-
# Update current query in session state
|
130 |
st.session_state.current_query = query
|
131 |
-
|
132 |
-
# Add query button and clear chat button in the same line
|
133 |
col1, col2 = st.columns([1, 4])
|
134 |
with col1:
|
135 |
-
if st.button("ส่งคำถาม", type="primary", use_container_width=True):
|
136 |
-
if query:
|
137 |
process_query(query)
|
138 |
-
# Clear the input only after successful processing
|
139 |
st.session_state.current_query = ''
|
140 |
st.rerun()
|
141 |
with col2:
|
142 |
-
if st.button("
|
143 |
st.session_state.chat_history = []
|
144 |
st.session_state.current_query = ''
|
145 |
st.rerun()
|
|
|
146 |
|
147 |
with info_col:
|
148 |
-
# System
|
149 |
-
st.markdown("### ℹ️ เกี่ยวกับระบบ")
|
150 |
st.markdown("""
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
if __name__ == "__main__":
|
168 |
main()
|
|
|
8 |
PipelineConfig
|
9 |
)
|
10 |
|
11 |
+
# Custom CSS for enhanced styling
|
12 |
st.set_page_config(
|
13 |
page_title="Academic Calendar Assistant",
|
14 |
page_icon="📅",
|
15 |
layout="wide"
|
16 |
)
|
17 |
|
18 |
+
# Apply custom CSS
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
/* Modern color scheme */
|
22 |
+
:root {
|
23 |
+
--primary-color: #2563eb;
|
24 |
+
--secondary-color: #1e40af;
|
25 |
+
--background-color: #f8fafc;
|
26 |
+
--text-color: #1e293b;
|
27 |
+
--accent-color: #3b82f6;
|
28 |
+
}
|
29 |
+
|
30 |
+
/* Main container styling */
|
31 |
+
.main {
|
32 |
+
background-color: var(--background-color);
|
33 |
+
padding: 2rem;
|
34 |
+
border-radius: 1rem;
|
35 |
+
}
|
36 |
+
|
37 |
+
/* Header styling */
|
38 |
+
.stTitle {
|
39 |
+
color: var(--primary-color) !important;
|
40 |
+
font-size: 2.5rem !important;
|
41 |
+
font-weight: 700 !important;
|
42 |
+
margin-bottom: 1rem !important;
|
43 |
+
text-align: center;
|
44 |
+
padding: 2rem 0;
|
45 |
+
background: linear-gradient(135deg, #2563eb 0%, #1e40af 100%);
|
46 |
+
-webkit-background-clip: text;
|
47 |
+
-webkit-text-fill-color: transparent;
|
48 |
+
}
|
49 |
+
|
50 |
+
/* Card styling */
|
51 |
+
.stMarkdown {
|
52 |
+
background-color: white;
|
53 |
+
padding: 1.5rem;
|
54 |
+
border-radius: 0.75rem;
|
55 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
56 |
+
margin-bottom: 1rem;
|
57 |
+
}
|
58 |
+
|
59 |
+
/* Button styling */
|
60 |
+
.stButton > button {
|
61 |
+
border-radius: 0.5rem !important;
|
62 |
+
padding: 0.75rem 1.5rem !important;
|
63 |
+
font-weight: 600 !important;
|
64 |
+
transition: all 0.3s ease !important;
|
65 |
+
}
|
66 |
+
|
67 |
+
.stButton > button:hover {
|
68 |
+
transform: translateY(-2px);
|
69 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
70 |
+
}
|
71 |
+
|
72 |
+
/* Input field styling */
|
73 |
+
.stTextInput > div > div > input {
|
74 |
+
border-radius: 0.5rem !important;
|
75 |
+
border: 2px solid #e2e8f0 !important;
|
76 |
+
padding: 1rem !important;
|
77 |
+
font-size: 1rem !important;
|
78 |
+
transition: all 0.3s ease !important;
|
79 |
+
}
|
80 |
+
|
81 |
+
.stTextInput > div > div > input:focus {
|
82 |
+
border-color: var(--primary-color) !important;
|
83 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2) !important;
|
84 |
+
}
|
85 |
+
|
86 |
+
/* Chat message styling */
|
87 |
+
.user-message {
|
88 |
+
background-color: #f1f5f9;
|
89 |
+
padding: 1rem;
|
90 |
+
border-radius: 0.5rem;
|
91 |
+
margin: 0.5rem 0;
|
92 |
+
border-left: 4px solid var(--primary-color);
|
93 |
+
}
|
94 |
+
|
95 |
+
.assistant-message {
|
96 |
+
background-color: white;
|
97 |
+
padding: 1rem;
|
98 |
+
border-radius: 0.5rem;
|
99 |
+
margin: 0.5rem 0;
|
100 |
+
border-left: 4px solid var(--accent-color);
|
101 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
102 |
+
}
|
103 |
+
|
104 |
+
/* Expander styling */
|
105 |
+
.streamlit-expander {
|
106 |
+
border: none !important;
|
107 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
108 |
+
border-radius: 0.5rem !important;
|
109 |
+
margin: 1rem 0 !important;
|
110 |
+
}
|
111 |
+
|
112 |
+
/* Status indicator styling */
|
113 |
+
.status-indicator {
|
114 |
+
display: inline-block;
|
115 |
+
width: 10px;
|
116 |
+
height: 10px;
|
117 |
+
border-radius: 50%;
|
118 |
+
margin-right: 0.5rem;
|
119 |
+
}
|
120 |
+
|
121 |
+
.status-online {
|
122 |
+
background-color: #22c55e;
|
123 |
+
}
|
124 |
+
|
125 |
+
/* Container styling */
|
126 |
+
.content-container {
|
127 |
+
background-color: white;
|
128 |
+
padding: 2rem;
|
129 |
+
border-radius: 1rem;
|
130 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
131 |
+
}
|
132 |
+
</style>
|
133 |
+
""", unsafe_allow_html=True)
|
134 |
+
|
135 |
+
# Initialize session states
|
136 |
if 'pipeline' not in st.session_state:
|
137 |
st.session_state.pipeline = None
|
138 |
|
139 |
if 'chat_history' not in st.session_state:
|
140 |
st.session_state.chat_history = []
|
141 |
+
|
142 |
+
if 'current_query' not in st.session_state:
|
143 |
+
st.session_state.current_query = ''
|
144 |
|
145 |
def initialize_pipeline():
|
146 |
"""Initialize RAG pipeline with configurations"""
|
147 |
try:
|
|
|
148 |
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
|
|
|
|
|
149 |
config = create_default_config(openai_api_key)
|
150 |
config.localization.enable_thai_normalization = True
|
151 |
config.retriever.top_k = 5
|
152 |
config.model.temperature = 0.3
|
153 |
|
|
|
154 |
pipeline = AcademicCalendarRAG(config)
|
155 |
|
|
|
156 |
with open("calendar.json", "r", encoding="utf-8") as f:
|
157 |
calendar_data = json.load(f)
|
158 |
pipeline.load_data(calendar_data)
|
|
|
164 |
return None
|
165 |
|
166 |
def display_chat_history():
|
167 |
+
"""Display chat history with enhanced styling"""
|
168 |
+
for role, message in st.session_state.chat_history:
|
169 |
if role == "user":
|
170 |
+
st.markdown(f"""
|
171 |
+
<div class="user-message">
|
172 |
+
<strong>🧑 คำถาม:</strong><br>
|
173 |
+
{message}
|
174 |
+
</div>
|
175 |
+
""", unsafe_allow_html=True)
|
176 |
else:
|
177 |
+
st.markdown(f"""
|
178 |
+
<div class="assistant-message">
|
179 |
+
<strong>🤖 คำตอบ:</strong><br>
|
180 |
+
{message}
|
181 |
+
</div>
|
182 |
+
""", unsafe_allow_html=True)
|
|
|
183 |
|
184 |
def process_query(query: str):
|
185 |
"""Process a query and update the chat history"""
|
186 |
if not query:
|
187 |
+
st.warning("⚠️ กรุณาระบุคำถาม")
|
188 |
return
|
189 |
|
190 |
if st.session_state.pipeline is None:
|
191 |
+
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
192 |
return
|
193 |
|
194 |
+
st.session_state.chat_history.append(("user", query))
|
|
|
195 |
|
196 |
try:
|
197 |
+
with st.spinner("🔄 กำลังค้นหาคำตอบ..."):
|
|
|
198 |
result = st.session_state.pipeline.process_query(query)
|
199 |
+
st.session_state.chat_history.append(("assistant", result["answer"]))
|
200 |
|
201 |
+
with st.expander("📚 **ข้อมูลอ้างอิง**", expanded=False):
|
|
|
|
|
|
|
|
|
202 |
for i, doc in enumerate(result["documents"], 1):
|
203 |
+
st.markdown(f"""
|
204 |
+
<div class="content-container">
|
205 |
+
<h3>เอกสารที่ {i}</h3>
|
206 |
+
<p>{doc.content}</p>
|
207 |
+
</div>
|
208 |
+
""", unsafe_allow_html=True)
|
209 |
|
210 |
+
with st.expander("🔍 **การวิเคราะห์คำถาม**", expanded=False):
|
211 |
+
analysis = result["query_info"]
|
212 |
+
col1, col2 = st.columns(2)
|
213 |
+
|
214 |
+
with col1:
|
215 |
+
st.markdown("""
|
216 |
+
<div class="content-container">
|
217 |
+
<h3>ประเภทคำถาม</h3>
|
218 |
+
<p>{}</p>
|
219 |
+
<h3>ภาคการศึกษา</h3>
|
220 |
+
<p>{}</p>
|
221 |
+
</div>
|
222 |
+
""".format(
|
223 |
+
analysis.get('event_type', 'ไม่ระบุ'),
|
224 |
+
analysis.get('semester', 'ไม่ระบุ')
|
225 |
+
), unsafe_allow_html=True)
|
226 |
+
|
227 |
+
with col2:
|
228 |
+
st.markdown("""
|
229 |
+
<div class="content-container">
|
230 |
+
<h3>คำสำคัญ</h3>
|
231 |
+
<p>{}</p>
|
232 |
+
<h3>รูปแบบการตอบ</h3>
|
233 |
+
<p>{}</p>
|
234 |
+
</div>
|
235 |
+
""".format(
|
236 |
+
', '.join(analysis.get('key_terms', ['ไม่พบคำสำคัญ'])),
|
237 |
+
analysis.get('response_format', 'ไม่ระบุ')
|
238 |
+
), unsafe_allow_html=True)
|
239 |
|
240 |
except Exception as e:
|
241 |
+
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
242 |
|
243 |
def main():
|
244 |
+
# Header with gradient background
|
245 |
+
st.markdown("""
|
246 |
+
<div style="text-align: center; padding: 2rem 0; background: linear-gradient(135deg, #2563eb 0%, #1e40af 100%); margin-bottom: 2rem; border-radius: 1rem;">
|
247 |
+
<h1 style="color: white; font-size: 2.5rem; font-weight: 700;">🎓 ระบบค้นหาข้อมูลปฏิทินการศึ��ษา</h1>
|
248 |
+
<p style="color: white; font-size: 1.2rem;">ค้นหาข้อมูลและกำหนดการต่างๆ ในปฏิทินการศึกษาได้อย่างง่ายดาย</p>
|
249 |
+
</div>
|
250 |
+
""", unsafe_allow_html=True)
|
251 |
|
252 |
# Initialize pipeline if needed
|
253 |
if st.session_state.pipeline is None:
|
254 |
+
with st.spinner("🚀 กำลังเริ่มต้นระบบ..."):
|
255 |
st.session_state.pipeline = initialize_pipeline()
|
256 |
|
257 |
+
# Main content area
|
258 |
chat_col, info_col = st.columns([2, 1])
|
259 |
|
260 |
with chat_col:
|
261 |
+
st.markdown('<div class="content-container">', unsafe_allow_html=True)
|
262 |
display_chat_history()
|
263 |
|
|
|
|
|
|
|
|
|
|
|
264 |
query = st.text_input(
|
265 |
+
"โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา",
|
266 |
value=st.session_state.current_query,
|
267 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
268 |
key="query_input"
|
269 |
)
|
270 |
+
|
|
|
271 |
st.session_state.current_query = query
|
272 |
+
|
|
|
273 |
col1, col2 = st.columns([1, 4])
|
274 |
with col1:
|
275 |
+
if st.button("🚀 ส่งคำถาม", type="primary", use_container_width=True):
|
276 |
+
if query:
|
277 |
process_query(query)
|
|
|
278 |
st.session_state.current_query = ''
|
279 |
st.rerun()
|
280 |
with col2:
|
281 |
+
if st.button("🗑️ ล้างประวัติ", type="secondary", use_container_width=True):
|
282 |
st.session_state.chat_history = []
|
283 |
st.session_state.current_query = ''
|
284 |
st.rerun()
|
285 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
286 |
|
287 |
with info_col:
|
288 |
+
# System Information Card
|
|
|
289 |
st.markdown("""
|
290 |
+
<div class="content-container">
|
291 |
+
<h2 style="color: var(--primary-color); font-size: 1.5rem; margin-bottom: 1rem;">
|
292 |
+
ℹ️ เกี่ยวกับระบบ
|
293 |
+
</h2>
|
294 |
+
<p style="margin-bottom: 1rem;">
|
295 |
+
ระบบนี้ใช้เทคโนโลยี <strong>RAG (Retrieval-Augmented Generation)</strong>
|
296 |
+
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
|
297 |
+
</p>
|
298 |
+
<h3 style="font-size: 1.2rem; margin: 1rem 0;">สามารถสอบถามข้อมูลเกี่ยวกับ:</h3>
|
299 |
+
<ul style="list-style-type: none; padding-left: 0;">
|
300 |
+
<li style="margin: 0.5rem 0;">📅 กำหนดการต่างๆ ในปฏิทินการศึกษา</li>
|
301 |
+
<li style="margin: 0.5rem 0;">🎯 วันสำคัญและกิจกรรม</li>
|
302 |
+
<li style="margin: 0.5rem 0;">📝 การลงทะเบียนเรียน</li>
|
303 |
+
<li style="margin: 0.5rem 0;">📚 กำหนดการสอบ</li>
|
304 |
+
<li style="margin: 0.5rem 0;">🏖️ วันหยุดการศึกษ���</li>
|
305 |
+
</ul>
|
306 |
+
</div>
|
307 |
|
308 |
+
<div class="content-container" style="margin-top: 1rem;">
|
309 |
+
<h2 style="color: var(--primary-color); font-size: 1.5rem; margin-bottom: 1rem;">
|
310 |
+
🔄 สถานะระบบ
|
311 |
+
</h2>
|
312 |
+
<p>
|
313 |
+
<span class="status-indicator status-online"></span>
|
314 |
+
<strong>สถานะ:</strong> {'🟢 พร้อมใช้งาน' if st.session_state.pipeline else '🔴 ไม่พร้อมใช้งาน'}
|
315 |
+
</p>
|
316 |
+
<p style="margin-top: 0.5rem;">
|
317 |
+
<strong>⏰ เวลาปัจจุบัน:</strong><br>
|
318 |
+
{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
319 |
+
</p>
|
320 |
+
</div>
|
321 |
+
""", unsafe_allow_html=True)
|
322 |
|
323 |
if __name__ == "__main__":
|
324 |
main()
|