streamlit-chat-simple-testing / src /streamlit_app.py
akhaliq's picture
akhaliq HF Staff
Upload src/streamlit_app.py with huggingface_hub
304cf53 verified
Raw
History Blame Contribute Delete
1.39 kB
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})