Spaces:
Build error
Build error
Merge branch 'main' into vlad
Browse files
mathtext_fastapi/conversation_manager.py
CHANGED
@@ -106,6 +106,39 @@ def pickle_and_encode_state_machine(state_machine):
|
|
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,40 +159,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'
|
|
|
106 |
return dump_encoded
|
107 |
|
108 |
|
109 |
+
def manage_math_quiz_fsm(user_message, contact_uuid):
|
110 |
+
fsm_check = SUPA.table('state_machines').select("*").eq(
|
111 |
+
"contact_uuid",
|
112 |
+
contact_uuid
|
113 |
+
).execute()
|
114 |
+
|
115 |
+
if fsm_check.data == []:
|
116 |
+
math_quiz_state_machine = MathQuizFSM()
|
117 |
+
messages = [math_quiz_state_machine.response_text]
|
118 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
119 |
+
|
120 |
+
SUPA.table('state_machines').insert({
|
121 |
+
'contact_uuid': contact_uuid,
|
122 |
+
'addition3': dump_encoded
|
123 |
+
}).execute()
|
124 |
+
else:
|
125 |
+
undump_encoded = base64.b64decode(
|
126 |
+
fsm_check.data[0]['addition3'].encode('utf-8')
|
127 |
+
)
|
128 |
+
math_quiz_state_machine = pickle.loads(undump_encoded)
|
129 |
+
|
130 |
+
math_quiz_state_machine.student_answer = user_message
|
131 |
+
math_quiz_state_machine.correct_answer = str(math_quiz_state_machine.correct_answer)
|
132 |
+
messages = math_quiz_state_machine.validate_answer()
|
133 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
134 |
+
SUPA.table('state_machines').update({
|
135 |
+
'addition3': dump_encoded
|
136 |
+
}).eq(
|
137 |
+
"contact_uuid", contact_uuid
|
138 |
+
).execute()
|
139 |
+
return messages
|
140 |
+
|
141 |
+
|
142 |
def return_next_conversational_state(context_data, user_message, contact_uuid):
|
143 |
""" Evaluates the conversation's current state to determine the next state
|
144 |
|
|
|
159 |
elif context_data['state'] == 'addition-question-sequence' or \
|
160 |
user_message == 'add':
|
161 |
|
162 |
+
messages = manage_math_quiz_fsm(user_message, contact_uuid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
if user_message == 'exit':
|
165 |
state_label = 'exit'
|
mathtext_fastapi/math_quiz_fsm.py
CHANGED
@@ -16,21 +16,26 @@ class MathQuizFSM(object):
|
|
16 |
['exit', 'quiz_question', 'quiz_end'],
|
17 |
]
|
18 |
|
19 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
20 |
# Instantiate the FSM
|
21 |
self.machine = Machine(
|
22 |
-
model=self,
|
23 |
-
states=MathQuizFSM.states,
|
24 |
transitions=MathQuizFSM.transitions,
|
25 |
-
initial=
|
26 |
)
|
27 |
|
28 |
# Instantiate variables necessary for tracking activity
|
29 |
-
self.question_nums =
|
30 |
-
self.correct_answer =
|
31 |
-
self.student_answer =
|
32 |
self.is_correct_answer = False
|
33 |
-
self.response_text = "What is
|
34 |
|
35 |
# Define functions to run on transitions
|
36 |
self.machine.on_enter_quiz_question('generate_math_problem')
|
|
|
16 |
['exit', 'quiz_question', 'quiz_end'],
|
17 |
]
|
18 |
|
19 |
+
def __init__(
|
20 |
+
self,
|
21 |
+
initial_state='quiz_start',
|
22 |
+
question_nums=[2, 3],
|
23 |
+
initial_student_answer=0,
|
24 |
+
):
|
25 |
# Instantiate the FSM
|
26 |
self.machine = Machine(
|
27 |
+
model=self,
|
28 |
+
states=MathQuizFSM.states,
|
29 |
transitions=MathQuizFSM.transitions,
|
30 |
+
initial=initial_state
|
31 |
)
|
32 |
|
33 |
# Instantiate variables necessary for tracking activity
|
34 |
+
self.question_nums = question_nums
|
35 |
+
self.correct_answer = self.question_nums[0] + self.question_nums[1]
|
36 |
+
self.student_answer = initial_student_answer
|
37 |
self.is_correct_answer = False
|
38 |
+
self.response_text = f"What is {self.question_nums[0]} + {self.question_nums[1]}?"
|
39 |
|
40 |
# Define functions to run on transitions
|
41 |
self.machine.on_enter_quiz_question('generate_math_problem')
|