dream

Форк
0
115 строк · 5.0 Кб
1
import requests
2
from time import time
3

4

5
def main_test():
6
    url = "http://0.0.0.0:8150/model"
7
    batch_url = "http://0.0.0.0:8150/batch_model"
8
    configs = [
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
    ]
65
    t = time()
66
    for config in configs:
67
        if "sentences_with_history" in config:
68
            config["utterances_with_histories"] = [[k] for k in config["sentences_with_history"]]
69
        else:
70
            config["utterances_with_histories"] = [[k] for k in config["sentences"]]
71
        responses = requests.post(url, json=config).json()
72
        batch_responses = requests.post(batch_url, json=config).json()
73
        batch_error_msg = f"Batch responses {batch_responses} not match to responses {responses}"
74
        assert (
75
            batch_responses[0]["batch"][0]["toxic_classification"] == responses[0]["toxic_classification"]
76
        ), batch_error_msg
77
        if config["task"] == "all":
78
            for i in range(len(responses)):
79
                print(f"Checking that at least 1 annotator works for {config['sentences'][i]}")
80
                predicted_cobot_topics = [
81
                    class_
82
                    for class_ in responses[i]["cobot_topics"]
83
                    if responses[i]["cobot_topics"][class_] == max(responses[0]["cobot_topics"].values())
84
                ]
85
                predicted_cobot_da_topics = [
86
                    class_
87
                    for class_ in responses[i]["cobot_dialogact_topics"]
88
                    if responses[i]["cobot_dialogact_topics"][class_]
89
                    == max(responses[i]["cobot_dialogact_topics"].values())
90
                ]
91
                error_msg1 = (
92
                    f"Predicted cobot topics {predicted_cobot_topics} and da topics {predicted_cobot_da_topics}"
93
                    f"not match with sensitive cobot_topics {config['possible_answers']['cobot_topics']}"
94
                    f"and sensitive cobot da topics {config['possible_answers']['cobot_dialogact_topics']}"
95
                )
96
                assert any(
97
                    [
98
                        set(predicted_cobot_topics) & set(config["possible_answers"]["cobot_topics"]),
99
                        set(predicted_cobot_da_topics) & set(config["possible_answers"]["cobot_dialogact_topics"]),
100
                    ]
101
                ), error_msg1
102
        else:
103
            responses = [j[config["task"]] for j in responses]
104
            for response, answer, sentence in zip(responses, config["answers_bert"], config["sentences"]):
105
                #  print((response, answer, sentence))
106
                predicted_classes = [class_ for class_ in response if response[class_] == max(response.values())]
107
                assert sorted(answer) == sorted(predicted_classes), " * ".join(
108
                    [str(j) for j in [sentence, config["task"], answer, predicted_classes, response]]
109
                )
110
    print("SUCCESS!")
111
    print(time() - t)
112
    return 0
113

114

115
main_test()
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.