File size: 12,332 Bytes
c186b27
349d1a2
 
bdc38d2
2b6660e
c186b27
 
 
 
 
6457b4b
c186b27
 
 
822e1b3
349d1a2
c186b27
822e1b3
 
 
 
 
 
 
 
76f757a
c186b27
822e1b3
 
c186b27
 
 
 
 
822e1b3
76f757a
 
2b6660e
 
 
 
 
 
 
 
 
c186b27
76f757a
2b6660e
c186b27
 
 
2b6660e
 
 
 
 
 
 
c186b27
2b6660e
822e1b3
 
c186b27
2b6660e
 
 
 
 
5585321
c186b27
 
 
 
 
 
2b6660e
c186b27
 
 
349d1a2
 
 
 
 
 
 
 
 
 
 
 
 
5585321
349d1a2
 
5585321
 
349d1a2
5585321
349d1a2
 
5585321
 
 
2b6660e
254630f
 
 
5585321
76f757a
068cb1a
 
76f757a
 
254630f
068cb1a
 
2b6660e
 
 
c186b27
 
 
 
bdc38d2
 
 
 
 
 
 
 
c186b27
 
 
 
 
 
 
068cb1a
 
 
76f757a
349d1a2
254630f
c9566b5
 
 
 
 
 
 
 
 
76f757a
c9566b5
76f757a
c9566b5
76f757a
 
 
 
254630f
 
c186b27
5585321
c186b27
 
2b6660e
c186b27
 
76f757a
bdc38d2
c186b27
 
 
bdc38d2
 
 
 
 
 
 
 
6457b4b
c186b27
 
 
 
 
 
2b6660e
 
c186b27
068cb1a
 
 
349d1a2
5585321
 
 
349d1a2
 
 
bdc38d2
 
349d1a2
 
 
 
 
bdc38d2
 
 
 
 
 
 
 
349d1a2
5585321
349d1a2
 
 
5585321
2b6660e
068cb1a
 
bdc38d2
068cb1a
bdc38d2
 
068cb1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc38d2
 
 
 
 
 
 
 
068cb1a
 
 
 
 
 
 
 
 
 
2b6660e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import re
import numpy as np
import scipy as sp
import glob
import json
from typing import Dict, List, Tuple, Union

import torch
import pandas
import streamlit as st
import matplotlib.pyplot as plt

from inference_tokenizer import NextSentencePredictionTokenizer
from models import get_class


def get_model(_model_path):
    print(f"Getting model at {_model_path}")
    if os.path.isfile(os.path.join(_model_path, "meta-info.json")):
        with open(os.path.join(_model_path, "meta-info.json"), "r") as f:
            meta_info = json.load(f)
            _model_package = meta_info["kwargs"].get("model_package", "transformers")
            _model_class = meta_info["kwargs"].get("model_class", "BertForNextSentencePrediction")
    else:
        raise FileNotFoundError("Model is provided without meta-info.json. Cannot interfere proper configuration!")

    model_class = get_class(_model_package, _model_class)
    _model = model_class.from_pretrained(_model_path)
    _model.eval()
    return _model


def get_tokenizer(tokenizer_path):
    print(f"Getting tokenizer at {tokenizer_path}")
    from transformers import AutoTokenizer
    tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
    if os.path.isfile(os.path.join(tokenizer_path, "meta-info.json")):
        with open(os.path.join(tokenizer_path, "meta-info.json"), "r") as f:
            meta_info = json.load(f)
            tokenizer_args = meta_info["tokenizer_args"]
            special_token = meta_info["kwargs"]["special_token"]
    else:
        raise FileNotFoundError("Tokenizer is provided without meta-info.json. Cannot interfere proper configuration!")

    if special_token != " ":
        tokenizer.add_special_tokens({"additional_special_tokens": [special_token]})

    _inference_tokenizer = NextSentencePredictionTokenizer(tokenizer, **tokenizer_args)
    return _inference_tokenizer


models_path = glob.glob("./model/*/info.json")
models = {}
for model_path in models_path:
    with open(model_path, "r") as f:
        model_data = json.load(f)
        model_data["path"] = model_path.replace("info.json", "")
        models[model_data["model"]] = model_data

model_name = st.selectbox('Which model do you want to use?',
                          (x for x in sorted(models.keys())),
                          index=0)

