Spaces:
Runtime error
Runtime error
Merge branch 'feature-accept-more-input-types' into 'main'
Browse filesFeature accept more input types
See merge request tangibleai/community/mathtext-fastapi!6
- app.py +30 -7
- scripts/make_request.py +32 -0
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
"""FastAPI endpoint
|
2 |
To run locally use 'uvicorn app:app --host localhost --port 7860'
|
3 |
"""
|
|
|
4 |
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import JSONResponse
|
@@ -63,18 +64,40 @@ async def evaluate_user_message_with_nlu_api(request: Request):
|
|
63 |
|
64 |
data_dict = await request.json()
|
65 |
message_data = data_dict.get('message_data', '')
|
66 |
-
message_text = message_data['message']['text']['body']
|
67 |
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
sentiment_api_resp = sentiment(message_text)
|
72 |
# [{'label': 'POSITIVE', 'score': 0.991188645362854}]
|
73 |
sent_data_dict = {'type': 'sentiment', 'data': sentiment_api_resp[0]['label']}
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
prepare_message_data_for_logging(message_data)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
return JSONResponse(content=int_data_dict)
|
|
|
1 |
"""FastAPI endpoint
|
2 |
To run locally use 'uvicorn app:app --host localhost --port 7860'
|
3 |
"""
|
4 |
+
import re
|
5 |
|
6 |
from fastapi import FastAPI, Request
|
7 |
from fastapi.responses import JSONResponse
|
|
|
64 |
|
65 |
data_dict = await request.json()
|
66 |
message_data = data_dict.get('message_data', '')
|
67 |
+
message_text = message_data['message']['text']['body']
|
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
|
74 |
+
message_text_arr = re.split(", |,| ", message_text.strip())
|
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 = []
|
81 |
+
|
82 |
+
for student_response in message_text_arr:
|
83 |
+
# Checks the student answer and returns an integer
|
84 |
+
|
85 |
+
int_api_resp = text2int(student_response.lower())
|
86 |
+
student_response_arr.append(int_api_resp)
|
87 |
+
|
88 |
+
# '32202' is text2int's error code for non-integer student answers (ie., "I don't know")
|
89 |
+
# If any part of the list is 32202, sentiment analysis will run
|
90 |
+
if 32202 in student_response_arr:
|
91 |
sentiment_api_resp = sentiment(message_text)
|
92 |
# [{'label': 'POSITIVE', 'score': 0.991188645362854}]
|
93 |
sent_data_dict = {'type': 'sentiment', 'data': sentiment_api_resp[0]['label']}
|
94 |
+
response_object = {'type': 'sentiment', 'data': 'negative'}
|
95 |
+
else:
|
96 |
+
if len(student_response_arr) > 1:
|
97 |
+
response_object = {'type': 'integer', 'data': ','.join(str(num) for num in student_response_arr)}
|
98 |
+
else:
|
99 |
+
response_object = {'type': 'integer', 'data': student_response_arr[0]}
|
100 |
|
101 |
prepare_message_data_for_logging(message_data)
|
102 |
|
103 |
+
return JSONResponse(content=response_object)
|
|
|
|
scripts/make_request.py
CHANGED
@@ -74,9 +74,41 @@ print(request)
|
|
74 |
|
75 |
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":"eight"},"timestamp":1673318248,"type":"text"},"type":"message"}}\n'
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
request = requests.post(url=
|
78 |
'http://localhost:7860/nlu',
|
79 |
data=json
|
80 |
).json()
|
81 |
|
82 |
print(request)
|
|
|
|
74 |
|
75 |
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":"eight"},"timestamp":1673318248,"type":"text"},"type":"message"}}\n'
|
76 |
|
77 |
+
# eight > 8
|
78 |
+
request = requests.post(url=
|
79 |
+
'http://localhost:7860/nlu',
|
80 |
+
data=json
|
81 |
+
).json()
|
82 |
+
print(request)
|
83 |
+
|
84 |
+
json2 = 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":"eight, nine, ten"},"timestamp":1673318248,"type":"text"},"type":"message"}}\n'
|
85 |
+
|
86 |
+
# "eight, nine, ten" > 8,9,10
|
87 |
+
request = requests.post(url=
|
88 |
+
'http://localhost:7860/nlu',
|
89 |
+
data=json2
|
90 |
+
).json()
|
91 |
+
|
92 |
+
print(request)
|
93 |
+
|
94 |
+
|
95 |
+
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},"timestamp":1673318248,"type":"text"},"type":"message"}}\n'
|
96 |
+
|
97 |
+
# 8 > 8
|
98 |
+
request = requests.post(url=
|
99 |
+
'http://localhost:7860/nlu',
|
100 |
+
data=json
|
101 |
+
).json()
|
102 |
+
|
103 |
+
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
|
111 |
).json()
|
112 |
|
113 |
print(request)
|
114 |
+
|