File size: 5,727 Bytes
69bf965
 
 
b1760d3
 
 
 
 
 
69bf965
278a2e2
b5ab9c7
 
c2e62bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5ab9c7
 
 
69bf965
 
 
b5ab9c7
c2e62bc
69bf965
 
b5ab9c7
 
278a2e2
b5ab9c7
69bf965
 
b1760d3
 
69bf965
 
 
 
 
 
 
b1760d3
 
 
c2e62bc
69bf965
 
 
c2e62bc
69bf965
c2e62bc
69bf965
 
 
 
b1760d3
278a2e2
c2e62bc
 
 
 
 
 
 
 
88c9430
69bf965
c2e62bc
 
b5ab9c7
c2e62bc
 
69bf965
c2e62bc
b5ab9c7
c2e62bc
 
 
b5ab9c7
278a2e2
c2e62bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69bf965
 
c2e62bc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import streamlit as st
import json
import os
from datetime import datetime
from calendar_rag import (
    create_default_config,
    AcademicCalendarRAG,
    PipelineConfig
)

# Custom CSS for enhanced styling
def load_custom_css():
    st.markdown("""
        <style>
        body {
            font-family: "Arial", sans-serif;
            color: #000000;
            background-color: #F7F9FC;
        }
        h1 {
            color: #1E3A8A;
            font-size: 2.8rem;
            text-align: center;
            font-weight: 700;
            margin-bottom: 1rem;
        }
        .chat-container {
            background-color: #FFFFFF;
            border-radius: 8px;
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 1rem;
        }
        .chat-message {
            margin-bottom: 1rem;
            padding: 1rem;
            border-radius: 8px;
            background-color: #F3F4F6;
            font-size: 1.1rem;
        }
        .assistant-message {
            background-color: #EFF6FF;
        }
        .user-message {
            background-color: #E5E7EB;
        }
        .input-card {
            background-color: #FFFFFF;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 2rem;
        }
        .sidebar-info {
            background-color: #F9FAFB;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }
        .status-indicator {
            font-weight: 600;
            font-size: 1rem;
            padding: 0.5rem 1rem;
            border-radius: 5px;
            display: inline-block;
        }
        .status-online {
            background-color: #DEF7EC;
            color: #03543F;
        }
        .status-offline {
            background-color: #FDE8E8;
            color: #9B1C1C;
        }
        </style>
    """, unsafe_allow_html=True)

# Page config
st.set_page_config(
    page_title="Academic Calendar Assistant",
    page_icon="📅",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Load custom CSS
load_custom_css()

# Initialize session state
if 'pipeline' not in st.session_state:
    st.session_state.pipeline = None
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

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
        config.retriever.top_k = 5
        config.model.temperature = 0.3
        pipeline = AcademicCalendarRAG(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 display_chat_history():
    """Display chat history with enhanced styling"""
    for role, message in st.session_state.chat_history:
        chat_style = "assistant-message" if role == "assistant" else "user-message"
        st.markdown(f"""
            <div class="chat-container {chat_style}">
                <strong>{'🤖 คำตอบ:' if role == 'assistant' else '🧑 คำถาม:'}</strong>
                <p>{message}</p>
            </div>
        """, unsafe_allow_html=True)

def main():
    # Header
    st.markdown("<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>", unsafe_allow_html=True)

    # Sidebar with system info
    with st.sidebar:
        st.markdown("""
            <div class="sidebar-info">
                <h3>ℹ️ เกี่ยวกับระบบ</h3>
                <p>ระบบนี้ใช้เทคโนโลยี <strong>RAG (Retrieval-Augmented Generation)</strong> ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา</p>
                <h4>สถานะระบบ:</h4>
                <span class="status-indicator status-online">🟢 พร้อมใช้งาน</span>
            </div>
        """, unsafe_allow_html=True)

    # Main chat area
    chat_col, input_col = st.columns([2, 1])
    with chat_col:
        st.subheader("ประวัติการสนทนา")
        display_chat_history()

    with input_col:
        st.subheader("ส่งคำถามของคุณ")
        st.markdown('<div class="input-card">', unsafe_allow_html=True)
        query = st.text_input(
            "โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:",
            placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?"
        )
        if st.button("📤 ส่งคำถาม"):
            if query:
                st.session_state.chat_history.append(("user", query))
                answer = "นี่คือคำตอบตัวอย่าง: วันสุดท้ายคือ 25 กันยายน 2567"  # Example
                st.session_state.chat_history.append(("assistant", answer))
            else:
                st.warning("⚠️ กรุณาระบุคำถาม")
        st.markdown('</div>', unsafe_allow_html=True)

if __name__ == "__main__":
    main()