Spaces:
Running
Running
File size: 5,692 Bytes
ec3584e |
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
import os
import time
import streamlit as st
from qa_loader import load_qa_and_create_vectorstore
from rag_chain import generate_response
from dotenv import load_dotenv
import asyncio
try:
asyncio.get_running_loop()
except RuntimeError:
asyncio.set_event_loop(asyncio.new_event_loop())
# 🔹 Load environment variables
load_dotenv()
# 🔹 Minimal CSS - sadece mesaj balonları için gerekli olan
def load_css():
st.markdown("""
<style>
/* WhatsApp benzeri mesaj balonları */
.message {
display: flex;
margin-bottom: 10px;
}
.message-user {
justify-content: flex-end;
}
.message-assistant {
justify-content: flex-start;
}
.message-content {
padding: 10px 15px;
border-radius: 18px;
max-width: 70%;
word-wrap: break-word;
}
.user-content {
border: 1px solid #E5E7EB;
border-bottom-right-radius: 5px;
}
.assistant-content {
border: 1px solid #E5E7EB;
border-bottom-left-radius: 5px;
}
.timestamp {
font-size: 0.7rem;
color: #64748B;
margin-top: 2px;
text-align: right;
}
.empty-chat {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 50vh;
color: #64748B;
}
.empty-chat img {
width: 100px;
margin-bottom: 20px;
opacity: 0.7;
}
</style>
""", unsafe_allow_html=True)
# 🔹 Streamlit Page Configuration
st.set_page_config(
page_title="University AI Assistant",
page_icon="🎓",
layout="wide",
initial_sidebar_state="expanded"
)
# Load minimal CSS
load_css()
# 🔹 Sidebar with information
with st.sidebar:
st.image("logo.png", width=200)
st.markdown("### ℹ️ About")
st.markdown("""
This AI assistant helps answer questions about Vistula University.
Ask anything about admissions, courses, campus life, and more!
""")
st.markdown("### 🔗 Quick Links")
st.markdown("[University Website](https://www.vistula.edu.pl/en)")
st.markdown("[Student Portal](https://www.vistula.edu.pl/en/students)")
st.markdown("[Contact Us](https://www.vistula.edu.pl/en/contact)")
# 🔹 Main content area
st.title("🎓 University AI Assistant")
st.subheader("Your personal guide to university information. Ask me anything!")
# 🔹 Retrieve Data (Cached for Performance)
@st.cache_resource
def get_retriever():
return load_qa_and_create_vectorstore()
retriever = get_retriever()
if isinstance(retriever, tuple):
retriever = retriever[0]
# 🔹 Start or Load Chat History
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "query" not in st.session_state:
st.session_state.query = ""
if "processing_done" not in st.session_state:
st.session_state.processing_done = False
# 🔹 Display Chat History in WhatsApp style
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
if not st.session_state.chat_history:
st.markdown("""
<div class="empty-chat">
<img src="https://cdn-icons-png.flaticon.com/512/1041/1041916.png">
<p>Start a conversation by asking a question below!</p>
</div>
""", unsafe_allow_html=True)
else:
for entry in st.session_state.chat_history:
# User message
st.markdown(f"""
<div class="message message-user">
<div class="message-content user-content">
{entry['question']}
<div class="timestamp">You</div>
</div>
</div>
""", unsafe_allow_html=True)
# Assistant message
st.markdown(f"""
<div class="message message-assistant">
<div class="message-content assistant-content">
{entry['answer']}
<div class="timestamp">Assistant</div>
</div>
</div>
""", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
# 🔹 Form kullanarak giriş alanını ve gönderme işlemini yönet
with st.form(key="message_form", clear_on_submit=True):
user_input = st.text_input("Type your message...", key="user_input")
submit_button = st.form_submit_button("Send")
# Handle category selection from sidebar
if st.session_state.query:
user_input = st.session_state.query
st.session_state.query = "" # Clear after using
submit_button = True
else:
submit_button = submit_button
# 🔹 Process When User Submits a Question
if submit_button and user_input and not st.session_state.processing_done:
with st.spinner("🤖"):
response = generate_response(retriever, user_input)
# Add to chat history
st.session_state.chat_history.append({
"question": user_input,
"answer": response
})
# Mark processing as done to prevent reprocessing
st.session_state.processing_done = True
# Force a rerun to update the UI
st.rerun()
# Reset processing flag when input changes
if 'previous_input' not in st.session_state:
st.session_state.previous_input = ""
if st.session_state.previous_input != user_input:
st.session_state.processing_done = False
st.session_state.previous_input = user_input
# 🔹 Footer
st.markdown("© 2025 University AI Assistant | Powered by AI") |