File size: 9,551 Bytes
2a15960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
7f98908
2a15960
39a465b
2a15960
 
39a465b
 
 
 
 
2a15960
7f98908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a15960
7f98908
2a15960
 
7f98908
2a15960
 
 
 
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
 
 
39a465b
 
ef0d76e
2a15960
 
 
 
 
 
 
 
 
 
 
 
7f98908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a15960
 
 
 
7f98908
 
 
 
 
 
 
 
 
 
2a15960
 
7f98908
 
2a15960
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f98908
2a15960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39a465b
2a15960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
280
import json
from typing import List, Optional

import gradio as gr
from gradio.components import Markdown

from src.dto.dto import ExplanationGranularity, ExplanationDto
from src.utils.registry import EXPLAINERS, MODELS, PERTURBERS, COMPARATORS
from src.utils.segregate import PercentileBasedSegregator
from src.utils.visualizer import Visualizer


class MockExplainerUI:
    def __init__(
        self,
        logo_path: str,
        css_path: str,
        visualizer: Visualizer,
        window_title: str,
        title: str,
        examples: Optional[List[str]] = None,
    ):
        self.__logo_path = logo_path
        self.__css_path = css_path
        self.__examples = examples
        self.__window_title = window_title
        self.__title = title
        self.__visualizer = visualizer

        self.app: gr.Blocks = self.build_app()

    def build_app(self):
        with gr.Blocks(
            theme=gr.themes.Monochrome().set(
                button_primary_background_fill="#009374",
                button_primary_background_fill_hover="#009374C4",
                checkbox_label_background_fill_selected="#028A6EFF",
            ),
            css=self.__css_path,
            title=self.__window_title,
        ) as demo:
            self.__build_app_title()
            (
                qn_choice,
                user_input,
                system_response,
                granularity,
                upper_percentile,
                middle_percentile,
                lower_percentile,
                explainer_name,
                model_name,
                perturber_name,
                comparator_name,
                generator_vis,
                submit_btn,
            ) = self.__build_chat_and_explain()

            submit_btn.click(
                fn=self.run,
                inputs=[
                    qn_choice,
                    user_input,
                    granularity,
                    upper_percentile,
                    middle_percentile,
                    lower_percentile,
                    explainer_name,
                    model_name,
                    perturber_name,
                    comparator_name,
                ],
                outputs=[user_input, system_response, generator_vis],
            )

        return demo

    def run(
        self,
        qn_choice: str,
        user_input: str,
        granularity: ExplanationGranularity,
        upper_percentile: str,
        middle_percentile: str,
        lower_percentile: str,
        explainer_name: str,
        model_name: str,
        perturber_name: str,
        comparator_name: str,
    ):
        language = "en" if "EN" in qn_choice else "de"
        if "1" in qn_choice:
            q_idx = 1
        elif "2" in qn_choice:
            q_idx = 2
        elif "3" in qn_choice:
            q_idx = 3
        elif "4" in qn_choice:
            q_idx = 4
        elif "5" in qn_choice:
            q_idx = 5
        else:
            q_idx = 1

        file_path = f"data/{model_name}/{language}_pert_{perturber_name}_comp_{comparator_name}_exp_dto.json"
        with open(file_path, "r") as f:
            data = json.load(f)
        data = data[q_idx]
        explanation_dto = ExplanationDto.parse_obj(data)

        user_input = explanation_dto.input_text
        system_response = explanation_dto.output_text
        generator_vis = self.__visualize_explanations(
            user_input=user_input,
            system_response=system_response,
            generator_explanations=explanation_dto,
            upper_percentile=int(upper_percentile),
            middle_percentile=int(middle_percentile),
            lower_percentile=int(lower_percentile),
        )
        return user_input, system_response, generator_vis

    def __build_app_title(self):
        with gr.Row():
            with gr.Column(min_width=50, scale=1):
                gr.Image(
                    value=self.__logo_path,
                    width=50,
                    height=50,
                    show_download_button=False,
                    show_label=False,
                    show_share_button=False,
                    container=False,
                )
            with gr.Column(scale=2):
                Markdown(
                    f'<p style="text-align: left; font-size:200%; font-weight: bold"'
                    f">{self.__title}"
                    f"</p>"
                )

    def __build_chat_and_explain(self):
        with gr.Row():
            with gr.Column(scale=2):
                qn_choice = gr.Radio(
                    # placeholder="Type your question here and press Enter.",
                    label="Choose from these examples",
                    container=True,
                    choices=[
                        "EN Example 1",
                        "EN Example 2",
                        "EN Example 3",
                        "EN Example 4",
                        "EN Example 5",
                        "DE Example 1",
                        "DE Example 2",
                        "DE Example 3",
                        "DE Example 4",
                        "DE Example 5"
                    ],
                )

        with gr.Row():
            with gr.Column(scale=2):
                gr.Markdown(
                    value="**Note:** This is a demo version of the tool with "
                          "limited functionalities. For building the full "
                          "version, please visit [here](https://github.com/fraunhofer-iais/explainable-lms/tree/master).",
                )

        with gr.Row():
            with gr.Column(scale=2):
                user_input = gr.Textbox(
                    placeholder="Choose an example from the list above.",
                    label="Question",
                    container=True,
                    lines=10,
                    interactive=False
                )
            with gr.Column(scale=1):
                granularity = gr.Radio(
                    choices=[e for e in ExplanationGranularity],
                    value=ExplanationGranularity.SENTENCE_LEVEL,
                    label="Explanation Granularity",
                    interactive=False
                )

        with gr.Accordion(label="Settings", open=False, elem_id="accordion"):
            with gr.Row(variant="compact"):
                explainer_name = gr.Radio(
                    label="Explainer",
                    choices=list(EXPLAINERS.keys()),
                    value=list(EXPLAINERS.keys())[0],
                    container=True,
                    visible=False
                )
            with gr.Row(variant="compact"):
                upper_percentile = gr.Textbox(label="Upper", value="85", container=True)
                middle_percentile = gr.Textbox(
                    label="Middle", value="75", container=True
                )
                lower_percentile = gr.Textbox(label="Lower", value="10", container=True)

            with gr.Row(variant="compact"):
                model_name = gr.Radio(
                    label="Model",
                    choices=list(MODELS.keys()),
                    value=list(MODELS.keys())[0],
                    container=True,
                )
            with gr.Row(variant="compact"):
                perturber_name = gr.Radio(
                    label="Perturber",
                    choices=list(PERTURBERS.keys()),
                    value=list(PERTURBERS.keys())[0],
                    container=True,
                )
            with gr.Row(variant="compact"):
                comparator_name = gr.Radio(
                    label="Comparator",
                    choices=list(COMPARATORS.keys()),
                    value=list(COMPARATORS.keys())[0],
                    container=True,
                )
        with gr.Row(variant="compact"):
            # passing "elem_id" to use a custom style for the component
            # in the CSS passed.
            submit_btn = gr.Button(
                value="🛠 Submit",
                variant="secondary",
                elem_id="button",
                interactive=True,
            )

        with gr.Row():
            generator_vis = gr.HTML(label="Explanations")

        with gr.Row():
            system_response = gr.Textbox(
                label="System Response",
                container=True,
                interactive=False,
            )

        return (
            qn_choice,
            user_input,
            system_response,
            granularity,
            upper_percentile,
            middle_percentile,
            lower_percentile,
            explainer_name,
            model_name,
            perturber_name,
            comparator_name,
            generator_vis,
            submit_btn,
        )

    def __visualize_explanations(
        self,
        user_input: str,
        system_response: Optional[str],
        generator_explanations: ExplanationDto,
        upper_percentile: Optional[int],
        middle_percentile: Optional[int],
        lower_percentile: Optional[int],
    ) -> str:
        segregator = PercentileBasedSegregator(
            upper_bound_percentile=upper_percentile,
            middle_bound_percentile=middle_percentile,
            lower_bound_percentile=lower_percentile,
        )
        return self.__visualizer.visualize(
            segregator=segregator,
            explanations=generator_explanations,
            output_from_explanations=user_input,
        )