TangibleAI/mathtext-fastapi
1
1import json2import requests3 4 5def add_message_text_to_sample_object(message_text):6 """7 Builds a sample request object using an example of a student answer8 9 Input10 - message_text: str - an example of user input to test11 12 Example Input13 "test message"14 15 Output16 - b_string: json b-string - simulated Turn.io message data17 18 Example Output19 b'{"context": "hi", "message_data": {"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "test message", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}'20 21 """22 message_data = '{' + f'"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "{message_text}", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"' + '}'23 # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}"' + '}'24 25 context_data = '{' + '"user":"", "state":"start-conversation", "bot_message":"", "user_message":"{message_text}"' + '}'26 27 # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}","text": "What is 2+3?","question_numbers": [4,3],"right_answer": 7,"number_correct": 2, "number_incorrect": 0, "hints_used": 0, "level": "easy"' + '}'28 29 json_string = '{' + f'"context_data": {context_data}, "message_data": {message_data}' + '}'30 b_string = json_string.encode("utf-8")31 32 return b_string33 34# """35# "text": "What is 2+3?",36# "question_numbers": [2,3],37# "right_answer": 5,38# "number_correct": 2,39# "hints_used": 0,40# """41 42 43def run_simulated_request(endpoint, sample_answer, context=None):44 print(f"Case: {sample_answer}")45 b_string = add_message_text_to_sample_object(sample_answer)46 47 if endpoint == 'sentiment-analysis' or endpoint == 'text2int' or endpoint =='intent-classification':48 request = requests.post(49 url=f'http://localhost:7860/{endpoint}',50 json={'content': sample_answer}51 ).json()52 else:53 request = requests.post(54 url=f'http://localhost:7860/{endpoint}',55 data=b_string56 ).json()57 58 print(request)59 60 61# run_simulated_request('intent-classification', 'exit')62# run_simulated_request('intent-classification', "I'm not sure")63# run_simulated_request('sentiment-analysis', 'I reject it')64# run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')65run_simulated_request('nlu', 'test message')66run_simulated_request('nlu', 'eight')67run_simulated_request('nlu', 'is it 8')68run_simulated_request('nlu', 'can I know how its 0.5')69run_simulated_request('nlu', 'eight, nine, ten')70run_simulated_request('nlu', '8, 9, 10')71run_simulated_request('nlu', '8')72run_simulated_request('nlu', "I don't know")73run_simulated_request('nlu', "I don't know eight")74run_simulated_request('nlu', "I don't 9")75run_simulated_request('nlu', "0.2")76run_simulated_request('nlu', 'Today is a wonderful day')77run_simulated_request('nlu', 'IDK 5?')78# run_simulated_request('manager', '')79# run_simulated_request('manager', 'add')80# run_simulated_request('manager', 'subtract')81# run_simulated_request("question", {82# 'number_correct': 0,83# 'number_incorrect': 0,84# 'level': 'easy'85# })86# run_simulated_request("hint", {87# 'question_numbers': [1, 2, 3],88# 'right_answer': 3,89# 'number_correct': 0,90# 'number_incorrect': 0,91# 'level': 'easy',92# 'hints_used': 093# })94# run_simulated_request("generate_question", {95# 'level': 'medium'96# })97# run_simulated_request("numbers_by_level", {98# 'level': 'medium'99# })100# run_simulated_request("number_sequence", {101# "current_number": 10,102# "ordinal_number": 2,103# "times": 1104# })105# run_simulated_request("level", {106# "current_level": "hard",107# "level_up": False108# })109# run_simulated_request('manager', 'exit')110 111 112# Example of simplified object received from Turn.io stacks113# This is a contrived example to show the structure, not an actual state114# NOTE: This is actually a bstring, not a dict115simplified_json = {116 "context": {117 "user": "+57787919091", 118 "state": "answer-addition-problem", 119 "bot_message": "What is 2+2?", 120 "user_message": "eight",121 "type": "ask"122 },123 "message_data": {124 "author_id": "+57787919091", 125 "author_type": "OWNER", 126 "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", 127 "message_body": "eight", 128 "message_direction": "inbound", 129 "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", 130 "message_inserted_at": "2023-01-10T02:37:28.477940Z", 131 "message_updated_at": "2023-01-10T02:37:28.487319Z"132 }133}134 135 136# Full example of event data from Turn.io 137# simplified_json is built from this in Turn.io138# full_json = {139# 'message': {140# '_vnd': {141# 'v1': {142# 'author': {143# 'id': 57787919091,144# 'name': 'GT',145# 'type': 'OWNER'146# },147# 'card_uuid': None,148# 'chat': {149# 'assigned_to': {150# 'id': 'jhk151kl-hj42-3752-3hjk-h4jk6hjkk2',151# 'name': 'Greg Thompson',152# 'type': 'OPERATOR'153# },154# 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09',155# 'inserted_at': '2022-07-05T04:00:34.033522Z',156# 'owner': '+57787919091',157# 'permalink': 'https://app.turn.io/c/4kl209sd0-a7b8-2hj3-8563-3hu4a89b32',158# 'state': 'OPEN',159# 'state_reason': 'Re-opened by inbound message.',160# 'unread_count': 19,161# 'updated_at': '2023-01-10T02:37:28.487319Z',162# 'uuid': '4kl209sd0-a7b8-2hj3-8563-3hu4a89b32'163# },164# 'direction': 'inbound',165# 'faq_uuid': None,166# 'in_reply_to': None,167# 'inserted_at': '2023-01-10T02:37:28.477940Z',168# 'labels': [{169# 'confidence': 0.506479332,170# 'metadata': {171# 'nlu': {172# 'confidence': 0.506479332,173# 'intent': 'question',174# 'model_name': 'nlu-general-spacy-ngrams-20191014'175# }176# },177# 'uuid': 'ha7890s2k-hjk2-2476-s8d9-fh9779a8a9ds',178# 'value': 'Unclassified'179# }],180# 'last_status': None,181# 'last_status_timestamp': None,182# 'on_fallback_channel': False,183# 'rendered_content': None,184# 'uuid': 's8df79zhws-h89s-hj23-7s8d-thb248d9bh2qn'185# }186# },187# 'from': 57787919091,188# 'id': 'hsjkthzZGehkzs09sijWA3',189# 'text': {'body': 'eight'},190# 'timestamp': 1673318248,191# 'type': 'text'192# },193# 'type': 'message'194# }195 