-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat_app.py
126 lines (109 loc) · 4.51 KB
/
chat_app.py
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
import streamlit as st
## Custom imports
from components.response_processor import process_user_input, process_cancellation
from utils.constants import MAX_MESSAGES
from components.state import load_session_state
from components.sidebar import (
setup_sidebar,
)
from components.chat_interface import (
display_chat_history,
clear_chat,
)
from components.chat_input_box import set_chat_box
st.set_page_config(
page_title="TransitGPT", page_icon="🚍", layout="wide", initial_sidebar_state="auto"
)
# Setup Session state variables at the beginning of your Streamlit app
load_session_state()
# Setup sidebar
setup_sidebar()
@st.dialog("User Information")
def show_user_info_popup():
if "user_name" not in st.session_state or "user_email" not in st.session_state:
st.write("Please provide your information for feedback purposes:")
name = st.text_input("Name")
email = st.text_input("Email")
if st.button("Submit"):
if name and email:
st.session_state['user_name'] = name
st.session_state['user_email'] = email
st.success("Thank you for providing your information!")
st.rerun()
else:
st.session_state["user_name"] = None
st.session_state["user_email"] = None
st.rerun()
# st.error("Please fill in both name and email.")
# Call the dialog function when needed
# if "user_name" not in st.session_state or "user_email" not in st.session_state:
# show_user_info_popup()
# Chat interface
st.title("🚌TransitGPT")
# Display chat history
display_chat_history(st.session_state["fb_agent"], st.session_state.uuid)
# Check if the number of messages has reached the limit
if len(st.session_state.chat_history) >= MAX_MESSAGES:
st.session_state["show_limit_popup"] = True
# Display dialog when message limit is reached
if st.session_state["show_limit_popup"]:
st.session_state["is_chat_input_disabled"] = True
clear_chat()
# Display sample questions only if it's the first question and no question has been selected
if not st.session_state.first_question_asked and not st.session_state.selected_question:
st.write("Sample Questions:")
for i, question in enumerate(st.session_state.questions):
if st.button(question, key=f"q_{i}"):
st.session_state.selected_question = question
st.session_state.user_input = (
question # Set user_input when a question is selected
)
st.session_state.first_question_asked = (
True # Mark that a question has been selected
)
st.rerun() # Rerun to remove the sample questions
if st.session_state.user_input:
set_chat_box(st.session_state.user_input, len(st.session_state.chat_history))
elif st.session_state.selected_question:
set_chat_box(st.session_state.selected_question, len(st.session_state.chat_history))
else:
set_chat_box("", len(st.session_state.chat_history))
# Process user input
user_input = st.chat_input(
"Type your query here...",
disabled=st.session_state.is_processing or st.session_state.is_chat_input_disabled,
)
# Process user input or selected question
if user_input:
# Use the edited input from the chat box
st.session_state.user_input = user_input
st.session_state.is_processing = True
st.session_state.selected_question = None # Clear the selected question
if not st.session_state.first_question_asked:
st.session_state.first_question_asked = True
st.rerun()
if st.session_state.is_processing:
user_input = st.session_state["user_input"]
# Check if this input hasn't been added to chat history yet
if not st.session_state.chat_history or (
"summary_response" in st.session_state.chat_history[-1]
):
with st.chat_message("user", avatar="🙋♂️"):
st.write(user_input)
st.session_state.chat_history.append({"role": "user", "content": user_input})
# Add a cancel button
col1, col2 = st.columns([8, 1])
with col2:
cancel_button = st.button("⏹Stop")
with col1:
if cancel_button:
st.session_state.is_processing = False
st.session_state.user_input = None
process_cancellation()
st.rerun()
else:
process_user_input(user_input)
# Clear the input box after processing
st.session_state.user_input = None
st.session_state.is_processing = False
st.rerun()