Spaces:
Runtime error
Runtime error
Greg Thompson commited on
Commit ·
46b6696
1
Parent(s): 1399a2f
Add error handling for additional payload types
Browse filesFiles changed (3)
3 unmodified lines456789102 unmodified lines1314151617181920212223242526272829131 unmodified lines16116216316416516616716814 unmodified lines1831841851861871881891901911921931943 unmodified lines`python -m uvicorn app:app --reload --host localhost --port 7860`"""import astimport mathactive.microlessons.num_one as num_one_quizimport sentry_sdkfrom fastapi import FastAPI, Request2 unmodified linesfrom fastapi.templating import Jinja2Templates# from mathtext.sentiment import sentimentfrom mathtext.text2int import text2intfrom pydantic import BaseModelfrom mathtext_fastapi.logging import prepare_message_data_for_loggingfrom mathtext_fastapi.conversation_manager import manage_conversation_responsefrom mathtext_fastapi.v2_conversation_manager import manage_conversation_responsefrom mathtext_fastapi.nlu import evaluate_message_with_nlufrom mathtext_fastapi.nlu import run_intent_classificationimport osfrom dotenv import load_dotenvload_dotenv()sentry_sdk.init(131 unmodified lines{'type':'integer', 'data': '8', 'confidence': 0}{'type':'sentiment', 'data': 'negative', 'confidence': 0.99}"""data_dict = await request.json()message_data = data_dict.get('message_data', '')nlu_response = evaluate_message_with_nlu(message_data)return JSONResponse(content=nlu_response)14 unmodified lines'state': 'question'}"""print("STEP 1")data_dict = await request.json()message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))user_id = message_data['user_id']message_text = message_data['message_text']print("STEP 2")return num_one_quiz.process_user_message(user_id, message_text)3 unmodified lines4567891011122 unmodified lines151617181920212223242526272829131 unmodified lines16116216316416516616716816917017114 unmodified lines1861871881891901911921931941953 unmodified lines`python -m uvicorn app:app --reload --host localhost --port 7860`"""import astfrom json import JSONDecodeErrorimport mathactive.microlessons.num_one as num_one_quizimport osimport sentry_sdkfrom fastapi import FastAPI, Request2 unmodified linesfrom fastapi.templating import Jinja2Templates# from mathtext.sentiment import sentimentfrom mathtext.text2int import text2intfrom mathtext_fastapi.logging import prepare_message_data_for_loggingfrom mathtext_fastapi.conversation_manager import manage_conversation_responsefrom mathtext_fastapi.v2_conversation_manager import manage_conversation_responsefrom mathtext_fastapi.nlu import evaluate_message_with_nlufrom mathtext_fastapi.nlu import run_intent_classificationfrom pydantic import BaseModelfrom dotenv import load_dotenvload_dotenv()sentry_sdk.init(131 unmodified lines{'type':'integer', 'data': '8', 'confidence': 0}{'type':'sentiment', 'data': 'negative', 'confidence': 0.99}"""try:data_dict = await request.json()except JSONDecodeError:data_dict = {}message_data = data_dict.get('message_data', {})nlu_response = evaluate_message_with_nlu(message_data)return JSONResponse(content=nlu_response)14 unmodified lines'state': 'question'}"""data_dict = await request.json()message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))user_id = message_data['user_id']message_text = message_data['message_text']return num_one_quiz.process_user_message(user_id, message_text)
3 unmodified lines45678910111213149 unmodified lines1631641651661671681691701711721731741751761771783 unmodified linesfrom dateutil.parser import isoparsefrom fuzzywuzzy import fuzzfrom mathtext_fastapi.logging import prepare_message_data_for_loggingfrom mathtext.sentiment import sentimentfrom mathtext.text2int import text2int, TOKENS2INT_ERROR_INTfrom mathtext_fastapi.intent_classification import predict_message_intentlog = getLogger(__name__)149 unmodified lines"""try:isinstance(isoparse(payload_object.get('message_inserted_at')),dt.datetime)isinstance(isoparse(payload_object.get('message_updated_at')),dt.datetime)except ValueError:return Falsereturn (isinstance(payload_object, Mapping) andisinstance(payload_object.get('author_id'), str) and3 unmodified lines45678910111213149 unmodified lines1631641651661671681691701711721731741751761773 unmodified linesfrom dateutil.parser import isoparsefrom fuzzywuzzy import fuzzfrom mathtext_fastapi.intent_classification import predict_message_intentfrom mathtext_fastapi.logging import prepare_message_data_for_loggingfrom mathtext.sentiment import sentimentfrom mathtext.text2int import text2int, TOKENS2INT_ERROR_INTlog = getLogger(__name__)149 unmodified lines"""try:isinstance(isoparse(payload_object.get('message_inserted_at','')),dt.datetime)isinstance(isoparse(payload_object.get('message_updated_at','')),dt.datetime)except ValueError:return Falsereturn (isinstance(payload_object, Mapping) andisinstance(payload_object.get('author_id'), str) and
70 unmodified lines71727374757677787970 unmodified linesprint(request)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"}}')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"}}')# run_simulated_request('intent-classification', 'exit')# run_simulated_request('intent-classification', "I'm not sure")# run_simulated_request('intent-classification', "easier")70 unmodified lines7172737475767778798081828384858687888970 unmodified linesprint(request)# Case: Wrong keyrun_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"}}')# Case: Correct payloadrun_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"}}')# Case: Incorrect payload valuesrun_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"}}')# Case: Wrong payload objectrun_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"}')# run_simulated_request('intent-classification', 'exit')# run_simulated_request('intent-classification', "I'm not sure")# run_simulated_request('intent-classification', "easier")
app.py+9-8
mathtext_fastapi/nlu.py+3-4
scripts/make_request.py+10