File size: 8,199 Bytes
c33448f
 
 
 
ae8fa3a
 
c33448f
 
14bc302
c33448f
 
 
b6dea97
c33448f
b6dea97
c33448f
87119af
b6dea97
14bc302
87119af
 
14bc302
87119af
 
c33448f
 
 
 
 
 
b6dea97
c33448f
 
 
 
92dd0b7
 
680ce56
 
 
 
 
14bc302
 
680ce56
c33448f
 
b6dea97
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6dea97
c33448f
14bc302
c33448f
 
b6dea97
c33448f
 
 
 
 
14bc302
 
 
c33448f
14bc302
 
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14bc302
 
c33448f
 
 
 
87119af
c33448f
 
14bc302
 
 
 
 
 
 
 
 
 
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
6ce8c6e
 
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
14bc302
 
 
c33448f
 
 
14bc302
c33448f
 
14bc302
c33448f
 
14bc302
 
088f179
 
 
 
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14bc302
c33448f
 
 
 
 
 
 
 
 
 
 
14bc302
 
 
 
 
 
 
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14bc302
c33448f
 
 
 
14bc302
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14bc302
 
 
 
 
 
 
 
 
c33448f
 
 
 
 
 
 
 
b6dea97
c33448f
 
 
 
 
 
 
 
 
 
 
 
b6dea97
c33448f
 
 
 
 
 
 
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
281
282
283
284
import uuid

import gradio as gr

from io_utils import read_scanners, write_scanners
from text_classification_ui_helpers import (
    get_related_datasets_from_leaderboard,
    align_columns_and_show_prediction,
    get_dataset_splits,
    check_dataset,
    precheck_model_ds_enable_example_btn,
    try_submit,
    empty_column_mapping,
    write_column_mapping_to_config,
    enable_run_btn,
)

import logging
from wordings import (
  CONFIRM_MAPPING_DETAILS_MD, 
  INTRODUCTION_MD, 
  LOG_IN_TIPS, 
  CHECK_LOG_SECTION_RAW,
)

MAX_LABELS = 40
MAX_FEATURES = 20

EXAMPLE_MODEL_ID = "cardiffnlp/twitter-roberta-base-sentiment-latest"
CONFIG_PATH = "./config.yaml"
logger = logging.getLogger(__name__)

