olamidegoriola commited on
Commit
fd7b88e
β€’
1 Parent(s): 789c61c

Upload 3 files

Browse files
Files changed (3) hide show
  1. Api_how_to.ipynb +66 -0
  2. README.md +8 -5
  3. app.py +43 -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
+ }
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Omdena Ng Lagos Chatbot
3
- emoji: πŸ“Š
4
- colorFrom: pink
5
- colorTo: red
6
- sdk: docker
 
 
7
  pinned: false
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: RASA Chat Interface Streamlit
3
+ emoji: πŸ†
4
+ colorFrom: purple
5
+ colorTo: yellow
6
+ sdk: streamlit
7
+ sdk_version: 1.25.0
8
+ app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
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})