Spaces:
Paused

CBIT / app.py
VAH6002's picture
Update app.py
eff1a7a verified
Raw History Blame Contribute Delete
1.65 kB
import streamlit as st
from backend import generate_response
# Streamlit Page Configuration
st.set_page_config(page_title="CBIT College Chatbot", layout="centered")
st.title("🎓 ChatCBIT-AI")
# Sidebar for User Type Selection
user_type = st.sidebar.selectbox(
"Select User Type", ["Student", "Parent", "General Public"]
)
# Initialize Chat History and session state variables
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "user_input" not in st.session_state:
st.session_state.user_input = ""
if "should_clear_input" not in st.session_state:
st.session_state.should_clear_input = False
# Clear input if flag is set
if st.session_state.should_clear_input:
st.session_state.user_input = ""
st.session_state.should_clear_input = False
# Form for handling input submission
with st.form(key="chat_form"):
user_query = st.text_input(
"Ask me anything about the CBIT college...",
value=st.session_state.user_input,
key="user_input"
)
submit_button = st.form_submit_button("Send")
if submit_button and user_query:
# Generate Response
bot_response = generate_response(user_query, user_type.lower())
# Store chat history
st.session_state.chat_history.append(("You", user_query))
st.session_state.chat_history.append(("Chatbot", bot_response))
# Set flag to clear input and rerun
st.session_state.should_clear_input = True
st.rerun()
# Display Chat History in Reverse Order
st.subheader("Chat History")
for role, message in reversed(st.session_state.chat_history):
with st.chat_message(role):
st.markdown(message)