-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
230 lines (198 loc) Β· 8.25 KB
/
app.py
File metadata and controls
230 lines (198 loc) Β· 8.25 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import streamlit as st
from langgraph.graph import END
from health_bot import create_health_bot_graph
from health_bot_session import HealthBotSession, BotResponse
from ui.ui_sidebar import render_sidebar
from ui.ui_styles import (
configure_page, apply_custom_styles, render_title, render_footer
)
# Configure page and apply styling
configure_page()
apply_custom_styles()
render_title()
# Render sidebar
render_sidebar()
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "conversation_active" not in st.session_state:
st.session_state.conversation_active = False
if "conversation_generator" not in st.session_state:
st.session_state.conversation_generator = None
if "awaiting_input" not in st.session_state:
st.session_state.awaiting_input = None
def start_new_conversation(question: str):
"""Start a new conversation with the given question"""
graph = create_health_bot_graph()
bot_session = HealthBotSession(question, graph, END)
st.session_state.conversation_generator = bot_session.run_conversation()
st.session_state.conversation_active = True
st.session_state.awaiting_input = None
def process_bot_response(response: BotResponse):
"""Process a BotResponse and update the UI state accordingly"""
if response.user_input_request:
# Bot is requesting user input - add question to chat history
st.session_state.messages.append({
"role": "assistant",
"content": response.user_input_request.prompt
})
st.session_state.awaiting_input = response.user_input_request
elif response.message:
# Bot sent a message - add it to messages with source info
message_content = response.message
# Add source information if available
if response.information_source:
source_icons = {
"rag": "π",
"knowledge": "π§ ",
"web": "π",
"documents": "π"
}
icon = source_icons.get(response.information_source, "βΉοΈ")
source_text = response.information_source.title()
message_content = (f"{message_content}\n\n*{icon} Source: "
f"{source_text}*")
st.session_state.messages.append({
"role": "assistant",
"content": message_content
})
# Clear awaiting input when we get a regular message
st.session_state.awaiting_input = None
def get_next_bot_response(user_input=None):
"""Get the next response from the bot generator"""
try:
if user_input is not None:
return st.session_state.conversation_generator.send(user_input)
else:
return next(st.session_state.conversation_generator)
except StopIteration:
# Conversation ended
st.session_state.conversation_active = False
st.session_state.conversation_generator = None
st.session_state.awaiting_input = None
return None
# Main chat interface
chat_container = st.container()
with chat_container:
# Display chat history if there's any, otherwise a welcome message
if st.session_state.messages:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
else:
# Welcome message for new users
with st.chat_message("assistant"):
st.markdown("""
π **Welcome to HealthBot!**
I'm here to help you research health topics using the latest
information from the web.
I can:
- π Search for current health information
- π Provide detailed summaries with citations
- π§ Create quizzes to test your understanding
- π¬ Answer follow-up questions
**What health topic would you like to explore today?**
""")
# Handle new user input
if prompt := st.chat_input(
"Ask a health question...",
disabled=st.session_state.awaiting_input is not None
):
# Add user message to chat
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message immediately
with st.chat_message("user"):
st.markdown(prompt)
# Start new conversation
if not st.session_state.conversation_active:
start_new_conversation(prompt)
# Show loading spinner and process bot responses
with st.chat_message("assistant"):
with st.spinner("π Researching your question..."):
while True:
response = get_next_bot_response()
if response is None:
break
process_bot_response(response)
if response.user_input_request:
break
st.rerun()
# Handle pending input requests
if st.session_state.awaiting_input:
input_req = st.session_state.awaiting_input
# Create appropriate input widget
if input_req.options:
# Multiple choice input
st.markdown("---")
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
choice = st.radio(
"Choose your response:",
input_req.options,
key=f"choice_{input_req.input_type}",
horizontal=True
)
if st.button("Submit Choice", use_container_width=True,
type="primary"):
# Add user's choice to message history for certain types
if input_req.input_type in ["new_topic_choice", "quiz_choice"]:
st.session_state.messages.append(
{"role": "user", "content": choice})
# Process responses until we get another input request or end
while True:
response = get_next_bot_response(
choice) if choice else get_next_bot_response()
choice = None # Only send choice on first iteration
if response is None:
break
process_bot_response(response)
if response.user_input_request:
break
st.rerun()
else:
# Free text input
st.markdown("---")
col1, col2, col3 = st.columns([1, 2, 1])
with ((((col2)))):
if input_req.input_type == "new_question":
answer = st.text_input(
"Enter your new health topic:",
key=f"input_{input_req.input_type}",
placeholder="e.g., benefits of yoga, healthy diet tips..."
)
elif input_req.input_type == "quiz_answer":
answer = st.text_area(
"Your answer:",
key=f"input_{input_req.input_type}",
placeholder="Type your answer here...",
height=100
)
else:
answer = st.text_area(
"Your response:",
key=f"input_{input_req.input_type}",
placeholder="Type your response here...",
height=100
)
if st.button("Submit Response", use_container_width=True,
type="primary", disabled=not answer.strip()):
# Add user's response to message history
st.session_state.messages.append(
{"role": "user", "content": answer.strip()})
with st.spinner("Processing your response..."):
# Process responses until we get another input request
# or end
user_input = answer.strip()
while True:
response = get_next_bot_response(
user_input) if user_input \
else get_next_bot_response()
user_input = None # Only send input on first iteration
if response is None:
break
process_bot_response(response)
if response.user_input_request:
break
st.rerun()
# Footer
render_footer()