Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -148,30 +148,59 @@ st.markdown("</div>", unsafe_allow_html=True)
|
|
148 |
# User Input Section
|
149 |
user_input = st.text_area("Type your message:", height=100)
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
# User Input Section
|
149 |
user_input = st.text_area("Type your message:", height=100)
|
150 |
|
151 |
+
# Ensure session state is initialized for response tracking
|
152 |
+
if "response_ready" not in st.session_state:
|
153 |
+
st.session_state.response_ready = False # False until HAL responds
|
154 |
+
|
155 |
+
# Input box for user message
|
156 |
+
user_input = st.text_area("Type your message:", height=100)
|
157 |
+
|
158 |
+
# Create a placeholder for the Send button so it can be removed dynamically
|
159 |
+
send_button_placeholder = st.empty()
|
160 |
+
|
161 |
+
# Only show the Send button if HAL has not responded
|
162 |
+
if not st.session_state.response_ready:
|
163 |
+
if send_button_placeholder.button("Send"):
|
164 |
+
if user_input:
|
165 |
+
response, follow_up, st.session_state.chat_history, image_url = get_response(
|
166 |
+
system_message="You are a helpful AI assistant.",
|
167 |
+
user_text=user_input,
|
168 |
+
chat_history=st.session_state.chat_history
|
169 |
+
)
|
170 |
+
|
171 |
+
# Display HAL's response
|
172 |
+
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
|
173 |
+
|
174 |
+
# Display NASA image if available
|
175 |
+
if image_url:
|
176 |
+
st.image(image_url, caption="NASA Image of the Day")
|
177 |
+
|
178 |
+
# Set response_ready to True so the Send button disappears
|
179 |
+
st.session_state.response_ready = True
|
180 |
+
|
181 |
+
# Show follow-up questions & continue button **only if HAL has responded**
|
182 |
+
if st.session_state.response_ready:
|
183 |
+
follow_up_options = [follow_up, "Explain differently", "Give me an example"]
|
184 |
+
selected_option = st.radio("What would you like to do next?", follow_up_options)
|
185 |
+
|
186 |
+
# Placeholder for the Continue button so it can be refreshed dynamically
|
187 |
+
continue_button_placeholder = st.empty()
|
188 |
+
|
189 |
+
if continue_button_placeholder.button("Continue"):
|
190 |
+
if selected_option:
|
191 |
+
response, _, st.session_state.chat_history, _ = get_response(
|
192 |
+
system_message="You are a helpful AI assistant.",
|
193 |
+
user_text=selected_option,
|
194 |
+
chat_history=st.session_state.chat_history
|
195 |
+
)
|
196 |
+
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
|
197 |
+
|
198 |
+
# Reset response tracking so "Send" button appears for new input
|
199 |
+
st.session_state.response_ready = False
|
200 |
+
|
201 |
+
# Reset chat and buttons
|
202 |
+
if st.sidebar.button("Reset Chat"):
|
203 |
+
st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
|
204 |
+
st.session_state.response_ready = False # Reset so the Send button reappears
|
205 |
+
st.experimental_rerun()
|
206 |
+
|