Greg Thompson commited on
Commit
cb15a69
1 Parent(s): c32f8d2

Update message logging to fix bugs

Browse files
Files changed (3) hide show
  1. app.py +3 -2
  2. mathtext_fastapi/nlu.py +33 -3
  3. scripts/make_request.py +1 -1
app.py CHANGED
@@ -68,6 +68,7 @@ async def evaluate_user_message_with_nlu_api(request: Request):
68
 
69
  # Handles if a student answer is already an integer or a float (ie., 8)
70
  if type(message_text) == int or type(message_text) == float:
 
71
  return JSONResponse(content={'type': 'integer', 'data': message_text})
72
 
73
  # Removes whitespace and converts str to arr to handle multiple numbers
@@ -75,6 +76,7 @@ async def evaluate_user_message_with_nlu_api(request: Request):
75
 
76
  # Handle if a student answer is a string of numbers (ie., "8,9, 10")
77
  if all(ele.isdigit() for ele in message_text_arr):
 
78
  return JSONResponse(content={'type': 'integer', 'data': ','.join(message_text_arr)})
79
 
80
  student_response_arr = []
@@ -99,6 +101,5 @@ async def evaluate_user_message_with_nlu_api(request: Request):
99
  response_object = {'type': 'integer', 'data': student_response_arr[0]}
100
 
101
  # Uncomment to enable logging to Supabase
102
- # prepare_message_data_for_logging(message_data)
103
-
104
  return JSONResponse(content=response_object)
 
68
 
69
  # Handles if a student answer is already an integer or a float (ie., 8)
70
  if type(message_text) == int or type(message_text) == float:
71
+ prepare_message_data_for_logging(message_data)
72
  return JSONResponse(content={'type': 'integer', 'data': message_text})
73
 
74
  # Removes whitespace and converts str to arr to handle multiple numbers
 
76
 
77
  # Handle if a student answer is a string of numbers (ie., "8,9, 10")
78
  if all(ele.isdigit() for ele in message_text_arr):
79
+ prepare_message_data_for_logging(message_data)
80
  return JSONResponse(content={'type': 'integer', 'data': ','.join(message_text_arr)})
81
 
82
  student_response_arr = []
 
101
  response_object = {'type': 'integer', 'data': student_response_arr[0]}
102
 
103
  # Uncomment to enable logging to Supabase
104
+ prepare_message_data_for_logging(message_data)
 
105
  return JSONResponse(content=response_object)
mathtext_fastapi/nlu.py CHANGED
@@ -17,12 +17,42 @@ def format_datetime_in_isoformat(dt):
17
  return getattr(dt.now(), 'isoformat', lambda x: None)()
18
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def prepare_message_data_for_logging(message_data):
 
 
 
 
 
21
  project_data = {
22
- 'name': "turn.io",
23
  # Autogenerated fields: id, created_at, modified_at
24
  }
25
- project_data_log = log_message_data_through_supabase_api('project', project_data)
26
 
27
  contact_data = {
28
  'project': project_data_log.data[0]['id'], # FK
@@ -32,7 +62,7 @@ def prepare_message_data_for_logging(message_data):
32
  'contact_inserted_at': format_datetime_in_isoformat(datetime.now())
33
  # Autogenerated fields: id, created_at, modified_at
34
  }
35
- contact_data_log = log_message_data_through_supabase_api('contact', contact_data)
36
 
