Greg Thompson commited on
Commit
da7d340
·
2 Parent(s): 81aa998 8f01606

Merge branch 'staging' of https://huggingface.co/spaces/thompsgj/mathtext-fastapi into staging

Browse files
Files changed (2) hide show
  1. app.py +18 -2
  2. scripts/make_request.py +6 -6
app.py CHANGED
@@ -4,7 +4,9 @@ or
4
  `python -m uvicorn app:app --reload --host localhost --port 7860`
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 +28,10 @@ from pydantic import BaseModel
26
  from dotenv import load_dotenv
27
  load_dotenv()
28
 
 
 
29
  sentry_sdk.init(
30
- dsn=os.environ.get('SENTRY_DNS'),
31
 
32
  # Set traces_sample_rate to 1.0 to capture 100%
33
  # of transactions for performance monitoring.
@@ -161,11 +165,23 @@ 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
 
 
4
  `python -m uvicorn app:app --reload --host localhost --port 7860`
5
  """
6
  import ast
7
+ import json
8
  from json import JSONDecodeError
9
+ from logging import getLogger
10
  import mathactive.microlessons.num_one as num_one_quiz
11
  import os
12
  import sentry_sdk
 
28
  from dotenv import load_dotenv
29
  load_dotenv()
30
 
31
+ log = getLogger(__name__)
32
+
33
  sentry_sdk.init(
34
+ dsn=os.environ.get('SENTRY_DSN'),
35
 
36
  # Set traces_sample_rate to 1.0 to capture 100%
37
  # of transactions for performance monitoring.
 
165
  {'type':'integer', 'data': '8', 'confidence': 0}
166
  {'type':'sentiment', 'data': 'negative', 'confidence': 0.99}
167
  """
168
+ log.info(f'Received request: {request}')
169
+ log.info(f'Request header: {request.headers}')
170
+ request_body = await request.body()
171
+ log.info(f'Request body: {request_body}')
172
+ request_body_str = request_body.decode()
173
+ log.info(f'Request_body_str: {request_body_str}')
174
+
175
  try:
176
  data_dict = await request.json()
177
  except JSONDecodeError:
178
+ log.error(f'Request.json failed: {dir(request)}')
179
  data_dict = {}
180
+ message_data = data_dict.get('message_data')
181
+
182
+ if not message_data:
183
+ log.error(f'Data_dict: {data_dict}')
184
+ message_data = data_dict.get('message', {})
185
  nlu_response = evaluate_message_with_nlu(message_data)
186
  return JSONResponse(content=nlu_response)
187
 
scripts/make_request.py CHANGED
@@ -71,14 +71,14 @@ def run_full_nlu_endpoint_payload_test(sample_payload):
71
  print(request)
72
 
73
 
74
- # Case: Wrong key
75
- run_full_nlu_endpoint_payload_test(b'{"message": {"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"}}')
76
 
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"}')
 
71
  print(request)
72
 
73
 
74
+ # # Case: Wrong key
75
+ # run_full_nlu_endpoint_payload_test(b'{"message": {"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"}}')
76
 
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"}')