model_path = models[model_name]["path"]
model = get_model(model_path)
inference_tokenizer = get_tokenizer(model_path)


def get_evaluation_data_from_json(_context: List) -> List[Tuple[List, str, str]]:
    output_data = []
    for _dict in _context:
        _dict: Dict
        for source in _dict["answers"].values():
            for _t, sentences in source.items():
                for sentence in sentences:
                    output_data.append((_dict["context"], sentence, _t))
    return output_data


control_sequence_regex_1 = re.compile(r"#.*? ")
control_sequence_regex_2 = re.compile(r"#.*?\n")


def _clean_conversational_line(_line: str):
    _line = _line.replace("Bot: ", "")
    _line = _line.replace("User: ", "")
    _line = control_sequence_regex_1.sub("", _line)
    _line = control_sequence_regex_2.sub("\n", _line)
    return _line.strip()


def get_evaluation_data_from_dialogue(_context: List[str]) -> List[Dict]:
    output_data = []
    _context = list(map(lambda x: x.strip(), _context))
    _context = list(filter(lambda x: len(x), _context))
    for idx, _line in enumerate(_context):
        actual_context = _context[max(0, idx - 5):idx]
        gradual_context_dict = {_line: []}
        for context_idx in range(len(actual_context)):
            gradual_context_dict[_line].append(actual_context[-context_idx:])
        output_data.append(gradual_context_dict)
    return output_data


option = st.selectbox("Choose type of input:",
                      ["01 - String (one turn per line)",
                       "02 - JSON (aggregated)",
                       "03 - JSON (example CA-OOD)",
                       "04 - JSON (example Elysai)",
                       "05 - Diagnostic mode",
                       "06 - JSON (example Elysai - large)",
                       "07 - Dialogue Breakdown Challenge"])

progres_bar = st.progress(0.0, text="Inference")

if "01" in option:
    with st.form("input_text"):
        context = st.text_area("Insert context here (one turn per line):")
        actual_text = st.text_input("Insert current turn:")
        context = list(filter(lambda x: len(x.strip()) >= 1, context.split("\n")))

        input_tensor = inference_tokenizer.get_item(context=context, actual_sentence=actual_text)
        output_model = model(**input_tensor.data).logits

        output_model = output_model.detach().numpy()[0]
        if len(output_model) == 2:  # classification
            output_model = sp.special.softmax(output_model, axis=-1)
            prop_follow = output_model[0]
            prop_not_follow = output_model[1]
        elif len(output_model) == 1:  # regression
            prop_follow = 1 - output_model[0]
            prop_not_follow = 1 - prop_follow

        submitted = st.form_submit_button("Submit")
        if submitted:
            fig, ax = plt.subplots()
            ax.pie([prop_follow, prop_not_follow], labels=["Probability - Follow", "Probability - Not Follow"],
                   autopct='%1.1f%%')
            st.pyplot(fig)

if "02" in option or "03" in option or "04" in option or "06" in option:
    with st.form("input_text"):
        from data.example_data import ca_ood, elysai, elysai_large

        option: str
        # > Python 3.10
        # match option.split("-")[0].strip():
        #     case "03":
        #         text = json.dumps(choices[0])
        #     case "04":
        #         text = json.dumps(choices[1])
        #     case _:
        #         text = ""
        option = option.split("-")[0].strip()
        text = ""
        if option == "03":
            text = json.dumps(ca_ood)
        elif option == "04":
            text = json.dumps(elysai)
        elif option == "06":
            text = json.dumps(elysai_large)

        context = st.text_area("Insert JSON here:", value=str(text))

        if "{" in context:
            data_for_evaluation = get_evaluation_data_from_json(_context=json.loads(context))
        results = []
        accuracy = []

        submitted = st.form_submit_button("Submit")
        if submitted:
            for idx, datapoint in enumerate(data_for_evaluation):
                progres_bar.progress(idx / len(data_for_evaluation), text="Inference")
                c, s, human_label = datapoint
                input_tensor = inference_tokenizer.get_item(context=c, actual_sentence=s)
                output_model = model(**input_tensor.data).logits
                output_model = output_model.detach().numpy()[0]
                if len(output_model) == 2:  # classification
                    output_model = sp.special.softmax(output_model, axis=-1)
                    prop_follow = output_model[0]
                    prop_not_follow = output_model[1]
                elif len(output_model) == 1:  # regression
                    prop_follow = 1 - output_model[0]
                    prop_not_follow = 1 - prop_follow

                results.append((c, s, human_label, prop_follow, prop_not_follow))
                if human_label == "coherent":
                    accuracy.append(int(prop_follow > prop_not_follow))
                else:
                    accuracy.append(int(prop_not_follow > prop_follow))
            st.metric(label="Accuracy", value=f"{sum(accuracy) / len(accuracy)} %")
            df = pandas.DataFrame(results, columns=["Context", "Query", "Human Label", "Probability (follow)",
                                                    "Probability (not-follow)"])
            st.dataframe(df)

