sviddo commited on
Commit
ad233e8
2 Parent(s): f2284bd 8df7dc6

Merge branch 'main' into 'vlad'

Browse files
mathtext_fastapi/conversation_manager.py CHANGED
@@ -116,6 +116,25 @@ def manage_math_quiz_fsm(user_message, contact_uuid, type):
116
  contact_uuid
117
  ).execute()
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  if fsm_check.data == []:
120
  if type == 'addition':
121
  math_quiz_state_machine = MathQuizFSM()
@@ -128,7 +147,22 @@ def manage_math_quiz_fsm(user_message, contact_uuid, type):
128
  'contact_uuid': contact_uuid,
129
  f'{type}': dump_encoded
130
  }).execute()
131
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  undump_encoded = base64.b64decode(
133
  fsm_check.data[0][type].encode('utf-8')
134
  )
@@ -217,24 +251,24 @@ def return_next_conversational_state(context_data, user_message, contact_uuid):
217
  # Used in FSM
218
  # messages = manage_math_quiz_fsm(user_message, contact_uuid)
219
 
220
- message_package, context_result = use_quiz_module_approach(user_message, context_data)
 
221
 
222
  if user_message == 'exit':
223
  state_label = 'exit'
224
  else:
225
  state_label = 'addition-question-sequence'
226
  # Used in FSM
227
- # input_prompt = messages.pop()
228
- # message_package = {
229
- # 'messages': messages,
230
- # 'input_prompt': input_prompt,
231
- # 'state': state_label
232
- # }
233
 
234
- print("MESSAGE PACKAGE")
235
- print(message_package)
236
- context_data = context_result
237
- message_package['state'] = state_label
238
 
239
  elif context_data['state'] == 'subtraction-question-sequence' or \
240
  user_message == 'subtract':
@@ -323,9 +357,6 @@ def manage_conversation_response(data_json):
323
  contact_uuid
324
  )
325
 
326
- print("MESSAGE PACKAGE")
327
- print(message_package)
328
-
329
  headers = {
330
  'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
331
  'Content-Type': 'application/json'
 
116
  contact_uuid
117
  ).execute()
118
 
119
+ # This doesn't allow for when one FSM is present and the other is empty
120
+ """
121
+ 1
122
+ data=[] count=None
123
+
124
+ 2
125
+ data=[{'id': 29, 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09', 'addition3': None, 'subtraction': None, 'addition':
126
+
127
+ - but problem is there is no subtraction , but it's assuming there's a subtration
128
+
129
+
130
+ Cases
131
+ - make a completely new record
132
+ - update an existing record with an existing FSM
133
+ - update an existing record without an existing FSM
134
+ """
135
+
136
+
137
+ # Make a completely new entry
138
  if fsm_check.data == []:
139
  if type == 'addition':
140
  math_quiz_state_machine = MathQuizFSM()
 
147
  'contact_uuid': contact_uuid,
148
  f'{type}': dump_encoded
149
  }).execute()
150
+ # Update an existing record with a new state machine
151
+ elif not fsm_check.data[0][type]:
152
+ if type == 'addition':
153
+ math_quiz_state_machine = MathQuizFSM()
154
+ else:
155
+ math_quiz_state_machine = MathSubtractionFSM()
156
+ messages = [math_quiz_state_machine.response_text]
157
+ dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
158
+
159
+ SUPA.table('state_machines').update({
160
+ f'{type}': dump_encoded
161
+ }).eq(
162
+ "contact_uuid", contact_uuid
163
+ ).execute()
164
+ # Update an existing record with an existing state machine
165
+ elif fsm_check.data[0][type]:
166
  undump_encoded = base64.b64decode(
167
  fsm_check.data[0][type].encode('utf-8')
168
  )
 
251
  # Used in FSM
252
  # messages = manage_math_quiz_fsm(user_message, contact_uuid)
253
 
254
+ # message_package, context_result = use_quiz_module_approach(user_message, context_data)
255
+ messages = manage_math_quiz_fsm(user_message, contact_uuid, 'addition')
256
 
257
  if user_message == 'exit':
258
  state_label = 'exit'
259
  else:
260
  state_label = 'addition-question-sequence'
261
  # Used in FSM
262
+ input_prompt = messages.pop()
263
+ message_package = {
264
+ 'messages': messages,
265
+ 'input_prompt': input_prompt,
266
+ 'state': state_label
267
+ }
268
 
269
+ # Used in quiz w/ hints
270
+ # context_data = context_result
271
+ # message_package['state'] = state_label
 
272
 
273
  elif context_data['state'] == 'subtraction-question-sequence' or \
274
  user_message == 'subtract':
 
357
  contact_uuid
358
  )
