I am trying to make an interface using Streamlit, but when the I refresh the page using ctrl + r or the round arrow from top left corner of chrome, the previous chat is no longer there.
import streamlit as st
st.title("Echo Bot")
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("What is up?"):
st.chat_message("user").markdown(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
response = f"Echo: {prompt}"
with st.chat_message("assistant"):
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
Seems to work using the decorator @st.cache_resource
(it replaces the deprecated @st.experimental_singleton
)
import streamlit as st
st.title("Echo Bot")
@st.cache_resource
def get_messages():
if "messages" not in st.session_state:
st.session_state.messages = []
return st.session_state.messages
st.session_state.messages = get_messages()
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("What is up?"):
st.chat_message("user").markdown(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
response = f"Echo: {prompt}"
with st.chat_message("assistant"):
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})