Spaces:
Sleeping
Sleeping
Commit
·
c8ae549
1
Parent(s):
32a6203
Add application file
Browse files
app.py
CHANGED
|
@@ -1,4 +1,43 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
+
|
| 6 |
+
# Streamed response emulator
|
| 7 |
+
def response_generator():
|
| 8 |
+
response = random.choice(
|
| 9 |
+
[
|
| 10 |
+
"Hello there! How can I assist you today?",
|
| 11 |
+
"Hi, human! Is there anything I can help you with?",
|
| 12 |
+
"Do you need help?",
|
| 13 |
+
]
|
| 14 |
+
)
|
| 15 |
+
for word in response.split():
|
| 16 |
+
yield word + " "
|
| 17 |
+
time.sleep(0.05)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
st.title("Simple chat")
|
| 21 |
+
|
| 22 |
+
# Initialize chat history
|
| 23 |
+
if "messages" not in st.session_state:
|
| 24 |
+
st.session_state.messages = []
|
| 25 |
+
|
| 26 |
+
# Display chat messages from history on app rerun
|
| 27 |
+
for message in st.session_state.messages:
|
| 28 |
+
with st.chat_message(message["role"]):
|
| 29 |
+
st.markdown(message["content"])
|
| 30 |
+
|
| 31 |
+
# Accept user input
|
| 32 |
+
if prompt := st.chat_input("What is up?"):
|
| 33 |
+
# Add user message to chat history
|
| 34 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 35 |
+
# Display user message in chat message container
|
| 36 |
+
with st.chat_message("user"):
|
| 37 |
+
st.markdown(prompt)
|
| 38 |
+
|
| 39 |
+
# Display assistant response in chat message container
|
| 40 |
+
with st.chat_message("assistant"):
|
| 41 |
+
response = st.write_stream(response_generator())
|
| 42 |
+
# Add assistant response to chat history
|
| 43 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|