create a streamlit interface with a chatbot with a carousel of cards
Drop files here
or click to upload
import streamlit as st
from streamlit_card_carousel import card_carousel
import random
import time
st.set_page_config(layout="wide")
# Initialize session state for messages if not exists
if "messages" not in st.session_state:
st.session_state.messages = []
st.title("Chatbot with Card Carousel")
# Create a container for the carousel
carousel_container = st.container()
with carousel_container:
# Sample cards data - you can modify this as needed
cards_data = [
{
"title": "Welcome!",
"text": "I can help you with various tasks. Click to learn more.",
"image": "https://images.unsplash.com/photo-1531379410502-63bfe8cdaf6f",
},
{
"title": "Features",
"text": "Explore our amazing features and capabilities.",
"image": "https://images.unsplash.com/photo-1518770660439-4636190af475",
},
{
"title": "Get Started",
"text": "Start chatting with me below to begin your journey.",
"image": "https://images.unsplash.com/photo-1453928582365-b6ad33cbcf64",
},
]
# Create the carousel of cards
cards = [
{
"key": f"card_{i}",
"title": card["title"],
"text": card["text"],
"image": card["image"],
}
for i, card in enumerate(cards_data)
]
selected_card = card_carousel(cards)
# Chat container
chat_container = st.container()
# Streamed response emulator
def response_generator():
responses = [
"I see you're interested in our services! How can I help you today?",
"Thanks for checking out our features. What would you like to know more about?",
"I'm here to assist you with any questions you might have!",
"Feel free to ask me anything about our services.",
]
response = random.choice(responses)
for word in response.split():
yield word + " "
time.sleep(0.05)
# Display chat messages from history
with chat_container:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
if prompt := st.chat_input("What would you like to know?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# 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})
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?