dream
115 строк · 5.0 Кб
1import requests2from time import time3
4
5def main_test():6url = "http://0.0.0.0:8150/model"7batch_url = "http://0.0.0.0:8150/batch_model"8configs = [9{10"sentences": ["do you like porn"],11"task": "all",12"possible_answers": {13"cobot_topics": ["Sex_Profanity"],14"cobot_dialogact_topics": ["Inappropriate_Content"],15},16},17{18"sentences": ["let's talk about movies", "do you like porn"],19"task": "cobot_dialogact_topics",20"answers_bert": [["Entertainment_Movies"], ["Inappropriate_Content"]],21},22{23"sentences": ["let's talk about games", "do you like watching films"],24"task": "cobot_topics",25"answers_bert": [["Games"], ["Movies_TV"]],26},27{28"sentences_with_history": ["What is the capital of Great Britain [SEP] I don't know"],29"sentences": ["I don't know"],30"task": "cobot_dialogact_intents",31"answers_bert": [["Information_DeliveryIntent"]],32},33{34"sentences": ["how do I empty my DNS cache?", "which do you prefer?", "where is montreal"],35"task": "factoid_classification",36"answers_bert": [["is_factoid"], ["is_conversational"], ["is_factoid"]],37},38{39"sentences": ["i love you", "i hate you", "It is now"],40"task": "sentiment_classification",41"answers_bert": [["positive"], ["negative"], ["neutral"]],42},43{44"sentences": ["why you are such a fool"],45"task": "emotion_classification",46"answers_bert": [["anger"]],47},48{49"sentences_with_history": ["this is the best dog [SEP] so what you think"],50"sentences": ["so what you think"],51"task": "midas_classification",52"answers_bert": [["open_question_opinion"]],53},54{55"sentences": ["please talk about movies", "talk about games"],56"task": "deeppavlov_topics",57"answers_bert": [["Movies&Tv"], ["Videogames"]],58},59{60"sentences": ["you son of the bitch", "yes", "do you like porn"],61"task": "toxic_classification",62"answers_bert": [["obscene"], ["not_toxic"], ["sexual_explicit"]],63},64]65t = time()66for config in configs:67if "sentences_with_history" in config:68config["utterances_with_histories"] = [[k] for k in config["sentences_with_history"]]69else:70config["utterances_with_histories"] = [[k] for k in config["sentences"]]71responses = requests.post(url, json=config).json()72batch_responses = requests.post(batch_url, json=config).json()73batch_error_msg = f"Batch responses {batch_responses} not match to responses {responses}"74assert (75batch_responses[0]["batch"][0]["toxic_classification"] == responses[0]["toxic_classification"]76), batch_error_msg77if config["task"] == "all":78for i in range(len(responses)):79print(f"Checking that at least 1 annotator works for {config['sentences'][i]}")80predicted_cobot_topics = [81class_
82for class_ in responses[i]["cobot_topics"]83if responses[i]["cobot_topics"][class_] == max(responses[0]["cobot_topics"].values())84]85predicted_cobot_da_topics = [86class_
87for class_ in responses[i]["cobot_dialogact_topics"]88if responses[i]["cobot_dialogact_topics"][class_]89== max(responses[i]["cobot_dialogact_topics"].values())90]91error_msg1 = (92f"Predicted cobot topics {predicted_cobot_topics} and da topics {predicted_cobot_da_topics}"93f"not match with sensitive cobot_topics {config['possible_answers']['cobot_topics']}"94f"and sensitive cobot da topics {config['possible_answers']['cobot_dialogact_topics']}"95)96assert any(97[98set(predicted_cobot_topics) & set(config["possible_answers"]["cobot_topics"]),99set(predicted_cobot_da_topics) & set(config["possible_answers"]["cobot_dialogact_topics"]),100]101), error_msg1102else:103responses = [j[config["task"]] for j in responses]104for response, answer, sentence in zip(responses, config["answers_bert"], config["sentences"]):105# print((response, answer, sentence))106predicted_classes = [class_ for class_ in response if response[class_] == max(response.values())]107assert sorted(answer) == sorted(predicted_classes), " * ".join(108[str(j) for j in [sentence, config["task"], answer, predicted_classes, response]]109)110print("SUCCESS!")111print(time() - t)112return 0113
114
115main_test()116