Spaces:
Runtime error
Runtime error
Merge branch 'feature-fsm-transitions' into 'main'
Browse filesFeature fsm transitions
See merge request tangibleai/community/mathtext-fastapi!9
- docs/transitions_math_quiz_example.ipynb +368 -0
- mathtext_fastapi/conversation_manager.py +63 -12
- mathtext_fastapi/math_quiz_fsm.py +53 -0
- requirements.txt +4 -0
- scripts/make_request.py +26 -26
docs/transitions_math_quiz_example.ipynb
ADDED
@@ -0,0 +1,368 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 19,
|
6 |
+
"id": "d3da0422",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import random\n",
|
11 |
+
"\n",
|
12 |
+
"from transitions import State, Machine"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 20,
|
18 |
+
"id": "07cfb740",
|
19 |
+
"metadata": {},
|
20 |
+
"outputs": [],
|
21 |
+
"source": [
|
22 |
+
"class MathQuizFSM(object):\n",
|
23 |
+
" states = [\n",
|
24 |
+
" 'quiz_start', \n",
|
25 |
+
" 'quiz_question', \n",
|
26 |
+
" 'quiz_end'\n",
|
27 |
+
" ]\n",
|
28 |
+
"\n",
|
29 |
+
" transitions = [\n",
|
30 |
+
" ['ask_second_question', 'quiz_start', 'quiz_question'],\n",
|
31 |
+
" ['ask_next_question', 'quiz_question', 'quiz_question'],\n",
|
32 |
+
" ['exit', 'quiz_start', 'quiz_end'],\n",
|
33 |
+
" ['exit', 'quiz_question', 'quiz_end'],\n",
|
34 |
+
" ]\n",
|
35 |
+
" \n",
|
36 |
+
" \n",
|
37 |
+
" def __init__(self):\n",
|
38 |
+
" # Instantiate the FSM\n",
|
39 |
+
" self.machine = Machine(model=self, states=MathQuizFSM.states, transitions=MathQuizFSM.transitions,initial='quiz_start')\n",
|
40 |
+
"\n",
|
41 |
+
" # Instantiate variables necessary for tracking activity\n",
|
42 |
+
" self.question_nums = [2, 3]\n",
|
43 |
+
" self.correct_answer = 5\n",
|
44 |
+
" self.student_answer = 0\n",
|
45 |
+
" self.is_correct_answer = False\n",
|
46 |
+
" self.response_text = \"What is 2 + 3?\"\n",
|
47 |
+
"\n",
|
48 |
+
" # Define transitions\n",
|
49 |
+
"# self.machine.add_transition('ask_second_question', 'quiz_start', 'quiz_question')\n",
|
50 |
+
"# self.machine.add_transition('ask_next_question', 'quiz_question', 'quiz_question')\n",
|
51 |
+
"# self.machine.add_transition('exit', 'quiz_start', 'quiz_end')\n",
|
52 |
+
"# self.machine.add_transition('exit', 'quiz_question', 'quiz_end')\n",
|
53 |
+
"\n",
|
54 |
+
" # Define functions to run on transitions\n",
|
55 |
+
" self.machine.on_enter_quiz_question('generate_math_problem')\n",
|
56 |
+
" self.machine.on_exit_quiz_question('validate_answer')\n",
|
57 |
+
"\n",
|
58 |
+
" def validate_answer(self):\n",
|
59 |
+
" if self.student_answer == 'exit':\n",
|
60 |
+
" self.machine.set_state('quiz_end')\n",
|
61 |
+
" return [\"Come back any time!\"]\n",
|
62 |
+
" elif self.correct_answer == self.student_answer:\n",
|
63 |
+
" self.machine.set_state('quiz_question')\n",
|
64 |
+
" self.generate_math_problem()\n",
|
65 |
+
" return ['Great job!', self.response_text]\n",
|
66 |
+
" else:\n",
|
67 |
+
" return [\"That's not quite right. Try again.\",self.response_text]\n",
|
68 |
+
" \n",
|
69 |
+
" def generate_math_problem(self):\n",
|
70 |
+
" self.question_nums = random.sample(range(1,100),2)\n",
|
71 |
+
" self.response_text = f\"What is {self.question_nums[0]} + {self.question_nums[1]}\"\n",
|
72 |
+
" self.correct_answer = self.question_nums[0] + self.question_nums[1]\n"
|
73 |
+
]
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"cell_type": "code",
|
77 |
+
"execution_count": 21,
|
78 |
+
"id": "ebdf92ae",
|
79 |
+
"metadata": {},
|
80 |
+
"outputs": [],
|
81 |
+
"source": [
|
82 |
+
"test = MathQuizFSM()"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"cell_type": "code",
|
87 |
+
"execution_count": 22,
|
88 |
+
"id": "92024fcc",
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [
|
91 |
+
{
|
92 |
+
"data": {
|
93 |
+
"text/plain": [
|
94 |
+
"'quiz_start'"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
"execution_count": 22,
|
98 |
+
"metadata": {},
|
99 |
+
"output_type": "execute_result"
|
100 |
+
}
|
101 |
+
],
|
102 |
+
"source": [
|
103 |
+
"# Set as `quiz_start` due to the initial setting in Line 10\n",
|
104 |
+
"test.state"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "code",
|
109 |
+
"execution_count": 23,
|
110 |
+
"id": "fd1ba433",
|
111 |
+
"metadata": {},
|
112 |
+
"outputs": [
|
113 |
+
{
|
114 |
+
"data": {
|
115 |
+
"text/plain": [
|
116 |
+
"['quiz_start', 'quiz_question', 'quiz_end']"
|
117 |
+
]
|
118 |
+
},
|
119 |
+
"execution_count": 23,
|
120 |
+
"metadata": {},
|
121 |
+
"output_type": "execute_result"
|
122 |
+
}
|
123 |
+
],
|
124 |
+
"source": [
|
125 |
+
"# Available states for the quiz module\n",
|
126 |
+
"test.states"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": 24,
|
132 |
+
"id": "bb190089",
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stdout",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"What is 2 + 3?\n",
|
140 |
+
"Initial Correct Answer: 5\n",
|
141 |
+
"Initial Student Answer: 0\n"
|
142 |
+
]
|
143 |
+
}
|
144 |
+
],
|
145 |
+
"source": [
|
146 |
+
"# When the FSM is created, it comes with a default question/answer pair loaded\n",
|
147 |
+
"print(test.response_text)\n",
|
148 |
+
"print(f\"Initial Correct Answer: {test.correct_answer}\")\n",
|
149 |
+
"print(f\"Initial Student Answer: {test.student_answer}\")"
|
150 |
+
]
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"cell_type": "code",
|
154 |
+
"execution_count": 25,
|
155 |
+
"id": "3de7c4e0",
|
156 |
+
"metadata": {},
|
157 |
+
"outputs": [
|
158 |
+
{
|
159 |
+
"data": {
|
160 |
+
"text/plain": [
|
161 |
+
"[\"That's not quite right. Try again.\", 'What is 2 + 3?']"
|
162 |
+
]
|
163 |
+
},
|
164 |
+
"execution_count": 25,
|
165 |
+
"metadata": {},
|
166 |
+
"output_type": "execute_result"
|
167 |
+
}
|
168 |
+
],
|
169 |
+
"source": [
|
170 |
+
"# Calling the validation fails because the answer is wrong. The state remains the same.\n",
|
171 |
+
"test.validate_answer()"
|
172 |
+
]
|
173 |
+
},
|
174 |
+
{
|
175 |
+
"cell_type": "code",
|
176 |
+
"execution_count": 26,
|
177 |
+
"id": "4935b470",
|
178 |
+
"metadata": {},
|
179 |
+
"outputs": [],
|
180 |
+
"source": [
|
181 |
+
"# The student tries again\n",
|
182 |
+
"test.student_answer = 5"
|
183 |
+
]
|
184 |
+
},
|
185 |
+
{
|
186 |
+
"cell_type": "code",
|
187 |
+
"execution_count": 27,
|
188 |
+
"id": "03722434",
|
189 |
+
"metadata": {},
|
190 |
+
"outputs": [
|
191 |
+
{
|
192 |
+
"data": {
|
193 |
+
"text/plain": [
|
194 |
+
"['Great job!', 'What is 58 + 89']"
|
195 |
+
]
|
196 |
+
},
|
197 |
+
"execution_count": 27,
|
198 |
+
"metadata": {},
|
199 |
+
"output_type": "execute_result"
|
200 |
+
}
|
201 |
+
],
|
202 |
+
"source": [
|
203 |
+
"# Since the student answered correctly, MathQuizFSM generates a new math problem\n",
|
204 |
+
"test.validate_answer()"
|
205 |
+
]
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"cell_type": "code",
|
209 |
+
"execution_count": 28,
|
210 |
+
"id": "d98a4d5b",
|
211 |
+
"metadata": {},
|
212 |
+
"outputs": [
|
213 |
+
{
|
214 |
+
"data": {
|
215 |
+
"text/plain": [
|
216 |
+
"'quiz_question'"
|
217 |
+
]
|
218 |
+
},
|
219 |
+
"execution_count": 28,
|
220 |
+
"metadata": {},
|
221 |
+
"output_type": "execute_result"
|
222 |
+
}
|
223 |
+
],
|
224 |
+
"source": [
|
225 |
+
"# It will repeatedly re-activate the same state\n",
|
226 |
+
"test.state"
|
227 |
+
]
|
228 |
+
},
|
229 |
+
{
|
230 |
+
"cell_type": "code",
|
231 |
+
"execution_count": 29,
|
232 |
+
"id": "76c8a5b2",
|
233 |
+
"metadata": {},
|
234 |
+
"outputs": [
|
235 |
+
{
|
236 |
+
"data": {
|
237 |
+
"text/plain": [
|
238 |
+
"[\"That's not quite right. Try again.\", 'What is 58 + 89']"
|
239 |
+
]
|
240 |
+
},
|
241 |
+
"execution_count": 29,
|
242 |
+
"metadata": {},
|
243 |
+
"output_type": "execute_result"
|
244 |
+
}
|
245 |
+
],
|
246 |
+
"source": [
|
247 |
+
"test.validate_answer()"
|
248 |
+
]
|
249 |
+
},
|
250 |
+
{
|
251 |
+
"cell_type": "code",
|
252 |
+
"execution_count": 30,
|
253 |
+
"id": "ec0a7e6a",
|
254 |
+
"metadata": {},
|
255 |
+
"outputs": [],
|
256 |
+
"source": [
|
257 |
+
"test.student_answer = 128"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
{
|
261 |
+
"cell_type": "code",
|
262 |
+
"execution_count": 31,
|
263 |
+
"id": "a093ff27",
|
264 |
+
"metadata": {},
|
265 |
+
"outputs": [
|
266 |
+
{
|
267 |
+
"data": {
|
268 |
+
"text/plain": [
|
269 |
+
"[\"That's not quite right. Try again.\", 'What is 58 + 89']"
|
270 |
+
]
|
271 |
+
},
|
272 |
+
"execution_count": 31,
|
273 |
+
"metadata": {},
|
274 |
+
"output_type": "execute_result"
|
275 |
+
}
|
276 |
+
],
|
277 |
+
"source": [
|
278 |
+
"test.validate_answer()"
|
279 |
+
]
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"cell_type": "code",
|
283 |
+
"execution_count": 32,
|
284 |
+
"id": "f992d34d",
|
285 |
+
"metadata": {},
|
286 |
+
"outputs": [],
|
287 |
+
"source": [
|
288 |
+
"test.student_answer = 'exit'"
|
289 |
+
]
|
290 |
+
},
|
291 |
+
{
|
292 |
+
"cell_type": "code",
|
293 |
+
"execution_count": 33,
|
294 |
+
"id": "28800a2b",
|
295 |
+
"metadata": {},
|
296 |
+
"outputs": [
|
297 |
+
{
|
298 |
+
"data": {
|
299 |
+
"text/plain": [
|
300 |
+
"['Come back any time!']"
|
301 |
+
]
|
302 |
+
},
|
303 |
+
"execution_count": 33,
|
304 |
+
"metadata": {},
|
305 |
+
"output_type": "execute_result"
|
306 |
+
}
|
307 |
+
],
|
308 |
+
"source": [
|
309 |
+
"test.validate_answer()"
|
310 |
+
]
|
311 |
+
},
|
312 |
+
{
|
313 |
+
"cell_type": "code",
|
314 |
+
"execution_count": 34,
|
315 |
+
"id": "360ef774",
|
316 |
+
"metadata": {},
|
317 |
+
"outputs": [
|
318 |
+
{
|
319 |
+
"data": {
|
320 |
+
"text/plain": [
|
321 |
+
"'quiz_end'"
|
322 |
+
]
|
323 |
+
},
|
324 |
+
"execution_count": 34,
|
325 |
+
"metadata": {},
|
326 |
+
"output_type": "execute_result"
|
327 |
+
}
|
328 |
+
],
|
329 |
+
"source": [
|
330 |
+
"test.state"
|
331 |
+
]
|
332 |
+
},
|
333 |
+
{
|
334 |
+
"cell_type": "code",
|
335 |
+
"execution_count": null,
|
336 |
+
"id": "3f0392ae",
|
337 |
+
"metadata": {},
|
338 |
+
"outputs": [],
|
339 |
+
"source": []
|
340 |
+
}
|
341 |
+
],
|
342 |
+
"metadata": {
|
343 |
+
"kernelspec": {
|
344 |
+
"display_name": "base",
|
345 |
+
"language": "python",
|
346 |
+
"name": "python3"
|
347 |
+
},
|
348 |
+
"language_info": {
|
349 |
+
"codemirror_mode": {
|
350 |
+
"name": "ipython",
|
351 |
+
"version": 3
|
352 |
+
},
|
353 |
+
"file_extension": ".py",
|
354 |
+
"mimetype": "text/x-python",
|
355 |
+
"name": "python",
|
356 |
+
"nbconvert_exporter": "python",
|
357 |
+
"pygments_lexer": "ipython3",
|
358 |
+
"version": "3.9.7"
|
359 |
+
},
|
360 |
+
"vscode": {
|
361 |
+
"interpreter": {
|
362 |
+
"hash": "32cf04bfac80a5e1e74e86fca42ae7f3079b15fa61041a60732bc19e88699268"
|
363 |
+
}
|
364 |
+
}
|
365 |
+
},
|
366 |
+
"nbformat": 4,
|
367 |
+
"nbformat_minor": 5
|
368 |
+
}
|
mathtext_fastapi/conversation_manager.py
CHANGED
@@ -1,13 +1,24 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
|
|
|
|
|
|
3 |
import requests
|
4 |
|
5 |
from dotenv import load_dotenv
|
6 |
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
|
|
|
|
|
|
7 |
|
8 |
load_dotenv()
|
9 |
|
10 |
-
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
def create_text_message(message_text, whatsapp_id):
|
@@ -89,7 +100,13 @@ def create_interactive_message(message_text, button_options, whatsapp_id):
|
|
89 |
return data
|
90 |
|
91 |
|
92 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
""" Evaluates the conversation's current state to determine the next state
|
94 |
|
95 |
Input
|
@@ -106,15 +123,46 @@ def return_next_conversational_state(context_data, user_message):
|
|
106 |
'input_prompt': "Welcome to our math practice. What would you like to try? Type add or subtract.",
|
107 |
'state': "welcome-sequence"
|
108 |
}
|
109 |
-
elif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
message_package = {
|
111 |
-
'messages':
|
112 |
-
|
113 |
-
|
114 |
-
"Type your response as a number. For example, for '1 + 1', you'd write 2."
|
115 |
-
],
|
116 |
-
'input_prompt': "Here's the first one... What's 2+2?",
|
117 |
-
'state': "add-question-sequence"
|
118 |
}
|
119 |
elif user_message == 'subtract':
|
120 |
message_package = {
|
@@ -125,7 +173,7 @@ def return_next_conversational_state(context_data, user_message):
|
|
125 |
'input_prompt': "Here's the first one... What's 3-1?",
|
126 |
'state': "subtract-question-sequence"
|
127 |
}
|
128 |
-
elif user_message == 'exit':
|
129 |
message_package = {
|
130 |
'messages': [
|
131 |
"Great, thanks for practicing math today. Come back any time."
|
@@ -165,15 +213,18 @@ def manage_conversation_response(data_json):
|
|
165 |
|
166 |
whatsapp_id = message_data['author_id']
|
167 |
user_message = message_data['message_body']
|
|
|
168 |
|
169 |
# TODO: Need to incorporate nlu_response into wormhole by checking answers against database (spreadsheet?)
|
170 |
nlu_response = evaluate_message_with_nlu(message_data)
|
171 |
|
172 |
message_package = return_next_conversational_state(
|
173 |
context_data,
|
174 |
-
user_message
|
|
|
175 |
)
|
176 |
|
|
|
177 |
headers = {
|
178 |
'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
|
179 |
'Content-Type': 'application/json'
|
|
|
1 |
+
import base64
|
2 |
+
import dill
|
3 |
import os
|
4 |
import json
|
5 |
+
import jsonpickle
|
6 |
+
import pickle
|
7 |
+
import random
|
8 |
import requests
|
9 |
|
10 |
from dotenv import load_dotenv
|
11 |
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
12 |
+
from mathtext_fastapi.math_quiz_fsm import MathQuizFSM
|
13 |
+
from supabase import create_client
|
14 |
+
from transitions import Machine
|
15 |
|
16 |
load_dotenv()
|
17 |
|
18 |
+
SUPA = create_client(
|
19 |
+
os.environ.get('SUPABASE_URL'),
|
20 |
+
os.environ.get('SUPABASE_KEY')
|
21 |
+
)
|
22 |
|
23 |
|
24 |
def create_text_message(message_text, whatsapp_id):
|
|
|
100 |
return data
|
101 |
|
102 |
|
103 |
+
def pickle_and_encode_state_machine(state_machine):
|
104 |
+
dump = pickle.dumps(state_machine)
|
105 |
+
dump_encoded = base64.b64encode(dump).decode('utf-8')
|
106 |
+
return dump_encoded
|
107 |
+
|
108 |
+
|
109 |
+
def return_next_conversational_state(context_data, user_message, contact_uuid):
|
110 |
""" Evaluates the conversation's current state to determine the next state
|
111 |
|
112 |
Input
|
|
|
123 |
'input_prompt': "Welcome to our math practice. What would you like to try? Type add or subtract.",
|
124 |
'state': "welcome-sequence"
|
125 |
}
|
126 |
+
elif context_data['state'] == 'addition-question-sequence' or \
|
127 |
+
user_message == 'add':
|
128 |
+
|
129 |
+
fsm_check = SUPA.table('state_machines').select("*").eq(
|
130 |
+
"contact_uuid",
|
131 |
+
contact_uuid
|
132 |
+
).execute()
|
133 |
+
|
134 |
+
if fsm_check.data == []:
|
135 |
+
math_quiz_state_machine = MathQuizFSM()
|
136 |
+
messages = [math_quiz_state_machine.response_text]
|
137 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
138 |
+
|
139 |
+
SUPA.table('state_machines').insert({
|
140 |
+
'contact_uuid': contact_uuid,
|
141 |
+
'addition3': dump_encoded
|
142 |
+
}).execute()
|
143 |
+
else:
|
144 |
+
undump_encoded = base64.b64decode(
|
145 |
+
fsm_check.data[0]['addition3'].encode('utf-8')
|
146 |
+
)
|
147 |
+
math_quiz_state_machine = pickle.loads(undump_encoded)
|
148 |
+
math_quiz_state_machine.student_answer == user_message
|
149 |
+
messages = math_quiz_state_machine.validate_answer()
|
150 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
151 |
+
SUPA.table('state_machines').update({
|
152 |
+
'addition3': dump_encoded
|
153 |
+
}).eq(
|
154 |
+
"contact_uuid", contact_uuid
|
155 |
+
).execute()
|
156 |
+
|
157 |
+
if user_message == 'exit':
|
158 |
+
state_label = 'exit'
|
159 |
+
else:
|
160 |
+
state_label = 'addition-question-sequence'
|
161 |
+
|
162 |
message_package = {
|
163 |
+
'messages': messages,
|
164 |
+
'input_prompt': "temporary value",
|
165 |
+
'state': state_label
|
|
|
|
|
|
|
|
|
166 |
}
|
167 |
elif user_message == 'subtract':
|
168 |
message_package = {
|
|
|
173 |
'input_prompt': "Here's the first one... What's 3-1?",
|
174 |
'state': "subtract-question-sequence"
|
175 |
}
|
176 |
+
elif context_data['state'] == 'exit' or user_message == 'exit':
|
177 |
message_package = {
|
178 |
'messages': [
|
179 |
"Great, thanks for practicing math today. Come back any time."
|
|
|
213 |
|
214 |
whatsapp_id = message_data['author_id']
|
215 |
user_message = message_data['message_body']
|
216 |
+
contact_uuid = message_data['contact_uuid']
|
217 |
|
218 |
# TODO: Need to incorporate nlu_response into wormhole by checking answers against database (spreadsheet?)
|
219 |
nlu_response = evaluate_message_with_nlu(message_data)
|
220 |
|
221 |
message_package = return_next_conversational_state(
|
222 |
context_data,
|
223 |
+
user_message,
|
224 |
+
contact_uuid
|
225 |
)
|
226 |
|
227 |
+
|
228 |
headers = {
|
229 |
'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
|
230 |
'Content-Type': 'application/json'
|
mathtext_fastapi/math_quiz_fsm.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from transitions import Machine
|
3 |
+
|
4 |
+
|
5 |
+
class MathQuizFSM(object):
|
6 |
+
states = [
|
7 |
+
'quiz_start',
|
8 |
+
'quiz_question',
|
9 |
+
'quiz_end'
|
10 |
+
]
|
11 |
+
|
12 |
+
transitions = [
|
13 |
+
['ask_second_question', 'quiz_start', 'quiz_question'],
|
14 |
+
['ask_next_question', 'quiz_question', 'quiz_question'],
|
15 |
+
['exit', 'quiz_start', 'quiz_end'],
|
16 |
+
['exit', 'quiz_question', 'quiz_end'],
|
17 |
+
]
|
18 |
+
|
19 |
+
def __init__(self):
|
20 |
+
# Instantiate the FSM
|
21 |
+
self.machine = Machine(
|
22 |
+
model=self,
|
23 |
+
states=MathQuizFSM.states,
|
24 |
+
transitions=MathQuizFSM.transitions,
|
25 |
+
initial='quiz_start'
|
26 |
+
)
|
27 |
+
|
28 |
+
# Instantiate variables necessary for tracking activity
|
29 |
+
self.question_nums = [2, 3]
|
30 |
+
self.correct_answer = 5
|
31 |
+
self.student_answer = 0
|
32 |
+
self.is_correct_answer = False
|
33 |
+
self.response_text = "What is 2 + 3?"
|
34 |
+
|
35 |
+
# Define functions to run on transitions
|
36 |
+
self.machine.on_enter_quiz_question('generate_math_problem')
|
37 |
+
self.machine.on_exit_quiz_question('validate_answer')
|
38 |
+
|
39 |
+
def validate_answer(self):
|
40 |
+
if self.student_answer == 'exit':
|
41 |
+
self.machine.set_state('quiz_end')
|
42 |
+
return ["Come back any time!"]
|
43 |
+
elif self.correct_answer == self.student_answer:
|
44 |
+
self.machine.set_state('quiz_question')
|
45 |
+
self.generate_math_problem()
|
46 |
+
return ['Great job!', self.response_text]
|
47 |
+
else:
|
48 |
+
return ["That's not quite right. Try again.", self.response_text]
|
49 |
+
|
50 |
+
def generate_math_problem(self):
|
51 |
+
self.question_nums = random.sample(range(1,100),2)
|
52 |
+
self.response_text = f"What is {self.question_nums[0]} + {self.question_nums[1]}"
|
53 |
+
self.correct_answer = self.question_nums[0] + self.question_nums[1]
|
requirements.txt
CHANGED
@@ -1,7 +1,11 @@
|
|
|
|
|
|
1 |
mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
|
2 |
fastapi==0.74.*
|
3 |
pydantic==1.10.*
|
4 |
requests==2.27.*
|
5 |
sentencepiece==0.1.*
|
6 |
supabase
|
|
|
7 |
uvicorn==0.17.*
|
|
|
|
1 |
+
dill
|
2 |
+
jsonpickle
|
3 |
mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
|
4 |
fastapi==0.74.*
|
5 |
pydantic==1.10.*
|
6 |
requests==2.27.*
|
7 |
sentencepiece==0.1.*
|
8 |
supabase
|
9 |
+
transitions
|
10 |
uvicorn==0.17.*
|
11 |
+
|
scripts/make_request.py
CHANGED
@@ -13,7 +13,7 @@ def add_message_text_to_sample_object(message_text):
|
|
13 |
"test message"
|
14 |
|
15 |
Output
|
16 |
-
- b_string: json b-string -
|
17 |
|
18 |
Example Output
|
19 |
b'{"context": "hi", "message_data": {"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "test message", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}'
|
@@ -28,37 +28,38 @@ def add_message_text_to_sample_object(message_text):
|
|
28 |
return b_string
|
29 |
|
30 |
|
31 |
-
def run_simulated_request(endpoint, sample_answer, context
|
32 |
print(f"Case: {sample_answer}")
|
33 |
b_string = add_message_text_to_sample_object(sample_answer)
|
34 |
|
35 |
if endpoint == 'sentiment-analysis' or endpoint == 'text2int':
|
36 |
-
request = requests.post(
|
37 |
-
f'http://localhost:7860/{endpoint}',
|
38 |
json={'content': sample_answer}
|
39 |
).json()
|
40 |
else:
|
41 |
-
request = requests.post(
|
42 |
-
f'http://localhost:7860/{endpoint}',
|
43 |
data=b_string
|
44 |
).json()
|
45 |
|
46 |
print(request)
|
47 |
|
48 |
-
|
49 |
-
run_simulated_request('
|
50 |
-
run_simulated_request('
|
51 |
-
run_simulated_request('nlu', '
|
52 |
-
run_simulated_request('nlu', 'eight
|
53 |
-
run_simulated_request('nlu', '
|
54 |
-
run_simulated_request('nlu', '8')
|
55 |
-
run_simulated_request('nlu',
|
56 |
-
run_simulated_request('nlu', '
|
57 |
-
run_simulated_request('nlu', '
|
58 |
-
run_simulated_request('
|
|
|
59 |
run_simulated_request('manager', 'add')
|
60 |
-
run_simulated_request('manager', 'subtract')
|
61 |
-
run_simulated_request('manager', 'exit')
|
62 |
|
63 |
|
64 |
# Example of simplified object received from Turn.io stacks
|
@@ -66,12 +67,12 @@ run_simulated_request('manager', 'exit')
|
|
66 |
# NOTE: This is actually a bstring, not a dict
|
67 |
simplified_json = {
|
68 |
"context": {
|
69 |
-
"user":"+57787919091",
|
70 |
-
"state":"answer-addition-problem",
|
71 |
-
"bot_message":"What is 2+2?",
|
72 |
-
"user_message":"eight",
|
73 |
"type": "ask"
|
74 |
-
},
|
75 |
"message_data": {
|
76 |
"author_id": "+57787919091",
|
77 |
"author_type": "OWNER",
|
@@ -85,7 +86,6 @@ simplified_json = {
|
|
85 |
}
|
86 |
|
87 |
|
88 |
-
|
89 |
# Full example of event data from Turn.io
|
90 |
# simplified_json is built from this in Turn.io
|
91 |
# full_json = {
|
@@ -144,4 +144,4 @@ simplified_json = {
|
|
144 |
# 'type': 'text'
|
145 |
# },
|
146 |
# 'type': 'message'
|
147 |
-
# }
|
|
|
13 |
"test message"
|
14 |
|
15 |
Output
|
16 |
+
- b_string: json b-string - simulated Turn.io message data
|
17 |
|
18 |
Example Output
|
19 |
b'{"context": "hi", "message_data": {"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "test message", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}'
|
|
|
28 |
return b_string
|
29 |
|
30 |
|
31 |
+
def run_simulated_request(endpoint, sample_answer, context=None):
|
32 |
print(f"Case: {sample_answer}")
|
33 |
b_string = add_message_text_to_sample_object(sample_answer)
|
34 |
|
35 |
if endpoint == 'sentiment-analysis' or endpoint == 'text2int':
|
36 |
+
request = requests.post(
|
37 |
+
url=f'http://localhost:7860/{endpoint}',
|
38 |
json={'content': sample_answer}
|
39 |
).json()
|
40 |
else:
|
41 |
+
request = requests.post(
|
42 |
+
url=f'http://localhost:7860/{endpoint}',
|
43 |
data=b_string
|
44 |
).json()
|
45 |
|
46 |
print(request)
|
47 |
|
48 |
+
|
49 |
+
# run_simulated_request('sentiment-analysis', 'I reject it')
|
50 |
+
# run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')
|
51 |
+
# run_simulated_request('nlu', 'test message')
|
52 |
+
# run_simulated_request('nlu', 'eight')
|
53 |
+
# run_simulated_request('nlu', 'eight, nine, ten')
|
54 |
+
# run_simulated_request('nlu', '8, 9, 10')
|
55 |
+
# run_simulated_request('nlu', '8')
|
56 |
+
# run_simulated_request('nlu', "I don't know")
|
57 |
+
# run_simulated_request('nlu', 'Today is a wonderful day')
|
58 |
+
# run_simulated_request('nlu', 'IDK 5?')
|
59 |
+
# run_simulated_request('manager', '')
|
60 |
run_simulated_request('manager', 'add')
|
61 |
+
# run_simulated_request('manager', 'subtract')
|
62 |
+
# run_simulated_request('manager', 'exit')
|
63 |
|
64 |
|
65 |
# Example of simplified object received from Turn.io stacks
|
|
|
67 |
# NOTE: This is actually a bstring, not a dict
|
68 |
simplified_json = {
|
69 |
"context": {
|
70 |
+
"user": "+57787919091",
|
71 |
+
"state": "answer-addition-problem",
|
72 |
+
"bot_message": "What is 2+2?",
|
73 |
+
"user_message": "eight",
|
74 |
"type": "ask"
|
75 |
+
},
|
76 |
"message_data": {
|
77 |
"author_id": "+57787919091",
|
78 |
"author_type": "OWNER",
|
|
|
86 |
}
|
87 |
|
88 |
|
|
|
89 |
# Full example of event data from Turn.io
|
90 |
# simplified_json is built from this in Turn.io
|
91 |
# full_json = {
|
|
|
144 |
# 'type': 'text'
|
145 |
# },
|
146 |
# 'type': 'message'
|
147 |
+
# }
|