the app should have two pages, and tabs on the top to select the page. In one page I want a chat interface, where "sender" is a customer and "receiver" is a CX agent. In the other page I want the same chat, but with sender/receiver inverted. In this page, every customer message has a button, that will generate a report/comment about the message received by the cx agent, and a templated reply to send back
Drop files here
or click to upload
import streamlit as st
st.set_page_config(layout="wide")
if "messages" not in st.session_state:
st.session_state.messages = []
if "chat_view" not in st.session_state:
st.session_state.chat_view = "customer"
# Tab navigation
tab1, tab2 = st.tabs(["Customer View", "Agent View"])
with tab1:
if st.session_state.chat_view != "customer":
st.session_state.chat_view = "customer"
st.rerun()
st.title("Customer Chat Interface")
# Display chat messages
for message in st.session_state.messages:
with st.chat_message("user" if message["sender"] == "customer" else "assistant"):
st.write(message["content"])
# Chat input for customer
if prompt := st.chat_input("Type your message here..."):
# Add customer message
new_message = {
"sender": "customer",
"receiver": "agent",
"content": prompt
}
st.session_state.messages.append(new_message)
st.rerun()
with tab2:
if st.session_state.chat_view != "agent":
st.session_state.chat_view = "agent"
st.rerun()
st.title("Agent Chat Interface")
# Display chat messages with action buttons for customer messages
for idx, message in enumerate(st.session_state.messages):
with st.chat_message("assistant" if message["sender"] == "customer" else "user"):
st.write(message["content"])
# Add action buttons for customer messages
if message["sender"] == "customer":
col1, col2 = st.columns(2)
with col1:
if st.button(f"Generate Report #{idx}"):
report = generate_report(message["content"])
st.info(f"Report: {report}")
with col2:
if st.button(f"Generate Reply #{idx}"):
reply = generate_template_reply(message["content"])
st.success(f"Suggested Reply: {reply}")
# Chat input for agent
if prompt := st.chat_input("Type your reply here..."):
# Add agent message
new_message = {
"sender": "agent",
"receiver": "customer",
"content": prompt
}
st.session_state.messages.append(new_message)
st.rerun()
def generate_report(message):
"""Generate a simple report about the customer message."""
return f"Customer message analysis: The message contains {len(message.split())} words. " \
f"Sentiment: {'Positive' if '!' in message else 'Neutral'}"
def generate_template_reply(message):
"""Generate a template reply based on the message content."""
if "help" in message.lower():
return "I understand you need assistance. I'll be happy to help you with that."
elif "thank" in message.lower():
return "You're welcome! Is there anything else I can help you with?"
else:
return "Thank you for your message. Let me assist you with that."
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?