import streamlit as st
import pyperclip
Add custom CSS for styling
st.markdown(
“”"
.message-container {
display: flex;
align-items: center;
justify-content: flex-start;
margin-bottom: 10px;
}
.user-message {
background-color: #e0f7fa;
color: #00796b;
padding: 10px;
border-radius: 10px;
max-width: 60%;
word-wrap: break-word;
}
.bot-message {
background-color: #fce4ec;
color: #ad1457;
padding: 10px;
border-radius: 10px;
max-width: 60%;
word-wrap: break-word;
}
.stContainer {
overflow-y: auto !important;
max-height: 500px !important;
}
“”",
unsafe_allow_html=True,
)
Initialize session state
if “messagelog” not in st.session_state:
st.session_state.messagelog =
Simulate messages (for demonstration purposes)
st.session_state.messagelog.append({“role”: “user”, “message”: “Hello, bot!”})
st.session_state.messagelog.append({“role”: “bot”, “message”: “Hello! How can I help you today?”})
Create conversation container
convo_con = st.container(border=True, height=500, key=“convo_con”)
Display messages
with convo_con:
for idx, message in enumerate(st.session_state.messagelog):
if message[“role”] == “user”:
st.markdown(
f"“”
{message[‘message’]}
user-icon
“”“,
unsafe_allow_html=True,
)
else:
bot_col, message_col = st.columns([1, 29])
with bot_col:
st.image(“images/ai.png”, width=40)
with message_col:
st.markdown(
f”“”
{message[‘message’]}
“”",
unsafe_allow_html=True,
)
marginbtn1, copy_button, retry_button, marginbtn2 = st.columns([4, 10, 90, 1])
with copy_button:
unique_copy_key = f’copy_{idx}’
if st.button(“Copy”, key=unique_copy_key):
pyperclip.copy(message[“message”])
with retry_button:
unique_retry_key = f’retry_{idx}’
if st.button(“Retry”, key=unique_retry_key):
st.rerun()
JavaScript for auto-scrolling
scroll_script = “”"
“”"
st.markdown(scroll_script, unsafe_allow_html=True)
Im running it automatically