|
import streamlit as st |
|
import json |
|
import os |
|
from calendar_rag import create_default_config, CalendarRAG |
|
|
|
|
|
st.set_page_config( |
|
page_title="Academic Calendar Assistant", |
|
page_icon="📅", |
|
layout="wide" |
|
) |
|
|
|
|
|
if 'pipeline' not in st.session_state: |
|
st.session_state.pipeline = None |
|
|
|
def initialize_pipeline(): |
|
"""Initialize RAG pipeline with configurations""" |
|
try: |
|
|
|
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY'] |
|
|
|
|
|
config = create_default_config(openai_api_key) |
|
config.localization.enable_thai_normalization = True |
|
pipeline = CalendarRAG(config) |
|
|
|
|
|
with open("calendar.json", "r", encoding="utf-8") as f: |
|
calendar_data = json.load(f) |
|
pipeline.load_data(calendar_data) |
|
|
|
return pipeline |
|
|
|
except Exception as e: |
|
st.error(f"Error initializing pipeline: {str(e)}") |
|
return None |
|
|
|
def main(): |
|
|
|
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา") |
|
st.markdown("---") |
|
|
|
|
|
if st.session_state.pipeline is None: |
|
with st.spinner("กำลังเริ่มต้นระบบ..."): |
|
st.session_state.pipeline = initialize_pipeline() |
|
|
|
|
|
query = st.text_input( |
|
"โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:", |
|
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?" |
|
) |
|
|
|
|
|
if st.button("ค้นหา", type="primary"): |
|
if not query: |
|
st.warning("กรุณาระบุคำถาม") |
|
return |
|
|
|
if st.session_state.pipeline is None: |
|
st.error("ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง") |
|
return |
|
|
|
try: |
|
with st.spinner("กำลังค้นหาคำตอบ..."): |
|
|
|
result = st.session_state.pipeline.process_query(query) |
|
|
|
|
|
st.markdown("### 📝 คำตอบ") |
|
st.write(result["answer"]) |
|
|
|
|
|
with st.expander("แสดงข้อมูลอ้างอิง"): |
|
for i, doc in enumerate(result["documents"], 1): |
|
st.markdown(f"**เอกสารที่ {i}:**") |
|
st.text(doc.content) |
|
st.markdown("---") |
|
|
|
|
|
with st.expander("รายละเอียดการวิเคราะห์คำถาม"): |
|
st.json(result["query_info"]) |
|
|
|
except Exception as e: |
|
st.error(f"เกิดข้อผิดพลาด: {str(e)}") |
|
|
|
|
|
with st.sidebar: |
|
st.markdown("### 📚 เกี่ยวกับระบบ") |
|
st.markdown(""" |
|
ระบบนี้ใช้เทคโนโลยี RAG (Retrieval-Augmented Generation) |
|
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา |
|
|
|
สามารถสอบถามข้อมูลเกี่ยวกับ: |
|
- กำหนดการต่างๆ |
|
- วันสำคัญ |
|
- การลงทะเบียน |
|
- การสอบ |
|
- วันหยุด |
|
""") |
|
|
|
if __name__ == "__main__": |
|
main() |