Spaces:
Build error
Build error
Greg Thompson
commited on
Commit
•
fbc5903
1
Parent(s):
8acf519
Fix spacing and name error
Browse files- app.py +7 -7
- mathtext_fastapi/conversation_manager.py +19 -19
- mathtext_fastapi/logging.py +10 -11
- mathtext_fastapi/nlu.py +8 -10
app.py
CHANGED
@@ -72,25 +72,25 @@ async def programmatic_message_manager(request: Request):
|
|
72 |
Output
|
73 |
context: dict - the information for the current state
|
74 |
{
|
75 |
-
"user": "47897891",
|
76 |
-
"state": "welcome-message-state",
|
77 |
-
"bot_message": "Welcome to Rori!",
|
78 |
-
"user_message": "",
|
79 |
"type": "ask"
|
80 |
}
|
81 |
"""
|
82 |
data_dict = await request.json()
|
83 |
-
context =
|
84 |
return JSONResponse(context)
|
85 |
|
86 |
|
87 |
@app.post("/nlu")
|
88 |
async def evaluate_user_message_with_nlu_api(request: Request):
|
89 |
""" Calls nlu evaluation and returns the nlu_response
|
90 |
-
|
91 |
Input
|
92 |
- request.body: json - message data for the most recent user response
|
93 |
-
|
94 |
Output
|
95 |
- int_data_dict or sent_data_dict: dict - the type of NLU run and result
|
96 |
{'type':'integer', 'data': '8'}
|
|
|
72 |
Output
|
73 |
context: dict - the information for the current state
|
74 |
{
|
75 |
+
"user": "47897891",
|
76 |
+
"state": "welcome-message-state",
|
77 |
+
"bot_message": "Welcome to Rori!",
|
78 |
+
"user_message": "",
|
79 |
"type": "ask"
|
80 |
}
|
81 |
"""
|
82 |
data_dict = await request.json()
|
83 |
+
context = manage_conversation_response(data_dict)
|
84 |
return JSONResponse(context)
|
85 |
|
86 |
|
87 |
@app.post("/nlu")
|
88 |
async def evaluate_user_message_with_nlu_api(request: Request):
|
89 |
""" Calls nlu evaluation and returns the nlu_response
|
90 |
+
|
91 |
Input
|
92 |
- request.body: json - message data for the most recent user response
|
93 |
+
|
94 |
Output
|
95 |
- int_data_dict or sent_data_dict: dict - the type of NLU run and result
|
96 |
{'type':'integer', 'data': '8'}
|
mathtext_fastapi/conversation_manager.py
CHANGED
@@ -12,7 +12,7 @@ load_dotenv()
|
|
12 |
|
13 |
def create_text_message(message_text, whatsapp_id):
|
14 |
""" Fills a template with input values to send a text message to Whatsapp
|
15 |
-
|
16 |
Inputs
|
17 |
- message_text: str - the content that the message should display
|
18 |
- whatsapp_id: str - the message recipient's phone number
|
@@ -28,7 +28,7 @@ def create_text_message(message_text, whatsapp_id):
|
|
28 |
"text": {
|
29 |
"body": message_text
|
30 |
}
|
31 |
-
}
|
32 |
return message_data
|
33 |
|
34 |
|
@@ -48,7 +48,7 @@ def create_button_objects(button_options):
|
|
48 |
"type": "reply",
|
49 |
"reply": {
|
50 |
"id": "inquiry-yes",
|
51 |
-
"title": option['text']
|
52 |
}
|
53 |
}
|
54 |
button_arr.append(button_choice)
|
@@ -59,9 +59,9 @@ def create_interactive_message(message_text, button_options, whatsapp_id):
|
|
59 |
""" Fills a template to create a button message for Whatsapp
|
60 |
|
61 |
* NOTE: Not fully implemented and tested
|
62 |
-
* NOTE/TODO: It is possible to create other kinds of messages
|
63 |
with the 'interactive message' template
|
64 |
-
* Documentation:
|
65 |
https://whatsapp.turn.io/docs/api/messages#interactive-messages
|
66 |
|
67 |
Inputs
|
@@ -124,7 +124,7 @@ def return_next_conversational_state(context_data, user_message):
|
|
124 |
],
|
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': [
|
@@ -132,7 +132,7 @@ def return_next_conversational_state(context_data, user_message):
|
|
132 |
],
|
133 |
'input_prompt': "",
|
134 |
'state': "exit"
|
135 |
-
}
|
136 |
else:
|
137 |
message_package = {
|
138 |
'messages': [
|
@@ -162,15 +162,15 @@ def manage_conversation_response(data_json):
|
|
162 |
"""
|
163 |
message_data = data_json.get('message_data', '')
|
164 |
context_data = data_json.get('context_data', '')
|
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 |
|
@@ -183,18 +183,18 @@ def manage_conversation_response(data_json):
|
|
183 |
for message in message_package['messages']:
|
184 |
data = create_text_message(message, whatsapp_id)
|
185 |
r = requests.post(
|
186 |
-
f'https://whatsapp.turn.io/v1/messages',
|
187 |
-
data=json.dumps(data),
|
188 |
headers=headers
|
189 |
)
|
190 |
|
191 |
# Update the context object with the new state of the conversation
|
192 |
context = {
|
193 |
"context":{
|
194 |
-
"user": whatsapp_id,
|
195 |
-
"state": message_package['state'],
|
196 |
-
"bot_message": message_package['input_prompt'],
|
197 |
-
"user_message": user_message,
|
198 |
"type": 'ask'
|
199 |
}
|
200 |
}
|
@@ -217,17 +217,17 @@ def manage_conversation_response(data_json):
|
|
217 |
# "type": "reply",
|
218 |
# "reply": {
|
219 |
# "id": "inquiry-yes",
|
220 |
-
# "title": "Yes"
|
221 |
# }
|
222 |
# },
|
223 |
# {
|
224 |
# "type": "reply",
|
225 |
# "reply": {
|
226 |
# "id": "inquiry-no",
|
227 |
-
# "title": "No"
|
228 |
# }
|
229 |
# }
|
230 |
# ]
|
231 |
# }
|
232 |
# }
|
233 |
-
# }
|
|
|
12 |
|
13 |
def create_text_message(message_text, whatsapp_id):
|
14 |
""" Fills a template with input values to send a text message to Whatsapp
|
15 |
+
|
16 |
Inputs
|
17 |
- message_text: str - the content that the message should display
|
18 |
- whatsapp_id: str - the message recipient's phone number
|
|
|
28 |
"text": {
|
29 |
"body": message_text
|
30 |
}
|
31 |
+
}
|
32 |
return message_data
|
33 |
|
34 |
|
|
|
48 |
"type": "reply",
|
49 |
"reply": {
|
50 |
"id": "inquiry-yes",
|
51 |
+
"title": option['text']
|
52 |
}
|
53 |
}
|
54 |
button_arr.append(button_choice)
|
|
|
59 |
""" Fills a template to create a button message for Whatsapp
|
60 |
|
61 |
* NOTE: Not fully implemented and tested
|
62 |
+
* NOTE/TODO: It is possible to create other kinds of messages
|
63 |
with the 'interactive message' template
|
64 |
+
* Documentation:
|
65 |
https://whatsapp.turn.io/docs/api/messages#interactive-messages
|
66 |
|
67 |
Inputs
|
|
|
124 |
],
|
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': [
|
|
|
132 |
],
|
133 |
'input_prompt': "",
|
134 |
'state': "exit"
|
135 |
+
}
|
136 |
else:
|
137 |
message_package = {
|
138 |
'messages': [
|
|
|
162 |
"""
|
163 |
message_data = data_json.get('message_data', '')
|
164 |
context_data = data_json.get('context_data', '')
|
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 |
|
|
|
183 |
for message in message_package['messages']:
|
184 |
data = create_text_message(message, whatsapp_id)
|
185 |
r = requests.post(
|
186 |
+
f'https://whatsapp.turn.io/v1/messages',
|
187 |
+
data=json.dumps(data),
|
188 |
headers=headers
|
189 |
)
|
190 |
|
191 |
# Update the context object with the new state of the conversation
|
192 |
context = {
|
193 |
"context":{
|
194 |
+
"user": whatsapp_id,
|
195 |
+
"state": message_package['state'],
|
196 |
+
"bot_message": message_package['input_prompt'],
|
197 |
+
"user_message": user_message,
|
198 |
"type": 'ask'
|
199 |
}
|
200 |
}
|
|
|
217 |
# "type": "reply",
|
218 |
# "reply": {
|
219 |
# "id": "inquiry-yes",
|
220 |
+
# "title": "Yes"
|
221 |
# }
|
222 |
# },
|
223 |
# {
|
224 |
# "type": "reply",
|
225 |
# "reply": {
|
226 |
# "id": "inquiry-no",
|
227 |
+
# "title": "No"
|
228 |
# }
|
229 |
# }
|
230 |
# ]
|
231 |
# }
|
232 |
# }
|
233 |
+
# }
|
mathtext_fastapi/logging.py
CHANGED
@@ -7,7 +7,7 @@ from supabase import create_client
|
|
7 |
load_dotenv()
|
8 |
|
9 |
SUPA = create_client(
|
10 |
-
os.environ.get('SUPABASE_URL'),
|
11 |
os.environ.get('SUPABASE_KEY')
|
12 |
)
|
13 |
|
@@ -30,25 +30,24 @@ def get_or_create_supabase_entry(table_name, insert_data, check_variable=None):
|
|
30 |
|
31 |
Result
|
32 |
- logged_data - an object with the Supabase data
|
33 |
-
|
34 |
"""
|
35 |
if table_name == 'contact':
|
36 |
resp = SUPA.table('contact').select("*").eq(
|
37 |
-
"original_contact_id",
|
38 |
insert_data['original_contact_id']
|
39 |
).eq(
|
40 |
-
"project",
|
41 |
insert_data['project']
|
42 |
).execute()
|
43 |
else:
|
44 |
resp = SUPA.table(table_name).select("*").eq(
|
45 |
-
check_variable,
|
46 |
insert_data[check_variable]
|
47 |
).execute()
|
48 |
|
49 |
if len(resp.data) == 0:
|
50 |
logged_data = log_message_data_through_supabase_api(
|
51 |
-
table_name,
|
52 |
insert_data
|
53 |
)
|
54 |
else:
|
@@ -64,11 +63,11 @@ def prepare_message_data_for_logging(message_data, nlu_response):
|
|
64 |
"""
|
65 |
project_data = {
|
66 |
'name': "Rori",
|
67 |
-
# Autogenerated fields: id, created_at, modified_at
|
68 |
}
|
69 |
project_data_log = get_or_create_supabase_entry(
|
70 |
-
'project',
|
71 |
-
project_data,
|
72 |
'name'
|
73 |
)
|
74 |
|
@@ -99,6 +98,6 @@ def prepare_message_data_for_logging(message_data, nlu_response):
|
|
99 |
# Autogenerated fields: created_at, modified_at
|
100 |
}
|
101 |
message_data_log = log_message_data_through_supabase_api(
|
102 |
-
'message',
|
103 |
message_data
|
104 |
-
)
|
|
|
7 |
load_dotenv()
|
8 |
|
9 |
SUPA = create_client(
|
10 |
+
os.environ.get('SUPABASE_URL'),
|
11 |
os.environ.get('SUPABASE_KEY')
|
12 |
)
|
13 |
|
|
|
30 |
|
31 |
Result
|
32 |
- logged_data - an object with the Supabase data
|
|
|
33 |
"""
|
34 |
if table_name == 'contact':
|
35 |
resp = SUPA.table('contact').select("*").eq(
|
36 |
+
"original_contact_id",
|
37 |
insert_data['original_contact_id']
|
38 |
).eq(
|
39 |
+
"project",
|
40 |
insert_data['project']
|
41 |
).execute()
|
42 |
else:
|
43 |
resp = SUPA.table(table_name).select("*").eq(
|
44 |
+
check_variable,
|
45 |
insert_data[check_variable]
|
46 |
).execute()
|
47 |
|
48 |
if len(resp.data) == 0:
|
49 |
logged_data = log_message_data_through_supabase_api(
|
50 |
+
table_name,
|
51 |
insert_data
|
52 |
)
|
53 |
else:
|
|
|
63 |
"""
|
64 |
project_data = {
|
65 |
'name': "Rori",
|
66 |
+
# Autogenerated fields: id, created_at, modified_at
|
67 |
}
|
68 |
project_data_log = get_or_create_supabase_entry(
|
69 |
+
'project',
|
70 |
+
project_data,
|
71 |
'name'
|
72 |
)
|
73 |
|
|
|
98 |
# Autogenerated fields: created_at, modified_at
|
99 |
}
|
100 |
message_data_log = log_message_data_through_supabase_api(
|
101 |
+
'message',
|
102 |
message_data
|
103 |
+
)
|
mathtext_fastapi/nlu.py
CHANGED
@@ -26,8 +26,8 @@ def test_for_number_sequence(message_text_arr, message_data, message_text):
|
|
26 |
nlu_response = {}
|
27 |
if all(ele.isdigit() for ele in message_text_arr):
|
28 |
nlu_response = build_nlu_response_object(
|
29 |
-
'integer',
|
30 |
-
','.join(message_text_arr),
|
31 |
''
|
32 |
)
|
33 |
prepare_message_data_for_logging(message_data, nlu_response)
|
@@ -79,25 +79,23 @@ def evaluate_message_with_nlu(message_data):
|
|
79 |
if 32202 in student_response_arr:
|
80 |
sentiment_api_resp = sentiment(message_text)
|
81 |
nlu_response = build_nlu_response_object(
|
82 |
-
'sentiment',
|
83 |
-
sentiment_api_resp[0]['label'],
|
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
|
102 |
-
|
103 |
-
|
|
|
26 |
nlu_response = {}
|
27 |
if all(ele.isdigit() for ele in message_text_arr):
|
28 |
nlu_response = build_nlu_response_object(
|
29 |
+
'integer',
|
30 |
+
','.join(message_text_arr),
|
31 |
''
|
32 |
)
|
33 |
prepare_message_data_for_logging(message_data, nlu_response)
|
|
|
79 |
if 32202 in student_response_arr:
|
80 |
sentiment_api_resp = sentiment(message_text)
|
81 |
nlu_response = build_nlu_response_object(
|
82 |
+
'sentiment',
|
83 |
+
sentiment_api_resp[0]['label'],
|
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
|
|
|
|