2024-06-23 13:53:36 +00:00
|
|
|
import streamlit as st
|
2024-06-23 14:18:59 +00:00
|
|
|
import random
|
|
|
|
import time
|
2024-06-23 13:53:36 +00:00
|
|
|
|
2024-06-23 14:18:59 +00:00
|
|
|
|
|
|
|
# Streamed response emulator
|
|
|
|
def response_generator():
|
|
|
|
response = random.choice(
|
|
|
|
[
|
|
|
|
"Hello there! How can I assist you today?",
|
|
|
|
"Hi, human! Is there anything I can help you with?",
|
|
|
|
"Do you need help?",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
for word in response.split():
|
|
|
|
yield word + " "
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
|
|
|
|
|
|
st.title("Simple chat")
|
2024-06-23 13:58:54 +00:00
|
|
|
|
|
|
|
# Initialize chat history
|
|
|
|
if "messages" not in st.session_state:
|
|
|
|
st.session_state.messages = []
|
|
|
|
|
|
|
|
# Display chat messages from history on app rerun
|
|
|
|
for message in st.session_state.messages:
|
|
|
|
with st.chat_message(message["role"]):
|
|
|
|
st.markdown(message["content"])
|
2024-06-23 14:08:42 +00:00
|
|
|
|
2024-06-23 14:18:59 +00:00
|
|
|
# Accept user input
|
2024-06-23 14:08:42 +00:00
|
|
|
if prompt := st.chat_input("What is up?"):
|
2024-06-23 14:18:59 +00:00
|
|
|
# Add user message to chat history
|
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
2024-06-23 14:08:42 +00:00
|
|
|
# Display user message in chat message container
|
|
|
|
with st.chat_message("user"):
|
|
|
|
st.markdown(prompt)
|
2024-06-23 14:12:45 +00:00
|
|
|
|
2024-06-23 14:18:59 +00:00
|
|
|
# Display assistant response in chat message container
|
|
|
|
with st.chat_message("assistant"):
|
|
|
|
response = st.write_stream(response_generator())
|
|
|
|
# Add assistant response to chat history
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response})
|