def get_demo():
    with gr.Row():
        gr.Markdown(INTRODUCTION_MD)

    with gr.Row(visible=False):
        uid_label = gr.Textbox(
            label="Evaluation ID:", value=uuid.uuid4, visible=False, interactive=False
        )

    with gr.Accordion(label="Log In", open=True):
        gr.HTML(LOG_IN_TIPS)
        gr.LoginButton()

    with gr.Row():
        model_id_input = gr.Textbox(
            label="Hugging Face Model id",
            placeholder=EXAMPLE_MODEL_ID + " (press enter to confirm)",
        )

        with gr.Column():
            dataset_id_input = gr.Dropdown(
                choices=[],
                value="",
                allow_custom_value=True,
                label="Hugging Face Dataset id",
            )

    with gr.Row():
        dataset_config_input = gr.Dropdown(label="Dataset Config", visible=False, allow_custom_value=True)
        dataset_split_input = gr.Dropdown(label="Dataset Split", visible=False, allow_custom_value=True)

    with gr.Row():
        first_line_ds = gr.DataFrame(label="Dataset Preview", visible=False)
    with gr.Row():
        loading_dataset_info = gr.HTML(visible=True)
    with gr.Row():
        example_btn = gr.Button(
            "Validate Model & Dataset",
            visible=True,
            variant="primary",
            interactive=False,
        )
    with gr.Row():
        loading_validation = gr.HTML(visible=True)
    with gr.Row():
        validation_result = gr.HTML(visible=False)
    with gr.Row():
        example_input = gr.Textbox(label="Example Input", visible=False, interactive=False)
        example_prediction = gr.Label(label="Model Sample Prediction", visible=False)

    with gr.Row():
        with gr.Accordion(
            label="Label and Feature Mapping", visible=False, open=False
        ) as column_mapping_accordion:
            with gr.Row():
                gr.Markdown(CONFIRM_MAPPING_DETAILS_MD)
            column_mappings = []
            with gr.Row():
                with gr.Column():
                    gr.Markdown("# Label Mapping")
                    for _ in range(MAX_LABELS):
                        column_mappings.append(gr.Dropdown(visible=False))
                with gr.Column():
                    gr.Markdown("# Feature Mapping")
                    for _ in range(MAX_LABELS, MAX_LABELS + MAX_FEATURES):
                        column_mappings.append(gr.Dropdown(visible=False))

    with gr.Accordion(label="Scanner Advanced Config (optional)", open=False):
        scanners = gr.CheckboxGroup(visible=True)

        @gr.on(triggers=[uid_label.change], inputs=[uid_label], outputs=[scanners])
        def get_scanners(uid):
            selected = read_scanners(uid)
            # we remove data_leakage from the default scanners
            # Reason: data_leakage barely raises any issues and takes too many requests
            # when using inference API, causing rate limit error
            scan_config = [
                "ethical_bias", 
                "text_perturbation", 
                "robustness",
                "performance",
                "underconfidence",
                "overconfidence",
                "spurious_correlation",
                "data_leakage",
                ]
            return gr.update(
                choices=scan_config, value=selected, label="Scan Settings", visible=True
            )

    with gr.Row():
        run_btn = gr.Button(
            "Get Evaluation Result",
            variant="primary",
            interactive=False,
            size="lg",
        )

    with gr.Row():
        logs = gr.Textbox(
            value=CHECK_LOG_SECTION_RAW,
            label="Giskard Bot Evaluation Guide:",
            visible=False,
            every=0.5,
        )

    
    scanners.change(write_scanners, inputs=[scanners, uid_label])

    gr.on(
        triggers=[model_id_input.change],
        fn=get_related_datasets_from_leaderboard,
        inputs=[model_id_input],
        outputs=[dataset_id_input],
    ).then(
        fn=check_dataset,
        inputs=[dataset_id_input],
        outputs=[dataset_config_input, dataset_split_input, loading_dataset_info],
    )
    
    gr.on(
        triggers=[dataset_id_input.input, dataset_id_input.select],
        fn=check_dataset,
        inputs=[dataset_id_input],
        outputs=[dataset_config_input, dataset_split_input, loading_dataset_info]
    )

    dataset_config_input.change(fn=get_dataset_splits, inputs=[dataset_id_input, dataset_config_input], outputs=[dataset_split_input])

    gr.on(
        triggers=[model_id_input.change, dataset_id_input.change, dataset_config_input.change],
        fn=empty_column_mapping,
        inputs=[uid_label]
    )

    gr.on(
        triggers=[label.change for label in column_mappings],
        fn=write_column_mapping_to_config,
        inputs=[
            uid_label,
            *column_mappings,
        ],
    )

    # label.change sometimes does not pass the changed value
    gr.on(
        triggers=[label.input for label in column_mappings],
        fn=write_column_mapping_to_config,
        inputs=[
            uid_label,
            *column_mappings,
        ],
    )

    gr.on(
        triggers=[
            model_id_input.change,
            model_id_input.input,
            dataset_id_input.change,
            dataset_config_input.change,
            dataset_split_input.change,
        ],
        fn=precheck_model_ds_enable_example_btn,
        inputs=[
            model_id_input,
            dataset_id_input,
            dataset_config_input,
            dataset_split_input,
        ],
        outputs=[
            example_btn, 
            first_line_ds,
            validation_result,
            example_input,
            example_prediction,
            column_mapping_accordion,],
    )

    gr.on(
        triggers=[
            example_btn.click,
        ],
        fn=align_columns_and_show_prediction,
        inputs=[
            model_id_input,
            dataset_id_input,
            dataset_config_input,
            dataset_split_input,
            uid_label,
        ],
        outputs=[
            validation_result,
            example_input,
            example_prediction,
            column_mapping_accordion,
            run_btn,
            loading_validation,
            *column_mappings,
        ],
    )

    gr.on(
        triggers=[
            run_btn.click,
        ],
        fn=try_submit,
        inputs=[
            model_id_input,
            dataset_id_input,
            dataset_config_input,
            dataset_split_input,
            uid_label,
        ],
        outputs=[
            run_btn, 
            logs, 
            uid_label,            
            validation_result,
            example_input,
            example_prediction,
            column_mapping_accordion,
          ],
    )

    gr.on(
        triggers=[
            scanners.input,
        ],
        fn=enable_run_btn,
        inputs=[
            uid_label,
            model_id_input, 
            dataset_id_input, 
            dataset_config_input, 
            dataset_split_input
        ],
        outputs=[run_btn],
    )

    gr.on(
        triggers=[label.input for label in column_mappings],
        fn=enable_run_btn,
        inputs=[
            uid_label,
            model_id_input, 
            dataset_id_input, 
            dataset_config_input, 
            dataset_split_input
        ],  # FIXME
        outputs=[run_btn],
    )