pvanand commited on
Commit
c0fe9b1
·
1 Parent(s): 6e1bf63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -31
app.py CHANGED
@@ -1,37 +1,43 @@
1
- import streamlit as st
2
  import requests
3
- import json
 
 
 
 
4
 
5
- st.title("Rasa Chatbot Interface")
 
 
6
 
7
- # Initialize or get the chat history
8
- if 'chat_history' not in st.session_state:
9
- st.session_state.chat_history = []
 
10
 
11
- with st.form(key='chat_form'):
12
- user_input = st.text_input("You:")
13
-
14
- # You can submit the form by pressing enter or by clicking the button
15
- submitted = st.form_submit_button("Send")
16
-
17
- if submitted and user_input:
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
- # Append the user message and bot reply to the chat history
23
- st.session_state.chat_history.append(("You:", user_input, True))
24
- st.session_state.chat_history.append(("Bot:", bot_reply[0]['text'], False))
 
25
 
26
- # Display the chat history
27
- for message in st.session_state.chat_history:
28
- sender, text, is_user = message
29
- alignment = "right" if is_user else "left"
30
- background_color = "#f5f5f5" # light background color
31
- text_color = "#333333" # dark text color
32
-
33
- st.markdown(
34
- f"<div style='text-align: {alignment};padding: 10px; margin: 5px; border-radius: 5px;'>{text}</div>",
35
- unsafe_allow_html=True,
36
- )
37
- # background-color: {background_color}; color: {text_color};
 
 
 
 
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})