Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,34 @@ import streamlit as st
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
from calendar_rag import (
|
| 7 |
create_default_config,
|
| 8 |
-
AcademicCalendarRAG
|
|
|
|
| 9 |
)
|
| 10 |
-
|
| 11 |
def load_custom_css():
|
| 12 |
st.markdown("""
|
| 13 |
<style>
|
|
|
|
| 14 |
body {
|
| 15 |
font-family: "Arial", sans-serif !important;
|
| 16 |
color: #000000 !important;
|
| 17 |
background-color: white !important;
|
| 18 |
line-height: 1.7 !important;
|
| 19 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
h1 {
|
| 21 |
color: #000000;
|
| 22 |
font-size: 2.8rem !important;
|
|
@@ -26,6 +39,15 @@ def load_custom_css():
|
|
| 26 |
padding: 1rem 0;
|
| 27 |
border-bottom: 3px solid #1E3A8A;
|
| 28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
.chat-message {
|
| 30 |
padding: 1.5rem;
|
| 31 |
border-radius: 10px;
|
|
@@ -33,18 +55,41 @@ def load_custom_css():
|
|
| 33 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 34 |
font-size: 1.1rem !important;
|
| 35 |
line-height: 1.6 !important;
|
|
|
|
| 36 |
color: #000000 !important;
|
| 37 |
}
|
|
|
|
| 38 |
.user-message {
|
| 39 |
background-color: #F3F4F6 !important;
|
| 40 |
}
|
|
|
|
| 41 |
.assistant-message {
|
| 42 |
background-color: #EFF6FF !important;
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
</style>
|
| 45 |
""", unsafe_allow_html=True)
|
| 46 |
|
| 47 |
def initialize_pipeline():
|
|
|
|
| 48 |
try:
|
| 49 |
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
|
| 50 |
config = create_default_config(openai_api_key)
|
|
@@ -52,17 +97,19 @@ def initialize_pipeline():
|
|
| 52 |
config.retriever.top_k = 5
|
| 53 |
config.model.temperature = 0.3
|
| 54 |
pipeline = AcademicCalendarRAG(config)
|
| 55 |
-
|
| 56 |
with open("calendar.json", "r", encoding="utf-8") as f:
|
| 57 |
calendar_data = json.load(f)
|
| 58 |
pipeline.load_data(calendar_data)
|
| 59 |
-
|
| 60 |
return pipeline
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
st.error(f"Error initializing pipeline: {str(e)}")
|
| 63 |
return None
|
| 64 |
|
| 65 |
def load_qa_history():
|
|
|
|
| 66 |
try:
|
| 67 |
history_file = Path("qa_history.json")
|
| 68 |
if history_file.exists():
|
|
@@ -74,37 +121,91 @@ def load_qa_history():
|
|
| 74 |
return []
|
| 75 |
|
| 76 |
def save_qa_history(history_entry):
|
|
|
|
| 77 |
try:
|
| 78 |
history_file = Path("qa_history.json")
|
|
|
|
|
|
|
| 79 |
if history_file.exists():
|
| 80 |
with open(history_file, "r", encoding="utf-8") as f:
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
else:
|
| 83 |
history_data = []
|
|
|
|
|
|
|
| 84 |
history_data.append(history_entry)
|
| 85 |
-
|
|
|
|
| 86 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
| 87 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
-
st.error(f"Error saving QA history: {str(e)}")
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def add_to_qa_history(query: str, answer: str):
|
|
|
|
| 92 |
history_entry = {
|
| 93 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
| 94 |
"query": query,
|
| 95 |
"answer": answer
|
| 96 |
}
|
|
|
|
| 97 |
save_qa_history(history_entry)
|
| 98 |
return history_entry
|
| 99 |
-
|
| 100 |
def add_to_history(role: str, message: str):
|
|
|
|
| 101 |
st.session_state.chat_history.append((role, message))
|
|
|
|
|
|
|
| 102 |
if role == "assistant" and len(st.session_state.chat_history) >= 2:
|
|
|
|
| 103 |
user_query = st.session_state.chat_history[-2][1]
|
| 104 |
add_to_qa_history(user_query, message)
|
| 105 |
-
|
| 106 |
def display_chat_history():
|
| 107 |
-
|
|
|
|
| 108 |
if role == "user":
|
| 109 |
st.markdown(f"""
|
| 110 |
<div class="chat-message user-message">
|
|
@@ -120,44 +221,51 @@ def display_chat_history():
|
|
| 120 |
</div>
|
| 121 |
""", unsafe_allow_html=True)
|
| 122 |
|
| 123 |
-
def reset_query_input():
|
| 124 |
-
st.session_state.query_input = ""
|
| 125 |
-
|
| 126 |
def main():
|
|
|
|
| 127 |
st.set_page_config(
|
| 128 |
page_title="Academic Calendar Assistant",
|
| 129 |
page_icon="📅",
|
| 130 |
-
layout="wide"
|
|
|
|
| 131 |
)
|
| 132 |
|
|
|
|
| 133 |
load_custom_css()
|
| 134 |
|
|
|
|
| 135 |
if 'pipeline' not in st.session_state:
|
| 136 |
st.session_state.pipeline = None
|
| 137 |
-
|
| 138 |
if 'chat_history' not in st.session_state:
|
| 139 |
st.session_state.chat_history = []
|
| 140 |
|
|
|
|
| 141 |
if 'qa_history_loaded' not in st.session_state:
|
| 142 |
st.session_state.qa_history_loaded = True
|
| 143 |
load_qa_history()
|
| 144 |
|
|
|
|
| 145 |
st.markdown("""
|
| 146 |
<div style="text-align: center; padding: 2rem 0;">
|
| 147 |
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
|
|
|
| 148 |
</div>
|
| 149 |
""", unsafe_allow_html=True)
|
| 150 |
|
|
|
|
| 151 |
if st.session_state.pipeline is None:
|
| 152 |
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
| 153 |
st.session_state.pipeline = initialize_pipeline()
|
| 154 |
|
| 155 |
chat_col, info_col = st.columns([7, 3])
|
| 156 |
-
|
| 157 |
with chat_col:
|
| 158 |
with st.container():
|
|
|
|
| 159 |
display_chat_history()
|
| 160 |
-
|
|
|
|
| 161 |
st.markdown("""
|
| 162 |
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
| 163 |
<span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
|
|
@@ -165,51 +273,110 @@ def main():
|
|
| 165 |
</span>
|
| 166 |
</label>
|
| 167 |
""", unsafe_allow_html=True)
|
| 168 |
-
|
| 169 |
-
if "query_input" not in st.session_state:
|
| 170 |
-
st.session_state.query_input = ""
|
| 171 |
-
|
| 172 |
query = st.text_input(
|
| 173 |
"",
|
| 174 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
| 175 |
-
key="query_input"
|
| 176 |
-
on_change=reset_query_input
|
| 177 |
)
|
| 178 |
|
|
|
|
| 179 |
col1, col2, col3 = st.columns([1, 1, 4])
|
| 180 |
-
|
| 181 |
with col1:
|
| 182 |
send_query = st.button(
|
| 183 |
"📤 ส่งคำถาม",
|
| 184 |
type="primary",
|
| 185 |
-
use_container_width=True
|
|
|
|
| 186 |
)
|
| 187 |
-
|
| 188 |
with col2:
|
| 189 |
clear_history = st.button(
|
| 190 |
"🗑️ ล้างประวัติ",
|
| 191 |
type="secondary",
|
| 192 |
-
use_container_width=True
|
|
|
|
| 193 |
)
|
| 194 |
-
|
| 195 |
if send_query and query:
|
| 196 |
if st.session_state.pipeline is None:
|
| 197 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
elif send_query and not query:
|
| 208 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|
| 215 |
main()
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
+
import subprocess
|
| 6 |
+
from huggingface_hub import HfApi
|
| 7 |
from pathlib import Path
|
| 8 |
from calendar_rag import (
|
| 9 |
create_default_config,
|
| 10 |
+
AcademicCalendarRAG,
|
| 11 |
+
PipelineConfig
|
| 12 |
)
|
| 13 |
+
# Custom CSS for enhanced styling
|
| 14 |
def load_custom_css():
|
| 15 |
st.markdown("""
|
| 16 |
<style>
|
| 17 |
+
/* General body styling */
|
| 18 |
body {
|
| 19 |
font-family: "Arial", sans-serif !important;
|
| 20 |
color: #000000 !important;
|
| 21 |
background-color: white !important;
|
| 22 |
line-height: 1.7 !important;
|
| 23 |
}
|
| 24 |
+
|
| 25 |
+
/* Main container styling */
|
| 26 |
+
.main {
|
| 27 |
+
padding: 2rem;
|
| 28 |
+
color: #000000;
|
| 29 |
+
background-color: white;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
/* Headers styling */
|
| 33 |
h1 {
|
| 34 |
color: #000000;
|
| 35 |
font-size: 2.8rem !important;
|
|
|
|
| 39 |
padding: 1rem 0;
|
| 40 |
border-bottom: 3px solid #1E3A8A;
|
| 41 |
}
|
| 42 |
+
|
| 43 |
+
h3, h4 {
|
| 44 |
+
color: #000000;
|
| 45 |
+
font-weight: 600 !important;
|
| 46 |
+
font-size: 1.6rem !important;
|
| 47 |
+
margin-top: 1.5rem !important;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/* Chat message styling */
|
| 51 |
.chat-message {
|
| 52 |
padding: 1.5rem;
|
| 53 |
border-radius: 10px;
|
|
|
|
| 55 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 56 |
font-size: 1.1rem !important;
|
| 57 |
line-height: 1.6 !important;
|
| 58 |
+
font-family: "Arial", sans-serif !important;
|
| 59 |
color: #000000 !important;
|
| 60 |
}
|
| 61 |
+
|
| 62 |
.user-message {
|
| 63 |
background-color: #F3F4F6 !important;
|
| 64 |
}
|
| 65 |
+
|
| 66 |
.assistant-message {
|
| 67 |
background-color: #EFF6FF !important;
|
| 68 |
}
|
| 69 |
+
|
| 70 |
+
/* Status indicators */
|
| 71 |
+
.status-indicator {
|
| 72 |
+
padding: 0.5rem 1rem;
|
| 73 |
+
border-radius: 6px;
|
| 74 |
+
font-weight: 500;
|
| 75 |
+
font-size: 1.2rem;
|
| 76 |
+
color: #000000;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.status-online {
|
| 80 |
+
background-color: #DEF7EC;
|
| 81 |
+
color: #03543F;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.status-offline {
|
| 85 |
+
background-color: #FDE8E8;
|
| 86 |
+
color: rgb(255, 255, 255);
|
| 87 |
+
}
|
| 88 |
</style>
|
| 89 |
""", unsafe_allow_html=True)
|
| 90 |
|
| 91 |
def initialize_pipeline():
|
| 92 |
+
"""Initialize RAG pipeline with configurations"""
|
| 93 |
try:
|
| 94 |
openai_api_key = os.getenv('OPENAI_API_KEY') or st.secrets['OPENAI_API_KEY']
|
| 95 |
config = create_default_config(openai_api_key)
|
|
|
|
| 97 |
config.retriever.top_k = 5
|
| 98 |
config.model.temperature = 0.3
|
| 99 |
pipeline = AcademicCalendarRAG(config)
|
| 100 |
+
|
| 101 |
with open("calendar.json", "r", encoding="utf-8") as f:
|
| 102 |
calendar_data = json.load(f)
|
| 103 |
pipeline.load_data(calendar_data)
|
| 104 |
+
|
| 105 |
return pipeline
|
| 106 |
+
|
| 107 |
except Exception as e:
|
| 108 |
st.error(f"Error initializing pipeline: {str(e)}")
|
| 109 |
return None
|
| 110 |
|
| 111 |
def load_qa_history():
|
| 112 |
+
"""Load QA history from local JSON file"""
|
| 113 |
try:
|
| 114 |
history_file = Path("qa_history.json")
|
| 115 |
if history_file.exists():
|
|
|
|
| 121 |
return []
|
| 122 |
|
| 123 |
def save_qa_history(history_entry):
|
| 124 |
+
"""Save QA history entry to local JSON file and push to GitHub"""
|
| 125 |
try:
|
| 126 |
history_file = Path("qa_history.json")
|
| 127 |
+
|
| 128 |
+
# Initialize or load existing history
|
| 129 |
if history_file.exists():
|
| 130 |
with open(history_file, "r", encoding="utf-8") as f:
|
| 131 |
+
try:
|
| 132 |
+
history_data = json.load(f)
|
| 133 |
+
except json.JSONDecodeError:
|
| 134 |
+
st.error("Error parsing existing JSON file")
|
| 135 |
+
history_data = []
|
| 136 |
else:
|
| 137 |
history_data = []
|
| 138 |
+
|
| 139 |
+
# Append new entry
|
| 140 |
history_data.append(history_entry)
|
| 141 |
+
|
| 142 |
+
# Save updated history locally
|
| 143 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
| 144 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
| 145 |
+
|
| 146 |
+
# st.info("Saved locally, attempting GitHub upload...")
|
| 147 |
+
|
| 148 |
+
# Push to GitHub
|
| 149 |
+
github_token = os.getenv('GITHUB_TOKEN') or st.secrets['GITHUB_TOKEN']
|
| 150 |
+
if not github_token:
|
| 151 |
+
st.error("GitHub token not found!")
|
| 152 |
+
return
|
| 153 |
+
|
| 154 |
+
from github import Github
|
| 155 |
+
g = Github(github_token)
|
| 156 |
+
|
| 157 |
+
# Replace with your repository name
|
| 158 |
+
repo = g.get_repo("jirasaksaimekJijo/swu-chat-bot-project")
|
| 159 |
+
|
| 160 |
+
try:
|
| 161 |
+
# Try to get the file first
|
| 162 |
+
contents = repo.get_contents("qa_history.json")
|
| 163 |
+
response = repo.update_file(
|
| 164 |
+
path="qa_history.json",
|
| 165 |
+
message="Update QA history",
|
| 166 |
+
content=json.dumps(history_data, ensure_ascii=False, indent=2),
|
| 167 |
+
sha=contents.sha
|
| 168 |
+
)
|
| 169 |
+
# st.success(f"Successfully updated file on GitHub: {response.commit.html_url}")
|
| 170 |
+
except Exception as e:
|
| 171 |
+
# st.info(f"File doesn't exist yet, creating new one... Error was: {str(e)}")
|
| 172 |
+
# File doesn't exist, create it
|
| 173 |
+
response = repo.create_file(
|
| 174 |
+
path="qa_history.json",
|
| 175 |
+
message="Create QA history",
|
| 176 |
+
content=json.dumps(history_data, ensure_ascii=False, indent=2)
|
| 177 |
+
)
|
| 178 |
+
# st.success(f"Successfully created file on GitHub: {response['commit'].html_url}")
|
| 179 |
+
|
| 180 |
except Exception as e:
|
| 181 |
+
# st.error(f"Error saving QA history: {str(e)}")
|
| 182 |
+
import traceback
|
| 183 |
+
# st.error(f"Full error: {traceback.format_exc()}")
|
| 184 |
|
| 185 |
def add_to_qa_history(query: str, answer: str):
|
| 186 |
+
"""Add new QA pair to history"""
|
| 187 |
history_entry = {
|
| 188 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
| 189 |
"query": query,
|
| 190 |
"answer": answer
|
| 191 |
}
|
| 192 |
+
|
| 193 |
save_qa_history(history_entry)
|
| 194 |
return history_entry
|
| 195 |
+
|
| 196 |
def add_to_history(role: str, message: str):
|
| 197 |
+
"""Add message to chat history and save if it's a complete QA pair"""
|
| 198 |
st.session_state.chat_history.append((role, message))
|
| 199 |
+
|
| 200 |
+
# If this is an assistant response, save the QA pair
|
| 201 |
if role == "assistant" and len(st.session_state.chat_history) >= 2:
|
| 202 |
+
# Get the corresponding user query (previous message)
|
| 203 |
user_query = st.session_state.chat_history[-2][1]
|
| 204 |
add_to_qa_history(user_query, message)
|
| 205 |
+
|
| 206 |
def display_chat_history():
|
| 207 |
+
"""Display chat history with enhanced styling"""
|
| 208 |
+
for i, (role, message) in enumerate(st.session_state.chat_history):
|
| 209 |
if role == "user":
|
| 210 |
st.markdown(f"""
|
| 211 |
<div class="chat-message user-message">
|
|
|
|
| 221 |
</div>
|
| 222 |
""", unsafe_allow_html=True)
|
| 223 |
|
|
|
|
|
|
|
|
|
|
| 224 |
def main():
|
| 225 |
+
# Page config
|
| 226 |
st.set_page_config(
|
| 227 |
page_title="Academic Calendar Assistant",
|
| 228 |
page_icon="📅",
|
| 229 |
+
layout="wide",
|
| 230 |
+
initial_sidebar_state="collapsed"
|
| 231 |
)
|
| 232 |
|
| 233 |
+
# Load custom CSS
|
| 234 |
load_custom_css()
|
| 235 |
|
| 236 |
+
# Initialize session state
|
| 237 |
if 'pipeline' not in st.session_state:
|
| 238 |
st.session_state.pipeline = None
|
| 239 |
+
|
| 240 |
if 'chat_history' not in st.session_state:
|
| 241 |
st.session_state.chat_history = []
|
| 242 |
|
| 243 |
+
# Load QA history at startup
|
| 244 |
if 'qa_history_loaded' not in st.session_state:
|
| 245 |
st.session_state.qa_history_loaded = True
|
| 246 |
load_qa_history()
|
| 247 |
|
| 248 |
+
# Header
|
| 249 |
st.markdown("""
|
| 250 |
<div style="text-align: center; padding: 2rem 0;">
|
| 251 |
<h1>🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา</h1>
|
| 252 |
+
<p style="font-size: 1.2rem; color: #666;">บัณฑิตวิทยาลัย มหาวิทยาลัยศรีนครินทรวิโรฒ</p>
|
| 253 |
</div>
|
| 254 |
""", unsafe_allow_html=True)
|
| 255 |
|
| 256 |
+
# Initialize pipeline
|
| 257 |
if st.session_state.pipeline is None:
|
| 258 |
with st.spinner("กำลังเริ่มต้นระบบ..."):
|
| 259 |
st.session_state.pipeline = initialize_pipeline()
|
| 260 |
|
| 261 |
chat_col, info_col = st.columns([7, 3])
|
| 262 |
+
|
| 263 |
with chat_col:
|
| 264 |
with st.container():
|
| 265 |
+
# Display chat history
|
| 266 |
display_chat_history()
|
| 267 |
+
|
| 268 |
+
# Query input section
|
| 269 |
st.markdown("""
|
| 270 |
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
| 271 |
<span style="color: #ffffff; border-left: 4px solid #ffffff; padding-left: 0.8rem;">
|
|
|
|
| 273 |
</span>
|
| 274 |
</label>
|
| 275 |
""", unsafe_allow_html=True)
|
| 276 |
+
|
|
|
|
|
|
|
|
|
|
| 277 |
query = st.text_input(
|
| 278 |
"",
|
| 279 |
placeholder="เช่น: วันสุดท้ายของการสอบปากเปล่าในภาคเรียนที่ 1/2567 คือวันที่เท่าไร?",
|
| 280 |
+
key="query_input"
|
|
|
|
| 281 |
)
|
| 282 |
|
| 283 |
+
# Button layout inside main() function
|
| 284 |
col1, col2, col3 = st.columns([1, 1, 4])
|
| 285 |
+
|
| 286 |
with col1:
|
| 287 |
send_query = st.button(
|
| 288 |
"📤 ส่งคำถาม",
|
| 289 |
type="primary",
|
| 290 |
+
use_container_width=True,
|
| 291 |
+
key="send_query_button"
|
| 292 |
)
|
| 293 |
+
|
| 294 |
with col2:
|
| 295 |
clear_history = st.button(
|
| 296 |
"🗑️ ล้างประวัติ",
|
| 297 |
type="secondary",
|
| 298 |
+
use_container_width=True,
|
| 299 |
+
key="clear_history_button"
|
| 300 |
)
|
| 301 |
+
# Process query
|
| 302 |
if send_query and query:
|
| 303 |
if st.session_state.pipeline is None:
|
| 304 |
st.error("❌ ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
| 305 |
+
return
|
| 306 |
+
|
| 307 |
+
add_to_history("user", query)
|
| 308 |
+
|
| 309 |
+
try:
|
| 310 |
+
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
| 311 |
+
result = st.session_state.pipeline.process_query(query)
|
| 312 |
+
add_to_history("assistant", result["answer"])
|
| 313 |
+
|
| 314 |
+
with st.expander("📚 แสดงข้อมูลอ้างอิง", expanded=False):
|
| 315 |
+
for i, doc in enumerate(result["documents"], 1):
|
| 316 |
+
st.markdown(f"""
|
| 317 |
+
<div style="padding: 1rem; background-color: #F9FAFB; border-radius: 8px; margin: 0.5rem 0;">
|
| 318 |
+
<strong>เอกสารที่ {i}:</strong><br>
|
| 319 |
+
{doc.content}
|
| 320 |
+
</div>
|
| 321 |
+
""", unsafe_allow_html=True)
|
| 322 |
+
|
| 323 |
+
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม", expanded=False):
|
| 324 |
+
st.json(result["query_info"])
|
| 325 |
+
|
| 326 |
+
# Clear input box and rerun app
|
| 327 |
+
st.session_state.query_input = ""
|
| 328 |
+
st.rerun()
|
| 329 |
+
|
| 330 |
+
except Exception as e:
|
| 331 |
+
st.error(f"❌ เกิดข้อผิดพลาด: {str(e)}")
|
| 332 |
+
|
| 333 |
elif send_query and not query:
|
| 334 |
st.warning("⚠️ กรุณาระบุคำถาม")
|
| 335 |
|
| 336 |
+
# Handle clear history action
|
| 337 |
+
if clear_history:
|
| 338 |
+
st.session_state.chat_history = []
|
| 339 |
+
st.rerun()
|
| 340 |
+
|
| 341 |
+
with info_col:
|
| 342 |
+
# System information
|
| 343 |
+
st.markdown("""
|
| 344 |
+
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|
| 345 |
+
<h3 style="color: #1E3A8A;">ℹ️ เกี่ยวกับระบบ</h3>
|
| 346 |
+
<p style="color: #000000;">
|
| 347 |
+
ระบบนี้ใช้เทคโนโลยี <strong>RAG (Retrieval-Augmented Generation)</strong>
|
| 348 |
+
ในการค้นหาและตอบคำถามเกี่ยวกับปฏิทินการศึกษา
|
| 349 |
+
</p>
|
| 350 |
+
<h4 style="color: #1E3A8A; margin-top: 1rem;">สามารถสอบถามข้อมูลเกี่ยวกับ:</h4>
|
| 351 |
+
<ul style="list-style-type: none; padding-left: 0;">
|
| 352 |
+
<li style="color: #000000; margin-bottom: 0.5rem;">📅 กำหนดการต่างๆ ในปฏิทินการศึกษา</li>
|
| 353 |
+
<li style="color: #000000; margin-bottom: 0.5rem;">🎯 วันสำคัญและกิจกรรม</li>
|
| 354 |
+
<li style="color: #000000; margin-bottom: 0.5rem;">📝 การลงทะเบียนเรียน</li>
|
| 355 |
+
<li style="color: #000000; margin-bottom: 0.5rem;">📚 กำหนดการสอบ</li>
|
| 356 |
+
<li style="color: #000000; margin-bottom: 0.5rem;">🏖️ วันหยุดการศึกษา</li>
|
| 357 |
+
</ul>
|
| 358 |
+
</div>
|
| 359 |
+
""", unsafe_allow_html=True)
|
| 360 |
+
|
| 361 |
+
# System status
|
| 362 |
+
st.markdown("""
|
| 363 |
+
<div style="background-color: #f9fafb; padding: 1.5rem; border-radius: 12px;">
|
| 364 |
+
<h3 style="color: #1E3A8A;">🔄 สถานะระบบ</h3>
|
| 365 |
+
<div style="margin-top: 1rem;">
|
| 366 |
+
<p><strong style="color: #000000;">⏰ เวลาปัจจุบัน:</strong><br>
|
| 367 |
+
<span style="color: #000000;">{}</span></p>
|
| 368 |
+
<p><strong style="color: #000000;">📡 สถานะระบบ:</strong><br>
|
| 369 |
+
<span class="status-indicator {}">
|
| 370 |
+
{} {}
|
| 371 |
+
</span></p>
|
| 372 |
+
</div>
|
| 373 |
+
</div>
|
| 374 |
+
""".format(
|
| 375 |
+
(datetime.now() + timedelta(hours=6)).strftime('%Y-%m-%d %H:%M:%S'),
|
| 376 |
+
"status-online" if st.session_state.pipeline else "status-offline",
|
| 377 |
+
"🟢" if st.session_state.pipeline else "🔴",
|
| 378 |
+
"พร้อมใช้งาน" if st.session_state.pipeline else "ไม่พร้อมใช้งาน"
|
| 379 |
+
), unsafe_allow_html=True)
|
| 380 |
|
| 381 |
if __name__ == "__main__":
|
| 382 |
main()
|