Create a chatbot interface with questions entered on the left and same question and response shown on the right side in incremental fashion
To upload files, please first save the app
import streamlit as st
import time
st.title("Dual Chat Interface")
# Initialize chat history if not exists
if "messages" not in st.session_state:
st.session_state.messages = []
# Create two columns
left_col, right_col = st.columns(2)
# Left column - Chat input
with left_col:
st.subheader("Ask your question")
if prompt := st.chat_input("What's on your mind?", key="left_input"):
# Add message to history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display the message
with st.chat_message("user"):
st.write(prompt)
# Right column - Chat display with incremental response
with right_col:
st.subheader("Response")
# Display chat messages from history
for message in st.session_state.messages:
# Show user message
with st.chat_message("user"):
st.write(message["content"])
# Generate and show assistant response
with st.chat_message("assistant"):
response = f"I received your message: '{message['content']}'. This is a demo response that types out character by character."
# Create an empty placeholder
message_placeholder = st.empty()
full_response = ""
# Simulate stream of response with a typing effect
for chunk in response.split():
full_response += chunk + " "
time.sleep(0.05) # Add delay between words
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?