JirasakJo commited on
Commit
b5ab9c7
·
verified ·
1 Parent(s): 9162983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +210 -255
app.py CHANGED
@@ -9,138 +9,116 @@ from calendar_rag import (
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"""
@@ -150,7 +128,6 @@ def initialize_pipeline():
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:
@@ -165,161 +142,139 @@ def initialize_pipeline():
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()
325
-
 
9
  )
10
 
11
  # Custom CSS for enhanced styling
12
+ def load_custom_css():
13
+ st.markdown("""
14
+ <style>
15
+ /* Main container styling */
16
+ .main {
17
+ padding: 2rem;
18
+ }
19
+
20
+ /* Headers styling */
21
+ h1 {
22
+ color: #1E3A8A;
23
+ font-size: 2.5rem !important;
24
+ font-weight: 700 !important;
25
+ margin-bottom: 1.5rem !important;
26
+ text-align: center;
27
+ padding: 1rem 0;
28
+ border-bottom: 3px solid #1E3A8A;
29
+ }
30
+
31
+ h3 {
32
+ color: #1E3A8A;
33
+ font-weight: 600 !important;
34
+ margin-top: 1.5rem !important;
35
+ }
36
+
37
+ /* Chat message styling */
38
+ .chat-message {
39
+ padding: 1.5rem;
40
+ border-radius: 10px;
41
+ margin: 1rem 0;
42
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
43
+ }
44
+
45
+ .user-message {
46
+ background-color: #F3F4F6;
47
+ }
48
+
49
+ .assistant-message {
50
+ background-color: #EFF6FF;
51
+ }
52
+
53
+ /* Input field styling */
54
+ .stTextInput input {
55
+ border-radius: 8px;
56
+ border: 2px solid #E5E7EB;
57
+ padding: 0.75rem;
58
+ font-size: 1rem;
59
+ }
60
+
61
+ .stTextInput input:focus {
62
+ border-color: #1E3A8A;
63
+ box-shadow: 0 0 0 2px rgba(30, 58, 138, 0.1);
64
+ }
65
+
66
+ /* Button styling */
67
+ .stButton button {
68
+ border-radius: 8px;
69
+ font-weight: 600;
70
+ transition: all 0.2s ease;
71
+ }
72
+
73
+ .stButton button:hover {
74
+ transform: translateY(-1px);
75
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
76
+ }
77
+
78
+ /* Expander styling */
79
+ .streamlit-expanderHeader {
80
+ background-color: #F9FAFB;
81
+ border-radius: 8px;
82
+ padding: 0.75rem !important;
83
+ font-weight: 600;
84
+ }
85
+
86
+ /* Status indicator styling */
87
+ .status-indicator {
88
+ padding: 0.5rem 1rem;
89
+ border-radius: 6px;
90
+ font-weight: 500;
91
+ }
92
+
93
+ .status-online {
94
+ background-color: #DEF7EC;
95
+ color: #03543F;
96
+ }
97
+
98
+ .status-offline {
99
+ background-color: #FDE8E8;
100
+ color: #9B1C1C;
101
+ }
102
+ </style>
103
+ """, unsafe_allow_html=True)
104
+
105
+ # Page config
106
  st.set_page_config(
107
  page_title="Academic Calendar Assistant",
108
  page_icon="📅",
109
+ layout="wide",
110
+ initial_sidebar_state="collapsed"
111
  )
112
 
113
+ # Load custom CSS
114
+ load_custom_css()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
+ # Initialize session state
117
  if 'pipeline' not in st.session_state:
118
  st.session_state.pipeline = None
119
 
120
  if 'chat_history' not in st.session_state:
121
  st.session_state.chat_history = []
 
 
 
122
 
123
  def initialize_pipeline():
124
  """Initialize RAG pipeline with configurations"""
 
128
  config.localization.enable_thai_normalization = True
129
  config.retriever.top_k = 5
130
  config.model.temperature = 0.3
 
131
  pipeline = AcademicCalendarRAG(config)
132
 
133
  with open("calendar.json", "r", encoding="utf-8") as f:
 
142
 
143
  def display_chat_history():
144
  """Display chat history with enhanced styling"""
145
+ for i, (role, message) in enumerate(st.session_state.chat_history):
146
  if role == "user":
147
  st.markdown(f"""
148
+ <div class="chat-message user-message">
149
+ <strong>🧑 คำถาม:</strong><br>
150
+ {message}
151
+ </div>
152
  """, unsafe_allow_html=True)
153
  else:
154
  st.markdown(f"""
155
+ <div class="chat-message assistant-message">
156
+ <strong>🤖 คำตอบ:</strong><br>
157
+ {message}
158
+ </div>
159
  """, unsafe_allow_html=True)
160
 
161
+ def add_to_history(role: str, message: str):
162
+ """Add message to chat history"""
163
+ st.session_state.chat_history.append((role, message))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
  def main():
166
+ # Header with animation
167
  st.markdown("""
168
+ <div style="text-align: center; padding: 2rem 0;">
169
+ <h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
170
+ </div>
 
171
  """, unsafe_allow_html=True)
172
 
173
  # Initialize pipeline if needed
174
  if st.session_state.pipeline is None:
175
+ with st.spinner("กำลังเริ่มต้นระบบ..."):
176
  st.session_state.pipeline = initialize_pipeline()
177
 
178
+ # Create two columns with better proportions
179
+ chat_col, info_col = st.columns([7, 3])
180
 
181
  with chat_col:
182
+ # Add a subtle container for the chat interface
183
+ with st.container():
184
+ # Display chat history
185
+ display_chat_history()
186
+
187
+ # Main query interface with enhanced styling
188
+ st.markdown("<br>", unsafe_allow_html=True)
189
+ query = st.text_input(
190
+ "โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:",
191
+ placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
192
+ key="query_input"
193
+ )
194
+
195
+ # Button layout with enhanced styling
196
+ col1, col2, col3 = st.columns([1, 1, 4])
197
+ with col1:
198
+ send_query = st.button("📤 ส่งคำถาม", type="primary", use_container_width=True)
199
+ with col2:
200
+ if st.button("🗑️ ล้างประวัติ", type="secondary", use_container_width=True):
201
+ st.session_state.chat_history = []
202
  st.rerun()
203
+
204
+ # Process query
205
+ if send_query and query:
206
+ if st.session_state.pipeline is None:
207
+ st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
208
+ return
209
+
210
+ add_to_history("user", query)
211
+
212
+ try:
213
+ with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
214
+ result = st.session_state.pipeline.process_query(query)
215
+ add_to_history("assistant", result["answer"])
216
+
217
+ # Enhanced expandable sections
218
+ with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
219
+ for i, doc in enumerate(result["documents"], 1):
220
+ st.markdown(f"""
221
+ <div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
222
+ <strong>เอกสารที่ {i}:</strong><br>
223
+ {doc.content}
224
+ </div>
225
+ """, unsafe_allow_html=True)
226
+
227
+ with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
228
+ st.json(result["query_info"])
229
+
230
+ st.session_state.query_input = ""
231
+ st.rerun()
232
+
233
+ except Exception as e:
234
+ st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
235
+
236
+ elif send_query and not query:
237
+ st.warning("⚠️ กรุณาระบุคำถาม")
238
 
239
  with info_col:
240
+ # Enhanced system information section
241
  st.markdown("""
242
+ <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
243
+ <h3>ℹ️ เกี่ยวกับระบบ</h3>
244
+ <p style="color: #4B5563;">
245
+ ระบบนี้ใช้เทคโนโลยี <strong>RAG (Retrieval-Augmented Generation)</strong>
246
+ ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
247
+ </p>
248
+ <h4 style="color: #1E3A8A; margin-top: 1rem;">สามารถสอบถามข้อมูลเกี่ยวกับ:</h4>
249
+ <ul style="list-style-type: none; padding-left: 0;">
250
+ <li>📅 กำหนดการต่างๆ ในปฏิทินการศึกษา</li>
251
+ <li>🎯 วันสำคัญและกิจกรรม</li>
252
+ <li>📝 การลงทะเบียนเรียน</li>
253
+ <li>📚 กำหนดการสอบ</li>
254
+ <li>🏖️ วันหยุดการศึกษา</li>
255
+ </ul>
256
+ </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  """, unsafe_allow_html=True)
258
+
259
+ # Enhanced system status section
260
+ st.markdown("""
261
+ <div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px;">
262
+ <h3>🔄 สถานะระบบ</h3>
263
+ <div style="margin-top: 1rem;">
264
+ <p><strong>⏰ เวลาปัจจุบัน:</strong><br>
265
+ {}</p>
266
+ <p><strong>📡 สถานะระบบ:</strong><br>
267
+ <span class="status-indicator {}">
268
+ {} {}
269
+ </span></p>
270
+ </div>
271
+ </div>
272
+ """.format(
273
+ datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
274
+ "status-online" if st.session_state.pipeline else "status-offline",
275
+ "🟢" if st.session_state.pipeline else "🔴",
276
+ "พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"
277
+ ), unsafe_allow_html=True)
278
 
279
  if __name__ == "__main__":
280
+ main()