Spaces:
Runtime error
Runtime error
Greg Thompson
commited on
Commit
•
30094ce
1
Parent(s):
54f63ac
Update endpoint request handling and validation
Browse files- app.py +15 -3
- scripts/make_request.py +1 -1
app.py
CHANGED
@@ -5,6 +5,7 @@ or
|
|
5 |
"""
|
6 |
import ast
|
7 |
from json import JSONDecodeError
|
|
|
8 |
import mathactive.microlessons.num_one as num_one_quiz
|
9 |
import os
|
10 |
import sentry_sdk
|
@@ -26,8 +27,10 @@ from pydantic import BaseModel
|
|
26 |
from dotenv import load_dotenv
|
27 |
load_dotenv()
|
28 |
|
|
|
|
|
29 |
sentry_sdk.init(
|
30 |
-
dsn=os.environ.get('
|
31 |
|
32 |
# Set traces_sample_rate to 1.0 to capture 100%
|
33 |
# of transactions for performance monitoring.
|
@@ -148,7 +151,7 @@ def intent_classification_ep(content: Text = None):
|
|
148 |
content = {"message": ml_response}
|
149 |
return JSONResponse(content=content)
|
150 |
|
151 |
-
|
152 |
@app.post("/nlu")
|
153 |
async def evaluate_user_message_with_nlu_api(request: Request):
|
154 |
""" Calls nlu evaluation and returns the nlu_response
|
@@ -161,11 +164,20 @@ async def evaluate_user_message_with_nlu_api(request: Request):
|
|
161 |
{'type':'integer', 'data': '8', 'confidence': 0}
|
162 |
{'type':'sentiment', 'data': 'negative', 'confidence': 0.99}
|
163 |
"""
|
|
|
|
|
|
|
|
|
164 |
try:
|
165 |
data_dict = await request.json()
|
166 |
except JSONDecodeError:
|
|
|
167 |
data_dict = {}
|
168 |
-
message_data = data_dict.get('message_data'
|
|
|
|
|
|
|
|
|
169 |
nlu_response = evaluate_message_with_nlu(message_data)
|
170 |
return JSONResponse(content=nlu_response)
|
171 |
|
|
|
5 |
"""
|
6 |
import ast
|
7 |
from json import JSONDecodeError
|
8 |
+
from logging import getLogger
|
9 |
import mathactive.microlessons.num_one as num_one_quiz
|
10 |
import os
|
11 |
import sentry_sdk
|
|
|
27 |
from dotenv import load_dotenv
|
28 |
load_dotenv()
|
29 |
|
30 |
+
log = getLogger(__name__)
|
31 |
+
|
32 |
sentry_sdk.init(
|
33 |
+
dsn=os.environ.get('SENTRY_DSN'),
|
34 |
|
35 |
# Set traces_sample_rate to 1.0 to capture 100%
|
36 |
# of transactions for performance monitoring.
|
|
|
151 |
content = {"message": ml_response}
|
152 |
return JSONResponse(content=content)
|
153 |
|
154 |
+
import json
|
155 |
@app.post("/nlu")
|
156 |
async def evaluate_user_message_with_nlu_api(request: Request):
|
157 |
""" Calls nlu evaluation and returns the nlu_response
|
|
|
164 |
{'type':'integer', 'data': '8', 'confidence': 0}
|
165 |
{'type':'sentiment', 'data': 'negative', 'confidence': 0.99}
|
166 |
"""
|
167 |
+
log.info(f'Received request: {request}')
|
168 |
+
log.info(f'Request header: {request.headers}')
|
169 |
+
log.info(f'Request body: {request.body()}')
|
170 |
+
|
171 |
try:
|
172 |
data_dict = await request.json()
|
173 |
except JSONDecodeError:
|
174 |
+
log.error(f'Request.json failed: {dir(request)}')
|
175 |
data_dict = {}
|
176 |
+
message_data = data_dict.get('message_data')
|
177 |
+
|
178 |
+
if not message_data:
|
179 |
+
log.error(f'Data_dict: {data_dict}')
|
180 |
+
message_data = data_dict.get('message', {})
|
181 |
nlu_response = evaluate_message_with_nlu(message_data)
|
182 |
return JSONResponse(content=nlu_response)
|
183 |
|
scripts/make_request.py
CHANGED
@@ -81,7 +81,7 @@ run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "57787919091
|
|
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')
|
|
|
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')
|