Spaces:
Runtime error
Runtime error
Merge branch 'production' of https://huggingface.co/spaces/TangibleAI/mathtext-fastapi into production
Browse files- mathtext_fastapi/nlu.py +20 -9
- scripts/make_request.py +4 -1
mathtext_fastapi/nlu.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
|
|
|
|
|
| 1 |
from collections.abc import Mapping
|
| 2 |
from logging import getLogger
|
| 3 |
import datetime as dt
|
| 4 |
from dateutil.parser import isoparse
|
| 5 |
|
| 6 |
from fuzzywuzzy import fuzz
|
|
|
|
| 7 |
from mathtext_fastapi.intent_classification import predict_message_intent
|
| 8 |
from mathtext_fastapi.logging import prepare_message_data_for_logging
|
| 9 |
from mathtext.sentiment import sentiment
|
|
@@ -121,7 +124,7 @@ def run_intent_classification(message_text):
|
|
| 121 |
label = ''
|
| 122 |
ratio = 0
|
| 123 |
nlu_response = {'type': 'intent', 'data': label, 'confidence': ratio}
|
| 124 |
-
|
| 125 |
'easier',
|
| 126 |
'exit',
|
| 127 |
'harder',
|
|
@@ -137,17 +140,25 @@ def run_intent_classification(message_text):
|
|
| 137 |
'question',
|
| 138 |
'easier',
|
| 139 |
'easy',
|
| 140 |
-
'support'
|
|
|
|
|
|
|
| 141 |
]
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
try:
|
| 145 |
-
|
| 146 |
except:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
nlu_response['
|
|
|
|
| 151 |
|
| 152 |
return nlu_response
|
| 153 |
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
from collections.abc import Mapping
|
| 4 |
from logging import getLogger
|
| 5 |
import datetime as dt
|
| 6 |
from dateutil.parser import isoparse
|
| 7 |
|
| 8 |
from fuzzywuzzy import fuzz
|
| 9 |
+
from fuzzywuzzy import process
|
| 10 |
from mathtext_fastapi.intent_classification import predict_message_intent
|
| 11 |
from mathtext_fastapi.logging import prepare_message_data_for_logging
|
| 12 |
from mathtext.sentiment import sentiment
|
|
|
|
| 124 |
label = ''
|
| 125 |
ratio = 0
|
| 126 |
nlu_response = {'type': 'intent', 'data': label, 'confidence': ratio}
|
| 127 |
+
keywords = [
|
| 128 |
'easier',
|
| 129 |
'exit',
|
| 130 |
'harder',
|
|
|
|
| 140 |
'question',
|
| 141 |
'easier',
|
| 142 |
'easy',
|
| 143 |
+
'support',
|
| 144 |
+
'skip',
|
| 145 |
+
'menu'
|
| 146 |
]
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
tokens = re.findall(r"[-a-zA-Z'_]+", message_text.lower())
|
| 150 |
+
except AttributeError:
|
| 151 |
+
tokens = ''
|
| 152 |
+
|
| 153 |
+
for keyword in keywords:
|
| 154 |
try:
|
| 155 |
+
tok, score = process.extractOne(keyword, tokens, scorer=fuzz.ratio)
|
| 156 |
except:
|
| 157 |
+
score = 0
|
| 158 |
+
|
| 159 |
+
if score > 80:
|
| 160 |
+
nlu_response['data'] = keyword
|
| 161 |
+
nlu_response['confidence'] = score
|
| 162 |
|
| 163 |
return nlu_response
|
| 164 |
|
scripts/make_request.py
CHANGED
|
@@ -77,11 +77,14 @@ run_full_nlu_endpoint_payload_test(b'{"message": {"author_id": "@event.message._
|
|
| 77 |
# # Case: Correct payload
|
| 78 |
run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}')
|
| 79 |
|
|
|
|
|
|
|
|
|
|
| 80 |
# # Case: Incorrect payload values
|
| 81 |
run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "@event.message._vnd.v1.chat.owner", "author_type": "@event.message._vnd.v1.author.type", "contact_uuid": "@event.message._vnd.v1.chat.contact_uuid", "message_body": "@event.message.text.body", "message_direction": "@event.message._vnd.v1.direction", "message_id": "@event.message.id", "message_inserted_at": "@event.message._vnd.v1.chat.inserted_at", "message_updated_at": "@event.message._vnd.v1.chat.updated_at"}}')
|
| 82 |
|
| 83 |
# Case: Wrong payload object
|
| 84 |
-
run_full_nlu_endpoint_payload_test(b'{"message_data": {"_vnd": {"v1": {"author": {"id": 54327547257, "name": "Jin", "type": "OWNER"}, "card_uuid": None, "chat": {"assigned_to": None, "contact_uuid": "f7889-f78dfgb798-f786ah89g7-f78f9a", "inserted_at": "2023-03-28T13:21:47.581221Z", "owner": "+43789789146", "permalink": "", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 97, "updated_at": "2023-04-07T21:05:27.389948Z", "uuid": "dfg9a78-d76a786dghas-78d9fga789g-a78d69a9"}, "direction": "inbound", "faq_uuid": None, "in_reply_to": None, "inserted_at": "2023-04-07T21:05:27.368580Z", "labels": [], "last_status": None, "last_status_timestamp": None, "on_fallback_channel": False, "rendered_content": None, "uuid": "hf78s7s89b-789fb68d9fg-789fb789dfb-f79sfb789"}}, "from": 5475248689, "id": "SBDE4zgAAy7887sfdT35SHFS", "text": {"body": 1000}, "timestamp": 1680901527, "type": "text"}, "type": "message"}')
|
| 85 |
|
| 86 |
|
| 87 |
# run_simulated_request('intent-classification', 'exit')
|
|
|
|
| 77 |
# # Case: Correct payload
|
| 78 |
run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}')
|
| 79 |
|
| 80 |
+
# Case: Correct payload + extra fields
|
| 81 |
+
run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z", "question": "What is next - 2, 6, 8?", "expected_answer": 8}}')
|
| 82 |
+
|
| 83 |
# # Case: Incorrect payload values
|
| 84 |
run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "@event.message._vnd.v1.chat.owner", "author_type": "@event.message._vnd.v1.author.type", "contact_uuid": "@event.message._vnd.v1.chat.contact_uuid", "message_body": "@event.message.text.body", "message_direction": "@event.message._vnd.v1.direction", "message_id": "@event.message.id", "message_inserted_at": "@event.message._vnd.v1.chat.inserted_at", "message_updated_at": "@event.message._vnd.v1.chat.updated_at"}}')
|
| 85 |
|
| 86 |
# Case: Wrong payload object
|
| 87 |
+
# run_full_nlu_endpoint_payload_test(b'{"message_data": {"_vnd": {"v1": {"author": {"id": 54327547257, "name": "Jin", "type": "OWNER"}, "card_uuid": None, "chat": {"assigned_to": None, "contact_uuid": "f7889-f78dfgb798-f786ah89g7-f78f9a", "inserted_at": "2023-03-28T13:21:47.581221Z", "owner": "+43789789146", "permalink": "", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 97, "updated_at": "2023-04-07T21:05:27.389948Z", "uuid": "dfg9a78-d76a786dghas-78d9fga789g-a78d69a9"}, "direction": "inbound", "faq_uuid": None, "in_reply_to": None, "inserted_at": "2023-04-07T21:05:27.368580Z", "labels": [], "last_status": None, "last_status_timestamp": None, "on_fallback_channel": False, "rendered_content": None, "uuid": "hf78s7s89b-789fb68d9fg-789fb789dfb-f79sfb789"}}, "from": 5475248689, "id": "SBDE4zgAAy7887sfdT35SHFS", "text": {"body": 1000}, "timestamp": 1680901527, "type": "text"}, "type": "message"}')
|
| 88 |
|
| 89 |
|
| 90 |
# run_simulated_request('intent-classification', 'exit')
|