heyal commited on
Commit
cb1ef25
·
1 Parent(s): 961431b

added app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+ import random
4
+ import time
5
+
6
+ st.title("Rasa Chatbot Interface")
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})