if "05" in option:
    with st.form("input_text"):
        context_size = 5
        context = st.text_area("Insert dialogue here (one turn per line):")
        submitted = st.form_submit_button("Submit")
        if submitted:
            data_for_evaluation = get_evaluation_data_from_dialogue(_clean_conversational_line(context).split("\n"))
            lines = []
            scores = np.zeros(shape=(len(data_for_evaluation), context_size))
            for idx, datapoint in enumerate(data_for_evaluation):
                progres_bar.progress(idx / len(data_for_evaluation), text="Inference")
                for actual_sentence, contexts in datapoint.items():
                    lines.append(actual_sentence)
                    for c in contexts:
                        input_tensor = inference_tokenizer.get_item(context=c, actual_sentence=actual_sentence)
                        output_model = model(**input_tensor.data).logits
                        output_model = output_model.detach().numpy()[0]
                        if len(output_model) == 2:  # classification
                            output_model = sp.special.softmax(output_model, axis=-1)
                            prop_follow = output_model[0]
                            prop_not_follow = output_model[1]
                        elif len(output_model) == 1:  # regression
                            prop_follow = 1 - output_model[0]
                            prop_not_follow = 1 - prop_follow
                        scores[len(lines) - 1][len(c) - 1] = prop_follow

            aggregated_result = []
            for idx, line in enumerate(lines):
                aggregated_result.append([line] + scores[idx].tolist())
            st.table(aggregated_result)

if "07" in option:
    from data.example_data import dbc

    select_conversation = st.selectbox("Which dialogue to evaluate", list(range(len(dbc))), index=0)
    context = st.text_area("Insert dialogue here (one turn per line):",
                           value=json.dumps([dbc[int(select_conversation)]]))
    st.markdown("# Formatted form")
    context_json = json.loads(context)
    output = ""
    for conversation in context_json:
        for utterance in conversation:
            output += " * " + utterance["text"] + "\n"
        output += "## ------------------------ "
    st.markdown(output)
    with st.form("input_text"):
        context_size = 5
        submitted = st.form_submit_button("Submit")
        if submitted:
            aggregated_result = []
            for idx, conversation in enumerate(context_json):
                data_for_evaluation = get_evaluation_data_from_dialogue([x["text"] for x in conversation])
                lines = []
                scores = np.zeros(shape=(len(data_for_evaluation), context_size))
                for datapoint in data_for_evaluation:
                    progres_bar.progress(idx / len(data_for_evaluation), text="Inference")
                    for actual_sentence, contexts in datapoint.items():
                        lines.append(actual_sentence)
                        for c in contexts:
                            input_tensor = inference_tokenizer.get_item(context=c, actual_sentence=actual_sentence)
                            output_model = model(**input_tensor.data).logits
                            output_model = output_model.detach().numpy()[0]
                            if len(output_model) == 2:  # classification
                                output_model = sp.special.softmax(output_model, axis=-1)
                                prop_follow = output_model[0]
                                prop_not_follow = output_model[1]
                            elif len(output_model) == 1:  # regression
                                prop_follow = 1 - output_model[0]
                                prop_not_follow = 1 - prop_follow
                            scores[len(lines) - 1][len(c) - 1] = prop_follow

                for idx, line in enumerate(lines):
                    NB = conversation[idx]["NB"]
                    PB = conversation[idx]["PB"]
                    B = conversation[idx]["B"]
                    aggregated_result.append([line] + [f"{NB}/{PB}/{B}"] + scores[idx].tolist())
                aggregated_result.append([["-"] * len(aggregated_result[-1])])
            st.table(aggregated_result)

st.markdown("## Description of models:")
for x in sorted(models.values(), key=lambda x: x["model"]):
    st.write((str(x["model"] + " - " + x["description"])))