Spaces:
Runtime error
Runtime error
Greg Thompson
commited on
Commit
•
63facdc
1
Parent(s):
85a4243
Update conversation manager to modularize fsm call
Browse files
mathtext_fastapi/conversation_manager.py
CHANGED
@@ -105,6 +105,38 @@ def pickle_and_encode_state_machine(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
|
@@ -126,40 +158,7 @@ def return_next_conversational_state(context_data, user_message, contact_uuid):
|
|
126 |
elif context_data['state'] == 'addition-question-sequence' or \
|
127 |
user_message == 'add':
|
128 |
|
129 |
-
|
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 |
-
|
149 |
-
print("student answer")
|
150 |
-
print(math_quiz_state_machine.student_answer)
|
151 |
-
print("user_message")
|
152 |
-
print(user_message)
|
153 |
-
|
154 |
-
math_quiz_state_machine.student_answer = user_message
|
155 |
-
math_quiz_state_machine.correct_answer = str(math_quiz_state_machine.correct_answer)
|
156 |
-
messages = math_quiz_state_machine.validate_answer()
|
157 |
-
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
158 |
-
SUPA.table('state_machines').update({
|
159 |
-
'addition3': dump_encoded
|
160 |
-
}).eq(
|
161 |
-
"contact_uuid", contact_uuid
|
162 |
-
).execute()
|
163 |
|
164 |
if user_message == 'exit':
|
165 |
state_label = 'exit'
|
|
|
105 |
dump_encoded = base64.b64encode(dump).decode('utf-8')
|
106 |
return dump_encoded
|
107 |
|
108 |
+
def manage_math_quiz_fsm(user_message, contact_uuid):
|
109 |
+
fsm_check = SUPA.table('state_machines').select("*").eq(
|
110 |
+
"contact_uuid",
|
111 |
+
contact_uuid
|
112 |
+
).execute()
|
113 |
+
|
114 |
+
if fsm_check.data == []:
|
115 |
+
math_quiz_state_machine = MathQuizFSM()
|
116 |
+
messages = [math_quiz_state_machine.response_text]
|
117 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
118 |
+
|
119 |
+
SUPA.table('state_machines').insert({
|
120 |
+
'contact_uuid': contact_uuid,
|
121 |
+
'addition3': dump_encoded
|
122 |
+
}).execute()
|
123 |
+
else:
|
124 |
+
undump_encoded = base64.b64decode(
|
125 |
+
fsm_check.data[0]['addition3'].encode('utf-8')
|
126 |
+
)
|
127 |
+
math_quiz_state_machine = pickle.loads(undump_encoded)
|
128 |
+
|
129 |
+
math_quiz_state_machine.student_answer = user_message
|
130 |
+
math_quiz_state_machine.correct_answer = str(math_quiz_state_machine.correct_answer)
|
131 |
+
messages = math_quiz_state_machine.validate_answer()
|
132 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
133 |
+
SUPA.table('state_machines').update({
|
134 |
+
'addition3': dump_encoded
|
135 |
+
}).eq(
|
136 |
+
"contact_uuid", contact_uuid
|
137 |
+
).execute()
|
138 |
+
return messages
|
139 |
+
|
140 |
|
141 |
def return_next_conversational_state(context_data, user_message, contact_uuid):
|
142 |
""" Evaluates the conversation's current state to determine the next state
|
|
|
158 |
elif context_data['state'] == 'addition-question-sequence' or \
|
159 |
user_message == 'add':
|
160 |
|
161 |
+
messages = manage_math_quiz_fsm(user_message, contact_uuid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
if user_message == 'exit':
|
164 |
state_label = 'exit'
|