Greg Thompson commited on
Commit
2286f04
1 Parent(s): a61d64f

Update nlu endpoint with additional logging

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. mathtext_fastapi/nlu.py +12 -4
app.py CHANGED
@@ -32,7 +32,7 @@ sentry_sdk.init(
32
  # Set traces_sample_rate to 1.0 to capture 100%
33
  # of transactions for performance monitoring.
34
  # We recommend adjusting this value in production,
35
- traces_sample_rate=0.20,
36
  )
37
 
38
  app = FastAPI()
 
32
  # Set traces_sample_rate to 1.0 to capture 100%
33
  # of transactions for performance monitoring.
34
  # We recommend adjusting this value in production,
35
+ traces_sample_rate=1.0,
36
  )
37
 
38
  app = FastAPI()
mathtext_fastapi/nlu.py CHANGED
@@ -1,10 +1,15 @@
 
 
 
1
  from fuzzywuzzy import fuzz
2
  from mathtext_fastapi.logging import prepare_message_data_for_logging
3
  from mathtext.sentiment import sentiment
4
  from mathtext.text2int import text2int
5
  from mathtext_fastapi.intent_classification import create_intent_classification_model, retrieve_intent_classification_model, predict_message_intent
6
- import re
7
 
 
 
 
8
 
9
  def build_nlu_response_object(type, data, confidence):
10
  """ Turns nlu results into an object to send back to Turn.io
@@ -141,10 +146,13 @@ def evaluate_message_with_nlu(message_data):
141
  {'type': 'sentiment', 'data': 'NEGATIVE', 'confidence': 0.9997807145118713}
142
  """
143
  # Keeps system working with two different inputs - full and filtered @event object
 
144
  try:
145
  message_text = str(message_data.get('message_body', ''))
146
- except KeyError:
147
- message_text = ''
 
 
148
 
149
  # Run intent classification only for keywords
150
  intent_api_response = run_intent_classification(message_text)
@@ -154,7 +162,7 @@ def evaluate_message_with_nlu(message_data):
154
 
155
  number_api_resp = text2int(message_text.lower())
156
 
157
- if number_api_resp == 32202:
158
  # Run intent classification with logistic regression model
159
  predicted_label = predict_message_intent(message_text)
160
  if predicted_label['confidence'] > 0.01:
 
1
+ from logging import getLogger
2
+ import re
3
+
4
  from fuzzywuzzy import fuzz
5
  from mathtext_fastapi.logging import prepare_message_data_for_logging
6
  from mathtext.sentiment import sentiment
7
  from mathtext.text2int import text2int
8
  from mathtext_fastapi.intent_classification import create_intent_classification_model, retrieve_intent_classification_model, predict_message_intent
 
9
 
10
+ log = getLogger(__name__)
11
+
12
+ ERROR_CODE = 32202
13
 
14
  def build_nlu_response_object(type, data, confidence):
15
  """ Turns nlu results into an object to send back to Turn.io
 
146
  {'type': 'sentiment', 'data': 'NEGATIVE', 'confidence': 0.9997807145118713}
147
  """
148
  # Keeps system working with two different inputs - full and filtered @event object
149
+ log.info(f'Starting evaluate message: {message_data}')
150
  try:
151
  message_text = str(message_data.get('message_body', ''))
152
+ except:
153
+ log.error(f'Invalid request payload: {message_data}')
154
+ # use python logging system to do this//
155
+ return {'type': 'error', 'data': ERROR_CODE, 'confidence': 0}
156
 
157
  # Run intent classification only for keywords
158
  intent_api_response = run_intent_classification(message_text)
 
162
 
163
  number_api_resp = text2int(message_text.lower())
164
 
165
+ if number_api_resp == ERROR_CODE:
166
  # Run intent classification with logistic regression model
167
  predicted_label = predict_message_intent(message_text)
168
  if predicted_label['confidence'] > 0.01: