Spaces:
Paused

File size: 1,654 Bytes
06bce9a
 
 
 
22bc5cb
 
06bce9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eff1a7a
06bce9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)