JirasakJo's picture
Update app.py
c2e62bc verified
raw
history blame
5.73 kB
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()