Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +27 -1
streamlit_app.py
CHANGED
|
@@ -58,4 +58,30 @@ if prompt := st.chat_input("Your Question"):
|
|
| 58 |
# Iterate through messages, excluding the very last user prompt which is the current input
|
| 59 |
for i in range(len(st.session_state.messages) - 1):
|
| 60 |
if st.session_state.messages[i]["role"] == "user" and st.session_state.messages[i+1]["role"] == "assistant":
|
| 61 |
-
history_for_respond.append((st.session_state.messages[i]["content"], st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Iterate through messages, excluding the very last user prompt which is the current input
|
| 59 |
for i in range(len(st.session_state.messages) - 1):
|
| 60 |
if st.session_state.messages[i]["role"] == "user" and st.session_state.messages[i+1]["role"] == "assistant":
|
| 61 |
+
history_for_respond.append((st.session_state.messages[i]["content"], st.session_state.messages[i+1]["content"]))
|
| 62 |
+
|
| 63 |
+
# Display assistant response in chat message container
|
| 64 |
+
with st.chat_message("assistant"):
|
| 65 |
+
with st.spinner("Thinking..."):
|
| 66 |
+
# Call your respond function
|
| 67 |
+
# The respond function expects user_input and chat_history in Gradio format
|
| 68 |
+
# It returns ("", updated_chat_history_in_gradio_format)
|
| 69 |
+
_, updated_gradio_history = respond(prompt, history_for_respond)
|
| 70 |
+
|
| 71 |
+
# Extract the latest assistant response from the updated history
|
| 72 |
+
if updated_gradio_history:
|
| 73 |
+
latest_turn = updated_gradio_history[-1]
|
| 74 |
+
# The bot response is the second element of the last tuple
|
| 75 |
+
full_response = latest_turn[1]
|
| 76 |
+
else:
|
| 77 |
+
full_response = "Sorry, I couldn't generate a response."
|
| 78 |
+
|
| 79 |
+
# Display the full response
|
| 80 |
+
st.markdown(full_response)
|
| 81 |
+
|
| 82 |
+
# Update Streamlit's session state history with the new user and assistant messages
|
| 83 |
+
# The user message was already added before calling respond
|
| 84 |
+
# Add the assistant message now
|
| 85 |
+
# Check if the last added message was the user prompt and if the response is not empty
|
| 86 |
+
if st.session_state.messages[-1]["role"] == "user" and full_response:
|
| 87 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|