Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
4 |
-
from
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Page config
|
7 |
st.set_page_config(
|
@@ -10,9 +15,12 @@ st.set_page_config(
|
|
10 |
layout="wide"
|
11 |
)
|
12 |
|
13 |
-
# Initialize session state
|
14 |
if 'pipeline' not in st.session_state:
|
15 |
st.session_state.pipeline = None
|
|
|
|
|
|
|
16 |
|
17 |
def initialize_pipeline():
|
18 |
"""Initialize RAG pipeline with configurations"""
|
@@ -20,10 +28,14 @@ def initialize_pipeline():
|
|
20 |
# Get OpenAI API key from environment variable or Streamlit secrets
|
21 |
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
|
22 |
|
23 |
-
# Create config
|
24 |
config = create_default_config(openai_api_key)
|
25 |
config.localization.enable_thai_normalization = True
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Load calendar data
|
29 |
with open("calendar.json", "r", encoding="utf-8") as f:
|
@@ -36,6 +48,21 @@ def initialize_pipeline():
|
|
36 |
st.error(f"Error initializing pipeline: {str(e)}")
|
37 |
return None
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def main():
|
40 |
# Header
|
41 |
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา")
|
@@ -46,59 +73,87 @@ def main():
|
|
46 |
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
47 |
st.session_state.pipeline = initialize_pipeline()
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
st.
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
# Display answer
|
71 |
-
st.markdown("### 📝 คำตอบ")
|
72 |
-
st.write(result["answer"])
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
for i, doc in enumerate(result["documents"], 1):
|
77 |
-
st.markdown(f"**เอกสารที่ {i}:**")
|
78 |
-
st.text(doc.content)
|
79 |
-
st.markdown("---")
|
80 |
|
81 |
-
|
82 |
-
with st.
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
st.markdown("###
|
91 |
st.markdown("""
|
92 |
ระบบนี้ใช้เทคโนโลยี RAG (Retrieval-Augmented Generation)
|
93 |
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
|
94 |
|
95 |
สามารถสอบถามข้อมูลเกี่ยวกับ:
|
96 |
-
- กำหนดการต่างๆ
|
97 |
-
-
|
98 |
-
-
|
99 |
-
-
|
100 |
-
-
|
101 |
""")
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
4 |
+
from datetime import datetime
|
5 |
+
from calendar_rag import (
|
6 |
+
create_default_config,
|
7 |
+
AcademicCalendarRAG,
|
8 |
+
PipelineConfig
|
9 |
+
)
|
10 |
|
11 |
# Page config
|
12 |
st.set_page_config(
|
|
|
15 |
layout="wide"
|
16 |
)
|
17 |
|
18 |
+
# Initialize session state for pipeline and chat history
|
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"""
|
|
|
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:
|
|
|
48 |
st.error(f"Error initializing pipeline: {str(e)}")
|
49 |
return None
|
50 |
|
51 |
+
def display_chat_history():
|
52 |
+
"""Display chat history with user and assistant messages"""
|
53 |
+
for i, (role, message) in enumerate(st.session_state.chat_history):
|
54 |
+
if role == "user":
|
55 |
+
st.markdown(f"**🧑 คำถาม:**")
|
56 |
+
st.markdown(f"> {message}")
|
57 |
+
else:
|
58 |
+
st.markdown(f"**🤖 คำตอบ:**")
|
59 |
+
st.markdown(message)
|
60 |
+
st.markdown("---")
|
61 |
+
|
62 |
+
def add_to_history(role: str, message: str):
|
63 |
+
"""Add message to chat history"""
|
64 |
+
st.session_state.chat_history.append((role, message))
|
65 |
+
|
66 |
def main():
|
67 |
# Header
|
68 |
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา")
|
|
|
73 |
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
74 |
st.session_state.pipeline = initialize_pipeline()
|
75 |
|
76 |
+
# Create two columns for chat layout
|
77 |
+
chat_col, info_col = st.columns([2, 1])
|
78 |
+
|
79 |
+
with chat_col:
|
80 |
+
# Display chat history
|
81 |
+
display_chat_history()
|
82 |
+
|
83 |
+
# Main query interface
|
84 |
+
query = st.text_input(
|
85 |
+
"โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:",
|
86 |
+
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
87 |
+
key="query_input"
|
88 |
+
)
|
89 |
|
90 |
+
# Add query button and clear chat button in the same line
|
91 |
+
col1, col2 = st.columns([1, 4])
|
92 |
+
with col1:
|
93 |
+
send_query = st.button("ส่งคำถาม", type="primary", use_container_width=True)
|
94 |
+
with col2:
|
95 |
+
if st.button("ล้างประวัติการสนทนา", type="secondary", use_container_width=True):
|
96 |
+
st.session_state.chat_history = []
|
97 |
+
st.rerun()
|
98 |
+
|
99 |
+
# Process query when button is clicked
|
100 |
+
if send_query and query:
|
101 |
+
if st.session_state.pipeline is None:
|
102 |
+
st.error("ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
103 |
+
return
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
# Add user query to history
|
106 |
+
add_to_history("user", query)
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
try:
|
109 |
+
with st.spinner("กำลังค้นหาคำตอบ..."):
|
110 |
+
# Process query
|
111 |
+
result = st.session_state.pipeline.process_query(query)
|
112 |
+
|
113 |
+
# Add assistant response to history
|
114 |
+
add_to_history("assistant", result["answer"])
|
115 |
+
|
116 |
+
# Create containers for additional information
|
117 |
+
with st.expander("📚 แสดงข้อมูลอ้างอิง"):
|
118 |
+
for i, doc in enumerate(result["documents"], 1):
|
119 |
+
st.markdown(f"**เอกสารที่ {i}:**")
|
120 |
+
st.text(doc.content)
|
121 |
+
st.markdown("---")
|
122 |
+
|
123 |
+
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม"):
|
124 |
+
st.json(result["query_info"])
|
125 |
+
|
126 |
+
# Clear the input field
|
127 |
+
st.session_state.query_input = ""
|
128 |
|
129 |
+
# Rerun to update chat display
|
130 |
+
st.rerun()
|
131 |
+
|
132 |
+
except Exception as e:
|
133 |
+
st.error(f"เกิดข้อผิดพลาด: {str(e)}")
|
134 |
+
|
135 |
+
elif send_query and not query:
|
136 |
+
st.warning("กรุณาระบุคำถาม")
|
137 |
|
138 |
+
with info_col:
|
139 |
+
# System information
|
140 |
+
st.markdown("### ℹ️ เกี่ยวกับระบบ")
|
141 |
st.markdown("""
|
142 |
ระบบนี้ใช้เทคโนโลยี RAG (Retrieval-Augmented Generation)
|
143 |
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
|
144 |
|
145 |
สามารถสอบถามข้อมูลเกี่ยวกับ:
|
146 |
+
- กำหนดการต่างๆ ในปฏิทินการศึกษา
|
147 |
+
- วันสำคัญและกิจกรรม
|
148 |
+
- การลงทะเบียนเรียน
|
149 |
+
- กำหนดการสอบ
|
150 |
+
- วันหยุดการศึกษา
|
151 |
""")
|
152 |
+
|
153 |
+
# System status
|
154 |
+
st.markdown("### 🔄 สถานะระบบ")
|
155 |
+
st.markdown(f"**เวลาปัจจุบัน:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
156 |
+
st.markdown(f"**สถานะระบบ:** {'🟢 พร้อมใช้งาน' if st.session_state.pipeline else '🔴 ไม่พร้อมใช้งาน'}")
|
157 |
|
158 |
if __name__ == "__main__":
|
159 |
main()
|