Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
13 |
+
page_title="Academic Calendar Assistant",
|
14 |
+
page_icon="📅",
|
15 |
+
layout="wide"
|
16 |
+
)
|
17 |
+
|
18 |
+
# Initialize session state
|
19 |
+
if 'pipeline' not in st.session_state:
|
20 |
+
st.session_state.pipeline = None
|
21 |
+
|
22 |
+
def initialize_pipeline():
|
23 |
+
"""Initialize RAG pipeline with configurations"""
|
24 |
+
try:
|
25 |
+
# Get OpenAI API key from environment variable or Streamlit secrets
|
26 |
+
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
|
27 |
+
|
28 |
+
# Create config
|
29 |
+
config = create_default_config(openai_api_key)
|
30 |
+
config.localization.enable_thai_normalization = True
|
31 |
+
config.retriever.top_k = 5
|
32 |
+
config.model.temperature = 0.3
|
33 |
+
|
34 |
+
# Initialize pipeline
|
35 |
+
pipeline = AcademicCalendarRAG(config)
|
36 |
+
|
37 |
+
# Load calendar data
|
38 |
+
with open("calendar.json", "r", encoding="utf-8") as f:
|
39 |
+
calendar_data = json.load(f)
|
40 |
+
pipeline.load_data(calendar_data)
|
41 |
+
|
42 |
+
return pipeline
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"Error initializing pipeline: {str(e)}")
|
46 |
+
return None
|
47 |
+
|
48 |
+
def main():
|
49 |
+
# Header
|
50 |
+
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา")
|
51 |
+
st.markdown("---")
|
52 |
+
|
53 |
+
# Initialize pipeline if needed
|
54 |
+
if st.session_state.pipeline is None:
|
55 |
+
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
56 |
+
st.session_state.pipeline = initialize_pipeline()
|
57 |
+
|
58 |
+
# Main query interface
|
59 |
+
query = st.text_input(
|
60 |
+
"โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:",
|
61 |
+
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
|
62 |
+
)
|
63 |
+
|
64 |
+
# Add query button
|
65 |
+
if st.button("ค้นหา", type="primary"):
|
66 |
+
if not query:
|
67 |
+
st.warning("กรุณาระบุคำถาม")
|
68 |
+
return
|
69 |
+
|
70 |
+
if st.session_state.pipeline is None:
|
71 |
+
st.error("ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
72 |
+
return
|
73 |
+
|
74 |
+
try:
|
75 |
+
with st.spinner("กำลังค้นหาคำตอบ..."):
|
76 |
+
# Process query
|
77 |
+
result = st.session_state.pipeline.process_query(query)
|
78 |
+
|
79 |
+
# Display answer
|
80 |
+
st.markdown("### 📝 คำตอบ")
|
81 |
+
st.write(result["answer"])
|
82 |
+
|
83 |
+
# Display relevant documents in expander
|
84 |
+
with st.expander("แสดงข้อมูลอ้างอิง"):
|
85 |
+
for i, doc in enumerate(result["documents"], 1):
|
86 |
+
st.markdown(f"**เอกสารที่ {i}:**")
|
87 |
+
st.text(doc.content)
|
88 |
+
st.markdown("---")
|
89 |
+
|
90 |
+
# Display query understanding
|
91 |
+
with st.expander("รายละเอียดการวิเคราะห์คำถาม"):
|
92 |
+
st.json(result["query_info"])
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
st.error(f"เกิดข้อผิดพลาด: {str(e)}")
|
96 |
+
|
97 |
+
# Add sidebar with information
|
98 |
+
with st.sidebar:
|
99 |
+
st.markdown("### 📚 เกี่ยวกับระบบ")
|
100 |
+
st.markdown("""
|
101 |
+
ระบบนี้ใช้เทคโนโลยี RAG (Retrieval-Augmented Generation)
|
102 |
+
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
|
103 |
+
|
104 |
+
สามารถสอบถามข้อมูลเกี่ยวกับ:
|
105 |
+
- กำหนดการต่างๆ
|
106 |
+
- วันสำคัญ
|
107 |
+
- การลงทะเบียน
|
108 |
+
- การสอบ
|
109 |
+
- วันหยุด
|
110 |
+
""")
|
111 |
+
|
112 |
+
st.markdown("### ⏰ สถานะระบบ")
|
113 |
+
st.write(f"Last Updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
main()
|