| 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}) |