Spaces:
No application file
No application file
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
# Page config
|
| 6 |
+
st.set_page_config(page_title="StreamChat", page_icon="🤖")
|
| 7 |
+
|
| 8 |
+
# Initialize session state
|
| 9 |
+
if "messages" not in st.session_state:
|
| 10 |
+
st.session_state.messages = []
|
| 11 |
+
if "thinking" not in st.session_state:
|
| 12 |
+
st.session_state.thinking = False
|
| 13 |
+
|
| 14 |
+
# Helper: mock LLM response
|
| 15 |
+
def mock_llm(prompt: str) -> str:
|
| 16 |
+
time.sleep(1.2) # Simulate latency
|
| 17 |
+
replies = [
|
| 18 |
+
f"Interesting point! Re: '{prompt}' — I'd say let's explore this further.",
|
| 19 |
+
f"Thanks for sharing '{prompt}'. From my perspective, that seems spot on.",
|
| 20 |
+
f"Ah, '{prompt}' — reminds me of something I read in the docs.",
|
| 21 |
+
f"'{prompt}'... 🤔 Let me ponder that for a moment.",
|
| 22 |
+
]
|
| 23 |
+
return random.choice(replies)
|
| 24 |
+
|
| 25 |
+
# Sidebar
|
| 26 |
+
with st.sidebar:
|
| 27 |
+
st.title("🤖 StreamChat")
|
| 28 |
+
st.markdown("A lightweight Streamlit chatbot demo.")
|
| 29 |
+
if st.button("Clear History"):
|
| 30 |
+
st.session_state.messages.clear()
|
| 31 |
+
st.rerun()
|
| 32 |
+
|
| 33 |
+
# Display chat messages
|
| 34 |
+
for msg in st.session_state.messages:
|
| 35 |
+
with st.chat_message(msg["role"]):
|
| 36 |
+
st.markdown(msg["content"])
|
| 37 |
+
|
| 38 |
+
# User input
|
| 39 |
+
if prompt := st.chat_input("Ask me anything...", disabled=st.session_state.thinking):
|
| 40 |
+
# Add user message
|
| 41 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 42 |
+
st.session_state.thinking = True
|
| 43 |
+
st.rerun()
|
| 44 |
+
|
| 45 |
+
# If thinking, generate response
|
| 46 |
+
if st.session_state.thinking:
|
| 47 |
+
last_user = st.session_state.messages[-1]["content"]
|
| 48 |
+
with st.chat_message("assistant"):
|
| 49 |
+
with st.spinner("Thinking..."):
|
| 50 |
+
response = mock_llm(last_user)
|
| 51 |
+
st.markdown(response)
|
| 52 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 53 |
+
st.session_state.thinking = False
|
| 54 |
+
st.rerun()
|