359
 
 
 
 
360
  headers = {
361
  'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
362
  'Content-Type': 'application/json'
mathtext_fastapi/nlu.py CHANGED
@@ -57,26 +57,25 @@ def run_sentiment_analysis(message_text):
57
 
58
 
59
  def evaluate_message_with_nlu(message_data):
60
- message_text = message_data['message_body']
61
- message_text_arr = re.split(", |,| ", message_text.strip())
62
-
63
- # TODO: Replace this with appropriate utility function (is_int, is_float, render_int_or_float)
64
- nlu_response = test_for_float_or_int(message_data, message_text)
65
- if len(nlu_response) > 0:
66
- return nlu_response
67
-
68
- # TODO: Replace this with appropriate utility function
69
- nlu_response = test_for_number_sequence(message_text_arr, message_data, message_text)
70
- if len(nlu_response) > 0:
71
- return nlu_response
72
-
73
- student_response_arr = run_text2int_on_each_list_item(message_text_arr)
74
-
75
- # '32202' is text2int's error code for non-integer student answers (ie., "I don't know")
76
- # If any part of the list is 32202, sentiment analysis will run
77
- # TODO: Need to replace this with logic that recognizes multiple intents (Maybe 36 = "sentiment analysis" & "integer")
78
- student_response_arr = run_text2int_on_each_list_item(message_text_arr)
79
- if 32202 in student_response_arr:
80
  sentiment_api_resp = sentiment(message_text)
81
  nlu_response = build_nlu_response_object(
82
  'sentiment',
@@ -84,18 +83,11 @@ def evaluate_message_with_nlu(message_data):
84
  sentiment_api_resp[0]['score']
85
  )
86
  else:
87
- if len(student_response_arr) > 1:
88
- nlu_response = build_nlu_response_object(
89
- 'integer',
90
- ','.join(str(num) for num in student_response_arr),
91
- ''
92
- )
93
- else:
94
- nlu_response = build_nlu_response_object(
95
- 'integer',
96
- student_response_arr[0],
97
- ''
98
- )
99
 
100
  prepare_message_data_for_logging(message_data, nlu_response)
101
  return nlu_response
 
57
 
58
 
59
  def evaluate_message_with_nlu(message_data):
60
+ # Keeps system working with two different inputs - full and filtered @event object
61
+ try:
62
+ message_text = message_data['message_body']
63
+ except KeyError:
64
+ message_data = {
65
+ 'author_id': message_data['message']['_vnd']['v1']['chat']['owner'],
66
+ 'author_type': message_data['message']['_vnd']['v1']['author']['type'],
67
+ 'contact_uuid': message_data['message']['_vnd']['v1']['chat']['contact_uuid'],
68
+ 'message_body': message_data['message']['text']['body'],
69
+ 'message_direction': message_data['message']['_vnd']['v1']['direction'],
70
+ 'message_id': message_data['message']['id'],
71
+ 'message_inserted_at': message_data['message']['_vnd']['v1']['chat']['inserted_at'],
72
+ 'message_updated_at': message_data['message']['_vnd']['v1']['chat']['updated_at'],
73
+ }
74
+ message_text = message_data['message_body']
75
+
76
+ number_api_resp = text2int(message_text.lower())
77
+
78
+ if number_api_resp == 32202:
 
79
  sentiment_api_resp = sentiment(message_text)
80
  nlu_response = build_nlu_response_object(
81
  'sentiment',
 
83
  sentiment_api_resp[0]['score']
84
  )
85
  else:
86
+ nlu_response = build_nlu_response_object(
87
+ 'integer',
88
+ number_api_resp,
89
+ ''
90
+ )
 
 
 
 
 
 
 
91
 
92
  prepare_message_data_for_logging(message_data, nlu_response)
93
  return nlu_response
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  dill
 
2
  jsonpickle
3
  mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
4
  fastapi==0.74.*
 
1
  dill
2
+ en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.4.1/en_core_web_sm-3.4.1-py3-none-any.whl
3
  jsonpickle
4
  mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
5
  fastapi==0.74.*
scripts/make_request.py CHANGED
@@ -60,14 +60,19 @@ def run_simulated_request(endpoint, sample_answer, context=None):
60
 
61
  # run_simulated_request('sentiment-analysis', 'I reject it')
62
  # run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')
63
- # run_simulated_request('nlu', 'test message')
64
- # run_simulated_request('nlu', 'eight')
65
- # run_simulated_request('nlu', 'eight, nine, ten')
66
- # run_simulated_request('nlu', '8, 9, 10')
67
- # run_simulated_request('nlu', '8')
68
- # run_simulated_request('nlu', "I don't know")
69
- # run_simulated_request('nlu', 'Today is a wonderful day')
70
- # run_simulated_request('nlu', 'IDK 5?')
 
 
 
 
 
71
  # run_simulated_request('manager', '')
72
  # run_simulated_request('manager', 'add')
73
  # run_simulated_request('manager', 'subtract')
 
60
 
61
  # run_simulated_request('sentiment-analysis', 'I reject it')
62
  # run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')
63
+ run_simulated_request('nlu', 'test message')
64
+ run_simulated_request('nlu', 'eight')
65
+ run_simulated_request('nlu', 'is it 8')
66
+ run_simulated_request('nlu', 'can I know how its 0.5')
67
+ run_simulated_request('nlu', 'eight, nine, ten')
68
+ run_simulated_request('nlu', '8, 9, 10')
69
+ run_simulated_request('nlu', '8')
70
+ run_simulated_request('nlu', "I don't know")
71
+ run_simulated_request('nlu', "I don't know eight")
72
+ run_simulated_request('nlu', "I don't 9")
73
+ run_simulated_request('nlu', "0.2")
74
+ run_simulated_request('nlu', 'Today is a wonderful day')
75
+ run_simulated_request('nlu', 'IDK 5?')
76
  # run_simulated_request('manager', '')
77
  # run_simulated_request('manager', 'add')
78
  # run_simulated_request('manager', 'subtract')