Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- Api_how_to.ipynb +66 -0
- app.py +37 -0
Api_how_to.ipynb
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "aab24c61",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import requests\n",
|
11 |
+
"import json"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 6,
|
17 |
+
"id": "df363313",
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [
|
20 |
+
{
|
21 |
+
"name": "stdout",
|
22 |
+
"output_type": "stream",
|
23 |
+
"text": [
|
24 |
+
"[{'recipient_id': 'user', 'text': 'Hey! How are you?'}]\n"
|
25 |
+
]
|
26 |
+
}
|
27 |
+
],
|
28 |
+
"source": [
|
29 |
+
"user_input = \"Hi\"\n",
|
30 |
+
"payload = {\"sender\": \"user\", \"message\": user_input}\n",
|
31 |
+
"response = requests.post('http://localhost:5005/webhooks/rest/webhook', json=payload)\n",
|
32 |
+
"bot_reply = response.json()\n",
|
33 |
+
"print(bot_reply)"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": null,
|
39 |
+
"id": "9177e987",
|
40 |
+
"metadata": {},
|
41 |
+
"outputs": [],
|
42 |
+
"source": []
|
43 |
+
}
|
44 |
+
],
|
45 |
+
"metadata": {
|
46 |
+
"kernelspec": {
|
47 |
+
"display_name": "Python 3 (ipykernel)",
|
48 |
+
"language": "python",
|
49 |
+
"name": "python3"
|
50 |
+
},
|
51 |
+
"language_info": {
|
52 |
+
"codemirror_mode": {
|
53 |
+
"name": "ipython",
|
54 |
+
"version": 3
|
55 |
+
},
|
56 |
+
"file_extension": ".py",
|
57 |
+
"mimetype": "text/x-python",
|
58 |
+
"name": "python",
|
59 |
+
"nbconvert_exporter": "python",
|
60 |
+
"pygments_lexer": "ipython3",
|
61 |
+
"version": "3.10.11"
|
62 |
+
}
|
63 |
+
},
|
64 |
+
"nbformat": 4,
|
65 |
+
"nbformat_minor": 5
|
66 |
+
}
|
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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('http://localhost:5005/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};
|