File size: 4,208 Bytes
69bf965 40b188b 69bf965 40b188b 69bf965 40b188b 69bf965 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import streamlit as st
import json
import os
from calendar_rag import create_default_config, CalendarRAG
# Page config
st.set_page_config(
page_title="Academic Calendar Assistant",
page_icon="📅",
layout="wide"
)
# Initialize session state
if 'pipeline' not in st.session_state:
st.session_state.pipeline = None
def initialize_pipeline():
"""Initialize RAG pipeline with configurations"""
try:
# Get OpenAI API key from environment variable or Streamlit secrets
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
# Create config and initialize pipeline
config = create_default_config(openai_api_key)
config.localization.enable_thai_normalization = True
pipeline = CalendarRAG(config)
# Load calendar data
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():
# Header
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา")
st.markdown("---")
# Initialize pipeline if needed
if st.session_state.pipeline is None:
with st.spinner("กำลังเริ่มต้นระบบ..."):
st.session_state.pipeline = initialize_pipeline()
# Main query interface
query = st.text_input(
"โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:",
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
)
# Add query button
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("กำลังค้นหาคำตอบ..."):
# Process query
result = st.session_state.pipeline.process_query(query)
# Display answer
st.markdown("### 📝 คำตอบ")
st.write(result["answer"])
# Display relevant documents in expander
with st.expander("แสดงข้อมูลอ้างอิง"):
for i, doc in enumerate(result["documents"], 1):
st.markdown(f"**เอกสารที่ {i}:**")
st.text(doc.content)
st.markdown("---")
# Display query understanding
with st.expander("รายละเอียดการวิเคราะห์คำถาม"):
st.json(result["query_info"])
except Exception as e:
st.error(f"เกิดข้อผิดพลาด: {str(e)}")
# Add sidebar with information
with st.sidebar:
st.markdown("### 📚 เกี่ยวกับระบบ")
st.markdown("""
ระบบนี้ใช้เทคโนโลยี RAG (Retrieval-Augmented Generation)
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
สามารถสอบถามข้อมูลเกี่ยวกับ:
- กำหนดการต่างๆ
- วันสำคัญ
- การลงทะเบียน
- การสอบ
- วันหยุด
""")
if __name__ == "__main__":
main() |