File size: 1,393 Bytes
7aa8120 304cf53 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import streamlit as st
import random
import time
# Page config
st.set_page_config(page_title="Simple ChatBot", page_icon="🤖")
# Session state for messages
if "messages" not in st.session_state:
st.session_state.messages = []
# Title
st.title("💬 Simple ChatBot")
st.caption("Type your message below and press Enter.")
# Display chat history
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Chat input
if prompt := st.chat_input("Ask me anything..."):
# User message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Bot response (simple echo + random choice)
bot_response = random.choice(
[
f"Got it: {prompt}",
f"You said: {prompt}",
f"Interesting! You mentioned: {prompt}",
f"Echo: {prompt}",
]
)
# Simulate typing
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for chunk in bot_response.split():
full_response += chunk + " "
time.sleep(0.05)
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response}) |