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(""" """, 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"""
{'ðŸĪ– āļ„āļģāļ•āļ­āļš:' if role == 'assistant' else '🧑 āļ„āļģāļ–āļēāļĄ:'}

{message}

""", unsafe_allow_html=True) def main(): # Header st.markdown("

🎓 āļĢāļ°āļšāļšāļ„āđ‰āļ™āļŦāļēāļ‚āđ‰āļ­āļĄāļđāļĨāļ›āļāļīāļ—āļīāļ™āļāļēāļĢāļĻāļķāļāļĐāļē

", unsafe_allow_html=True) # Sidebar with system info with st.sidebar: st.markdown(""" """, 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('
', 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('
', unsafe_allow_html=True) if __name__ == "__main__": main()