I want to create a simple streamlit app that offers a chat bot. When I input to the chat bot, while the chat bot is thinking for a response, disable the input field. Once a response if displayed, allow for user input again.
To upload files, please first save the app
import streamlit as st
import random
import time
# Streamed response emulator
def response_generator():
responses = [
"Hello there! How can I assist you today?",
"Hi, human! Is there anything I can help you with?",
"Do you need help?",
]
response = random.choice(responses)
response_text = ""
for word in response.split():
response_text += word + " "
st.write(response_text)
time.sleep(0.1)
return response_text
st.title("Simple Chat Bot")
if "messages" not in st.session_state:
st.session_state.messages = []
if "is_chat_input_disabled" not in st.session_state:
st.session_state.is_chat_input_disabled = False
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?", disabled=st.session_state.is_chat_input_disabled) or st.session_state.is_chat_input_disabled:
if not st.session_state.is_chat_input_disabled:
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
st.session_state.is_chat_input_disabled = True
st.experimental_rerun()
elif st.session_state.is_chat_input_disabled:
response_text = response_generator()
st.session_state.messages.append({"role": "assistant", "content": response_text})
st.session_state.is_chat_input_disabled = False
st.rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?