Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,43 @@
|
|
1 |
-
import streamlit as st
|
2 |
import requests
|
3 |
-
import
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
st.
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
payload = {"sender": "user", "message": user_input}
|
19 |
-
response = requests.post('https://pvanand-rasa-moodbot.hf.space/webhooks/rest/webhook', json=payload)
|
20 |
-
bot_reply = response.json()
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
# Display
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
+
import streamlit as st
|
3 |
+
import random
|
4 |
+
import time
|
5 |
+
|
6 |
+
st.title("Simple chat")
|
7 |
|
8 |
+
# Initialize chat history
|
9 |
+
if "messages" not in st.session_state:
|
10 |
+
st.session_state.messages = []
|
11 |
|
12 |
+
# Display chat messages from history on app rerun
|
13 |
+
for message in st.session_state.messages:
|
14 |
+
with st.chat_message(message["role"]):
|
15 |
+
st.markdown(message["content"])
|
16 |
|
17 |
+
# Accept user input
|
18 |
+
if user_input := st.chat_input("What is up?"):
|
19 |
+
# Add user message to chat history
|
20 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
21 |
+
# Display user message in chat message container
|
22 |
+
with st.chat_message("user"):
|
23 |
+
st.markdown(user_input)
|
|
|
|
|
|
|
24 |
|
25 |
+
# Send user input to Rasa webhook
|
26 |
+
payload = {"sender": "user", "message": user_input}
|
27 |
+
response = requests.post('https://pvanand-rasa-moodbot.hf.space/webhooks/rest/webhook', json=payload)
|
28 |
+
bot_reply = response.json()
|
29 |
|
30 |
+
# Display assistant response in chat message container
|
31 |
+
with st.chat_message("assistant"):
|
32 |
+
message_placeholder = st.empty()
|
33 |
+
full_response = ""
|
34 |
+
assistant_response = random.choice(bot_reply)["text"]
|
35 |
+
# Simulate stream of response with milliseconds delay
|
36 |
+
for chunk in assistant_response.split():
|
37 |
+
full_response += chunk + " "
|
38 |
+
time.sleep(0.05)
|
39 |
+
# Add a blinking cursor to simulate typing
|
40 |
+
message_placeholder.markdown(full_response + "▌")
|
41 |
+
message_placeholder.markdown(full_response)
|
42 |
+
# Add assistant response to chat history
|
43 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|