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