Spaces:
No application file
No application file
| import streamlit as st | |
| import random | |
| import time | |
| # Page config | |
| st.set_page_config(page_title="StreamChat", page_icon="🤖") | |
| # Initialize session state | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| if "thinking" not in st.session_state: | |
| st.session_state.thinking = False | |
| # Helper: mock LLM response | |
| def mock_llm(prompt: str) -> str: | |
| time.sleep(1.2) # Simulate latency | |
| replies = [ | |
| f"Interesting point! Re: '{prompt}' — I'd say let's explore this further.", | |
| f"Thanks for sharing '{prompt}'. From my perspective, that seems spot on.", | |
| f"Ah, '{prompt}' — reminds me of something I read in the docs.", | |
| f"'{prompt}'... 🤔 Let me ponder that for a moment.", | |
| ] | |
| return random.choice(replies) | |
| # Sidebar | |
| with st.sidebar: | |
| st.title("🤖 StreamChat") | |
| st.markdown("A lightweight Streamlit chatbot demo.") | |
| if st.button("Clear History"): | |
| st.session_state.messages.clear() | |
| st.rerun() | |
| # Display chat messages | |
| for msg in st.session_state.messages: | |
| with st.chat_message(msg["role"]): | |
| st.markdown(msg["content"]) | |
| # User input | |
| if prompt := st.chat_input("Ask me anything...", disabled=st.session_state.thinking): | |
| # Add user message | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| st.session_state.thinking = True | |
| st.rerun() | |
| # If thinking, generate response | |
| if st.session_state.thinking: | |
| last_user = st.session_state.messages[-1]["content"] | |
| with st.chat_message("assistant"): | |
| with st.spinner("Thinking..."): | |
| response = mock_llm(last_user) | |
| st.markdown(response) | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |
| st.session_state.thinking = False | |
| st.rerun() |