Greg Thompson commited on
Commit
2be96c5
1 Parent(s): d602f3c

Fix bugs and get pickled object to save to database

Browse files
mathtext_fastapi/conversation_manager.py CHANGED
@@ -1,6 +1,9 @@
1
- import dill as pickle
 
2
  import os
3
  import json
 
 
4
  import random
5
  import requests
6
 
@@ -97,6 +100,12 @@ def create_interactive_message(message_text, button_options, whatsapp_id):
97
  return data
98
 
99
 
 
 
 
 
 
 
100
  def return_next_conversational_state(context_data, user_message, contact_uuid):
101
  """ Evaluates the conversation's current state to determine the next state
102
 
@@ -117,23 +126,30 @@ def return_next_conversational_state(context_data, user_message, contact_uuid):
117
  elif user_message == 'add':
118
 
119
  fsm_check = SUPA.table('state_machines').select("*").eq(
120
- "uuid",
121
  contact_uuid
122
  ).execute()
123
 
124
  if fsm_check.data == []:
125
  math_quiz_state_machine = MathQuizFSM()
126
  messages = [math_quiz_state_machine.response_text]
127
- dump = pickle.dumps(math_quiz_state_machine)
128
 
129
- # TODO: Check how to save - JSONB?
130
- SUPA.table('state_machines').insert(dump).execute()
 
 
131
  else:
132
- math_quiz_state_machine = pickle.loads(fsm_check.data['add'])
133
- math_quiz_state_machine.student_answer
134
- messages = math_quiz_state_machine.validate()
135
- dump = pickle.dumps(math_quiz_state_machine)
136
- SUPA.table('state_machines').update(dump).eq(
 
 
 
 
 
137
  "contact_uuid", contact_uuid
138
  ).execute()
139
 
@@ -202,6 +218,7 @@ def manage_conversation_response(data_json):
202
  contact_uuid
203
  )
204
 
 
205
  headers = {
206
  'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
207
  '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
 
 
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
 
 
126
  elif user_message == 'add':
127
 
128
  fsm_check = SUPA.table('state_machines').select("*").eq(
129
+ "contact_uuid",
130
  contact_uuid
131
  ).execute()
132
 
133
  if fsm_check.data == []:
134
  math_quiz_state_machine = MathQuizFSM()
135
  messages = [math_quiz_state_machine.response_text]
136
+ dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
137
 
138
+ SUPA.table('state_machines').insert({
139
+ 'contact_uuid': contact_uuid,
140
+ 'addition3': dump_encoded
141
+ }).execute()
142
  else:
143
+ undump_encoded = base64.b64decode(
144
+ fsm_check.data[0]['addition3'].encode('utf-8')
145
+ )
146
+ math_quiz_state_machine = pickle.loads(undump_encoded)
147
+ math_quiz_state_machine.student_answer == user_message
148
+ messages = math_quiz_state_machine.validate_answer()
149
+ dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
150
+ SUPA.table('state_machines').update({
151
+ 'addition3': dump_encoded
152
+ }).eq(
153
  "contact_uuid", contact_uuid
154
  ).execute()
155
 
 
218
  contact_uuid
219
  )
220
 
221
+
222
  headers = {
223
  'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
224
  'Content-Type': 'application/json'
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  dill
 
2
  mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
3
  fastapi==0.74.*
4
  pydantic==1.10.*
 
1
  dill
2
+ jsonpickle
3
  mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
4
  fastapi==0.74.*
5
  pydantic==1.10.*
scripts/make_request.py CHANGED
@@ -46,20 +46,20 @@ def run_simulated_request(endpoint, sample_answer, context=None):
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
@@ -72,7 +72,7 @@ simplified_json = {
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",
 
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
 
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",