Text_input is not being cleared out/reset using streamlit

22 Views Asked by At

i am using streamlit to build a chat application and the query txt is not getting rest on submission. here is my code and screenshot for the same

def submit():
        record_timing()  # Record time before submitting message
        st.session_state.something = st.session_state.widget
        st.session_state.widget = ''

    if "messages" not in st.session_state:
        st.session_state.messages = [{"role": "assistant", "content": "How may I help you today?"}]

    if user_prompt := st.text_input("Your message here", on_change=submit, key="text_input"):  # Assign unique key
        st.session_state.messages.append({"role": "user", "content": user_prompt})
        with st.chat_message("user"):
            st.write(user_prompt)

    if st.session_state.messages[-1]["role"] != "assistant":
        with st.chat_message("assistant"):
            with st.spinner("Thinking..."):
                response = model(user_prompt, max_length, temp)
                placeholder = st.empty()
                full_response = ''
                for item in response:
                    full_response += item
                    placeholder.markdown(full_response)
                placeholder.markdown(full_response)
        message = {"role": "assistant", "content": full_response}
        st.session_state.messages.append(message)

output

i want the user question area to be reset after submission

0

There are 0 best solutions below