Spaces:
Runtime error
Runtime error
Greg Thompson
commited on
Commit
·
3260908
1
Parent(s):
b50e39f
Update nlu endpoint with payload validity check
Browse files- mathtext_fastapi/nlu.py +36 -0
mathtext_fastapi/nlu.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
|
|
| 1 |
from logging import getLogger
|
|
|
|
| 2 |
import re
|
| 3 |
|
| 4 |
from fuzzywuzzy import fuzz
|
|
@@ -137,6 +139,35 @@ def run_intent_classification(message_text):
|
|
| 137 |
return nlu_response
|
| 138 |
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
def evaluate_message_with_nlu(message_data):
|
| 141 |
""" Process a student's message using NLU functions and send the result
|
| 142 |
|
|
@@ -147,7 +178,12 @@ def evaluate_message_with_nlu(message_data):
|
|
| 147 |
{'type': 'sentiment', 'data': 'NEGATIVE', 'confidence': 0.9997807145118713}
|
| 148 |
"""
|
| 149 |
# Keeps system working with two different inputs - full and filtered @event object
|
|
|
|
| 150 |
log.info(f'Starting evaluate message: {message_data}')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
try:
|
| 152 |
message_text = str(message_data.get('message_body', ''))
|
| 153 |
except:
|
|
|
|
| 1 |
+
from collections.abc import Mapping
|
| 2 |
from logging import getLogger
|
| 3 |
+
import datetime as dt
|
| 4 |
import re
|
| 5 |
|
| 6 |
from fuzzywuzzy import fuzz
|
|
|
|
| 139 |
return nlu_response
|
| 140 |
|
| 141 |
|
| 142 |
+
def payload_is_valid(payload_object):
|
| 143 |
+
"""
|
| 144 |
+
>>> payload_is_valid({'author_id': '+821031323138', 'author_type': 'OWNER', 'contact_uuid': '49d42557-7a64-42fc-98fb-794061f37cf9', 'message_body': 'thirty one', 'message_direction': 'inbound', 'message_id': 'ABEGghAxMjE4Ags-sGJ_JArMAnJBnA', 'message_inserted_at': '2022-07-05T04:00:34.03352Z', 'message_updated_at': '2023-04-06T10:08:23.745072Z'})
|
| 145 |
+
True
|
| 146 |
+
"""
|
| 147 |
+
return (
|
| 148 |
+
isinstance(payload_object, Mapping) and
|
| 149 |
+
isinstance(payload_object.get('author_id'), str) and
|
| 150 |
+
isinstance(payload_object.get('author_type'), str) and
|
| 151 |
+
isinstance(payload_object.get('contact_uuid'), str) and
|
| 152 |
+
isinstance(payload_object.get('message_body'), str) and
|
| 153 |
+
isinstance(payload_object.get('message_direction'), str) and
|
| 154 |
+
isinstance(payload_object.get('inbound'), str) and
|
| 155 |
+
isinstance(payload_object.get('message_id'), str) and
|
| 156 |
+
isinstance(payload_object.get('message_inserted_at'), str) and
|
| 157 |
+
isinstance(payload_object.get('message_updated_at'), str) and
|
| 158 |
+
isinstance(payload_object.get('message_inserted_at'), str) and
|
| 159 |
+
isinstance(payload_object.get('message_updated_at'), str) and
|
| 160 |
+
isinstance(
|
| 161 |
+
dt.datetime.fromisoformat(payload_object.get('message_inserted_at')),
|
| 162 |
+
dt.datetime
|
| 163 |
+
) and
|
| 164 |
+
isinstance(
|
| 165 |
+
dt.datetime.fromisoformat(payload_object.get('message_updated_at')),
|
| 166 |
+
dt.datetime
|
| 167 |
+
)
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
def evaluate_message_with_nlu(message_data):
|
| 172 |
""" Process a student's message using NLU functions and send the result
|
| 173 |
|
|
|
|
| 178 |
{'type': 'sentiment', 'data': 'NEGATIVE', 'confidence': 0.9997807145118713}
|
| 179 |
"""
|
| 180 |
# Keeps system working with two different inputs - full and filtered @event object
|
| 181 |
+
# Call validate payload
|
| 182 |
log.info(f'Starting evaluate message: {message_data}')
|
| 183 |
+
|
| 184 |
+
if not payload_is_valid(message_data):
|
| 185 |
+
return {'type': 'error', 'data': ERROR_CODE, 'confidence': 0}
|
| 186 |
+
|
| 187 |
try:
|
| 188 |
message_text = str(message_data.get('message_body', ''))
|
| 189 |
except:
|