37
  message_data = {
38
  'contact': contact_data_log.data[0]['id'], # FK
 
17
  return getattr(dt.now(), 'isoformat', lambda x: None)()
18
 
19
 
20
+ def get_or_create_supabase_entry(table_name, insert_data, check_variable=None):
21
+ """ Checks whether a project or contact exists in the database and adds if one is not found
22
+
23
+ Input:
24
+ - table_name: str- the name of the table in Supabase that is being examined
25
+ - insert_data: json - the data to insert
26
+ - check_variable: str/None - the specific field to examine for existing matches
27
+
28
+ Result
29
+ - logged_data - an object with the Supabase data
30
+
31
+ """
32
+ if table_name == 'contact':
33
+ resp = SUPA.table('contact').select("*").eq("original_contact_id", insert_data['original_contact_id']).eq("project", insert_data['project']).execute()
34
+ else:
35
+ resp = SUPA.table(table_name).select("*").eq(check_variable, insert_data[check_variable]).execute()
36
+
37
+ if len(resp.data) == 0:
38
+ logged_data = log_message_data_through_supabase_api(table_name, insert_data)
39
+ else:
40
+ logged_data = resp
41
+ return logged_data
42
+
43
+
44
+
45
  def prepare_message_data_for_logging(message_data):
46
+ """ Builds the message data for each table and ensures it's logged to the database
47
+
48
+ Input:
49
+ - message_data: an object with the full message data from Turn.io/Whatsapp
50
+ """
51
  project_data = {
52
+ 'name': "Rori",
53
  # Autogenerated fields: id, created_at, modified_at
54
  }
55
+ project_data_log = get_or_create_supabase_entry('project', project_data, 'name')
56
 
57
  contact_data = {
58
  'project': project_data_log.data[0]['id'], # FK
 
62
  'contact_inserted_at': format_datetime_in_isoformat(datetime.now())
63
  # Autogenerated fields: id, created_at, modified_at
64
  }
65
+ contact_data_log = get_or_create_supabase_entry('contact', contact_data)
66
 
67
  message_data = {
68
  'contact': contact_data_log.data[0]['id'], # FK
scripts/make_request.py CHANGED
@@ -104,7 +104,7 @@ print(request)
104
 
105
  json = b'{"message_data": {"message":{"_vnd":{"v1":{"author":{"id":57787919091,"name":"GT","type":"OWNER"},"card_uuid":null,"chat":{"assigned_to":{"id":"jhk151kl-hj42-3752-3hjk-h4jk6hjkk2","name":"Greg Thompson","type":"OPERATOR"},"contact_uuid":"j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09","inserted_at":"2022-07-05T04:00:34.033522Z","owner":"+57787919091","permalink":"https://app.turn.io/c/4kl209sd0-a7b8-2hj3-8563-3hu4a89b32","state":"OPEN","state_reason":"Re-opened by inbound message.","unread_count":14,"updated_at":"2023-01-10T02:37:28.487319Z","uuid":"4kl209sd0-a7b8-2hj3-8563-3hu4a89b32"},"direction":"inbound","faq_uuid":null,"in_reply_to":null,"inserted_at":"2023-01-10T02:37:28.477940Z","labels":[{"confidence":0.506479332,"metadata":{"nlu":{"confidence":0.506479332,"intent":"question","model_name":"nlu-general-spacy-ngrams-20191014"}},"uuid":"ha7890s2k-hjk2-2476-s8d9-fh9779a8a9ds","value":"Unclassified"}],"last_status":null,"last_status_timestamp":null,"on_fallback_channel":false,"rendered_content":null,"uuid":"s8df79zhws-h89s-hj23-7s8d-thb248d9bh2qn"}},"from":57787919091,"id":"hsjkthzZGehkzs09sijWA3","text":{"body":"8, 9, 10"},"timestamp":1673318248,"type":"text"},"type":"message"}}\n'
106
 
107
- # 8, 9, 10 > 8,9,10
108
  request = requests.post(url=
109
  'http://localhost:7860/nlu',
110
  data=json
 
104
 
105
  json = b'{"message_data": {"message":{"_vnd":{"v1":{"author":{"id":57787919091,"name":"GT","type":"OWNER"},"card_uuid":null,"chat":{"assigned_to":{"id":"jhk151kl-hj42-3752-3hjk-h4jk6hjkk2","name":"Greg Thompson","type":"OPERATOR"},"contact_uuid":"j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09","inserted_at":"2022-07-05T04:00:34.033522Z","owner":"+57787919091","permalink":"https://app.turn.io/c/4kl209sd0-a7b8-2hj3-8563-3hu4a89b32","state":"OPEN","state_reason":"Re-opened by inbound message.","unread_count":14,"updated_at":"2023-01-10T02:37:28.487319Z","uuid":"4kl209sd0-a7b8-2hj3-8563-3hu4a89b32"},"direction":"inbound","faq_uuid":null,"in_reply_to":null,"inserted_at":"2023-01-10T02:37:28.477940Z","labels":[{"confidence":0.506479332,"metadata":{"nlu":{"confidence":0.506479332,"intent":"question","model_name":"nlu-general-spacy-ngrams-20191014"}},"uuid":"ha7890s2k-hjk2-2476-s8d9-fh9779a8a9ds","value":"Unclassified"}],"last_status":null,"last_status_timestamp":null,"on_fallback_channel":false,"rendered_content":null,"uuid":"s8df79zhws-h89s-hj23-7s8d-thb248d9bh2qn"}},"from":57787919091,"id":"hsjkthzZGehkzs09sijWA3","text":{"body":"8, 9, 10"},"timestamp":1673318248,"type":"text"},"type":"message"}}\n'
106
 
107
+ # "8, 9, 10" > "8,9,10"
108
  request = requests.post(url=
109
  'http://localhost:7860/nlu',
110
  data=json