File size: 16,109 Bytes
4943752
 
 
 
 
 
 
 
 
 
 
 
ecdc8b8
 
 
 
 
 
 
 
d65ddc0
ecdc8b8
4943752
 
 
 
ecdc8b8
4943752
 
ecdc8b8
4943752
 
 
 
 
 
 
 
ecdc8b8
4943752
 
 
 
 
 
 
 
 
 
 
 
 
ecdc8b8
4943752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecdc8b8
 
 
4943752
ecdc8b8
 
 
4943752
 
ecdc8b8
4943752
 
 
 
 
ecdc8b8
 
 
 
 
04b0636
 
d65ddc0
4943752
 
04b0636
4943752
ecdc8b8
 
 
 
4943752
ecdc8b8
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
 
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
 
 
 
 
4943752
 
 
ecdc8b8
4943752
 
ecdc8b8
4943752
 
 
 
 
 
 
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
4943752
 
 
ecdc8b8
4943752
 
ecdc8b8
4943752
 
 
 
 
 
 
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
4943752
 
 
ecdc8b8
4943752
 
ecdc8b8
4943752
 
 
 
 
 
 
ecdc8b8
 
 
4943752
ecdc8b8
 
 
 
 
 
 
 
 
 
 
4943752
 
ecdc8b8
 
 
4943752
ecdc8b8
 
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
4943752
ecdc8b8
4943752
 
 
 
ecdc8b8
 
 
 
4943752
 
ecdc8b8
 
 
 
 
 
 
 
 
4943752
 
 
 
 
 
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
4943752
 
 
 
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65ddc0
04b0636
d65ddc0
ecdc8b8
 
 
 
 
 
 
 
 
 
d65ddc0
 
 
 
04b0636
d65ddc0
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
ecdc8b8
 
 
 
 
 
 
d65ddc0
 
 
ecdc8b8
 
d65ddc0
 
 
ecdc8b8
 
 
 
 
 
 
d65ddc0
ecdc8b8
 
 
 
 
 
 
 
 
d65ddc0
 
 
ecdc8b8
d65ddc0
 
 
ecdc8b8
d65ddc0
 
 
4943752
 
ecdc8b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4943752
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import os
import random
import zipfile
from difflib import Differ

import gradio as gr
import nltk
import pandas as pd
from findfile import find_files

from anonymous_demo import TADCheckpointManager
from textattack import Attacker
from textattack.attack_recipes import (
    BAEGarg2019,
    PWWSRen2019,
    TextFoolerJin2019,
    PSOZang2020,
    IGAWang2019,
    GeneticAlgorithmAlzantot2018,
    DeepWordBugGao2018,
    CLARE2020,
)
from textattack.attack_results import SuccessfulAttackResult
from textattack.datasets import Dataset
from textattack.models.wrappers import HuggingFaceModelWrapper

z = zipfile.ZipFile("checkpoints.zip", "r")
z.extractall(os.getcwd())


class ModelWrapper(HuggingFaceModelWrapper):
    def __init__(self, model):
        self.model = model  # pipeline = pipeline

    def __call__(self, text_inputs, **kwargs):
        outputs = []
        for text_input in text_inputs:
            raw_outputs = self.model.infer(text_input, print_result=False, **kwargs)
            outputs.append(raw_outputs["probs"])
        return outputs


class SentAttacker:
    def __init__(self, model, recipe_class=BAEGarg2019):
        model = model
        model_wrapper = ModelWrapper(model)

        recipe = recipe_class.build(model_wrapper)
        # WordNet defaults to english. Set the default language to French ('fra')

        # recipe.transformation.language = "en"

        _dataset = [("", 0)]
        _dataset = Dataset(_dataset)

        self.attacker = Attacker(recipe, _dataset)


def diff_texts(text1, text2):
    d = Differ()
    return [
        (token[2:], token[0] if token[0] != " " else None)
        for token in d.compare(text1, text2)
    ]


def get_ensembled_tad_results(results):
    target_dict = {}
    for r in results:
        target_dict[r["label"]] = (
            target_dict.get(r["label"]) + 1 if r["label"] in target_dict else 1
        )

    return dict(zip(target_dict.values(), target_dict.keys()))[
        max(target_dict.values())
    ]


nltk.download("omw-1.4")

sent_attackers = {}
tad_classifiers = {}

attack_recipes = {
    "bae": BAEGarg2019,
    "pwws": PWWSRen2019,
    "textfooler": TextFoolerJin2019,
    "pso": PSOZang2020,
    "iga": IGAWang2019,
    "ga": GeneticAlgorithmAlzantot2018,
    "deepwordbug": DeepWordBugGao2018,
    'clare': CLARE2020,
}

for attacker in ["pwws", "bae", "textfooler", "deepwordbug"]:
    for dataset in [
        "agnews10k",
        "amazon",
        "sst2",
        # 'imdb'
    ]:
        if "tad-{}".format(dataset) not in tad_classifiers:
            tad_classifiers[
                "tad-{}".format(dataset)
            ] = TADCheckpointManager.get_tad_text_classifier(
                "tad-{}".format(dataset).upper()
            )

        sent_attackers["tad-{}{}".format(dataset, attacker)] = SentAttacker(
            tad_classifiers["tad-{}".format(dataset)], attack_recipes[attacker]
        )
        tad_classifiers["tad-{}".format(dataset)].sent_attacker = sent_attackers[
            "tad-{}pwws".format(dataset)
        ]


def get_sst2_example():
    filter_key_words = [
        ".py",
        ".md",
        "readme",
        "log",
        "result",
        "zip",
        ".state_dict",
        ".model",
        ".png",
        "acc_",
        "f1_",
        ".origin",
        ".adv",
        ".csv",
    ]

    dataset_file = {"train": [], "test": [], "valid": []}
    dataset = "sst2"
    search_path = "./"
    task = "text_defense"
    dataset_file["test"] += find_files(
        search_path,
        [dataset, "test", task],
        exclude_key=[".adv", ".org", ".defense", ".inference", "train."]
        + filter_key_words,
    )

    for dat_type in ["test"]:
        data = []
        label_set = set()
        for data_file in dataset_file[dat_type]:
            with open(data_file, mode="r", encoding="utf8") as fin:
                lines = fin.readlines()
                for line in lines:
                    text, label = line.split("$LABEL$")
                    text = text.strip()
                    label = int(label.strip())
                    data.append((text, label))
                    label_set.add(label)
        return data[random.randint(0, len(data))]


def get_agnews_example():
    filter_key_words = [
        ".py",
        ".md",
        "readme",
        "log",
        "result",
        "zip",
        ".state_dict",
        ".model",
        ".png",
        "acc_",
        "f1_",
        ".origin",
        ".adv",
        ".csv",
    ]

    dataset_file = {"train": [], "test": [], "valid": []}
    dataset = "agnews"
    search_path = "./"
    task = "text_defense"
    dataset_file["test"] += find_files(
        search_path,
        [dataset, "test", task],
        exclude_key=[".adv", ".org", ".defense", ".inference", "train."]
        + filter_key_words,
    )
    for dat_type in ["test"]:
        data = []
        label_set = set()
        for data_file in dataset_file[dat_type]:
            with open(data_file, mode="r", encoding="utf8") as fin:
                lines = fin.readlines()
                for line in lines:
                    text, label = line.split("$LABEL$")
                    text = text.strip()
                    label = int(label.strip())
                    data.append((text, label))
                    label_set.add(label)
        return data[random.randint(0, len(data))]


def get_amazon_example():
    filter_key_words = [
        ".py",
        ".md",
        "readme",
        "log",
        "result",
        "zip",
        ".state_dict",
        ".model",
        ".png",
        "acc_",
        "f1_",
        ".origin",
        ".adv",
        ".csv",
    ]

    dataset_file = {"train": [], "test": [], "valid": []}
    dataset = "amazon"
    search_path = "./"
    task = "text_defense"
    dataset_file["test"] += find_files(
        search_path,
        [dataset, "test", task],
        exclude_key=[".adv", ".org", ".defense", ".inference", "train."]
        + filter_key_words,
    )

    for dat_type in ["test"]:
        data = []
        label_set = set()
        for data_file in dataset_file[dat_type]:
            with open(data_file, mode="r", encoding="utf8") as fin:
                lines = fin.readlines()
                for line in lines:
                    text, label = line.split("$LABEL$")
                    text = text.strip()
                    label = int(label.strip())
                    data.append((text, label))
                    label_set.add(label)
        return data[random.randint(0, len(data))]


def get_imdb_example():
    filter_key_words = [
        ".py",
        ".md",
        "readme",
        "log",
        "result",
        "zip",
        ".state_dict",
        ".model",
        ".png",
        "acc_",
        "f1_",
        ".origin",
        ".adv",
        ".csv",
    ]

    dataset_file = {"train": [], "test": [], "valid": []}
    dataset = "imdb"
    search_path = "./"
    task = "text_defense"
    dataset_file["test"] += find_files(
        search_path,
        [dataset, "test", task],
        exclude_key=[".adv", ".org", ".defense", ".inference", "train."]
        + filter_key_words,
    )

    for dat_type in ["test"]:
        data = []
        label_set = set()
        for data_file in dataset_file[dat_type]:
            with open(data_file, mode="r", encoding="utf8") as fin:
                lines = fin.readlines()
                for line in lines:
                    text, label = line.split("$LABEL$")
                    text = text.strip()
                    label = int(label.strip())
                    data.append((text, label))
                    label_set.add(label)
        return data[random.randint(0, len(data))]


cache = set()


def generate_adversarial_example(dataset, attacker, text=None, label=None):
    if not text or text in cache:
        if "agnews" in dataset.lower():
            text, label = get_agnews_example()
        elif "sst2" in dataset.lower():
            text, label = get_sst2_example()
        elif "amazon" in dataset.lower():
            text, label = get_amazon_example()
        elif "imdb" in dataset.lower():
            text, label = get_imdb_example()

    cache.add(text)

    result = None
    attack_result = sent_attackers[
        "tad-{}{}".format(dataset.lower(), attacker.lower())
    ].attacker.simple_attack(text, int(label))
    if isinstance(attack_result, SuccessfulAttackResult):
        if (
            attack_result.perturbed_result.output
            != attack_result.original_result.ground_truth_output
        ) and (
            attack_result.original_result.output
            == attack_result.original_result.ground_truth_output
        ):
            # with defense
            result = tad_classifiers["tad-{}".format(dataset.lower())].infer(
                attack_result.perturbed_result.attacked_text.text
                + "!ref!{},{},{}".format(
                    attack_result.original_result.ground_truth_output,
                    1,
                    attack_result.perturbed_result.output,
                ),
                print_result=True,
                defense="pwws",
            )

    if result:
        classification_df = {}
        classification_df["is_repaired"] = result["is_fixed"]
        classification_df["pred_label"] = result["label"]
        classification_df["confidence"] = round(result["confidence"], 3)
        classification_df["is_correct"] = result["ref_label_check"]

        advdetection_df = {}
        if result["is_adv_label"] != "0":
            advdetection_df["is_adversarial"] = {
                "0": False,
                "1": True,
                0: False,
                1: True,
            }[result["is_adv_label"]]
            advdetection_df["perturbed_label"] = result["perturbed_label"]
            advdetection_df["confidence"] = round(result["is_adv_confidence"], 3)
            # advdetection_df['ref_is_attack'] = result['ref_is_adv_label']
            # advdetection_df['is_correct'] = result['ref_is_adv_check']

    else:
        return generate_adversarial_example(dataset, attacker)

    return (
        text,
        label,
        result["restored_text"],
        result["label"],
        attack_result.perturbed_result.attacked_text.text,
        diff_texts(text, text),
        diff_texts(text, attack_result.perturbed_result.attacked_text.text),
        diff_texts(text, result["restored_text"]),
        attack_result.perturbed_result.output,
        pd.DataFrame(classification_df, index=[0]),
        pd.DataFrame(advdetection_df, index=[0]),
    )


demo = gr.Blocks()
with demo:
    gr.Markdown(
        "# <p align='center'>  Reactive Perturbation Defocusing for Textual Adversarial Defense </p> "
    )

    gr.Markdown("## <p align='center'>Clarifications</p>")
    gr.Markdown(
        "- This demo has no mechanism to ensure the adversarial example will be correctly repaired by RPD."
        " The repair success rate is actually the performance reported in the paper (approximately up to 97%.)"
    )
    gr.Markdown(
        "- The red (+) and green (-) colors in the character edition indicate the character is added "
        "or deleted in the adversarial example compared to the original input natural example."
    )
    gr.Markdown(
        "- The adversarial example and repaired adversarial example may be unnatural to read, "
        "while it is because the attackers usually generate unnatural perturbations."
        "RPD does not introduce additional unnatural perturbations."
    )
    gr.Markdown(
        "- To our best knowledge, Reactive Perturbation Defocusing is a novel approach in adversarial defense "
        ". RPD significantly (>10% defense accuracy improvement) outperforms the state-of-the-art methods."
    )
    gr.Markdown(
        "- The DeepWordBug is an unknown attacker to RPD's adversarial detector, which shows the robustness of RPD."
    )

    gr.Markdown("## <p align='center'>Natural Example Input</p>")
    with gr.Group():
        with gr.Row():
            input_dataset = gr.Radio(
                choices=["SST2", "AGNews10K", "Amazon"],
                value="SST2",
                label="Select a testing dataset and an adversarial attacker to generate an adversarial example.",
            )
            input_attacker = gr.Radio(
                choices=[
                    "BAE",
                    "PWWS",
                    "TextFooler",
                    "DeepWordBug"
                ],
                value="TextFooler",
                label="Choose an Adversarial Attacker for generating an adversarial example to attack the model.",
            )
        with gr.Group():
            with gr.Row():
                input_sentence = gr.Textbox(
                    placeholder="Input a natural example...",
                    label="Alternatively, input a natural example and its original label to generate an adversarial example.",
                )
                input_label = gr.Textbox(
                    placeholder="Original label...", label="Original Label"
                )

    button_gen = gr.Button(
        "Generate an adversarial example and repair using RPD (No GPU, Time:3-10 mins )",
        variant="primary",
    )

    gr.Markdown(
        "## <p align='center'>Generated Adversarial Example and Repaired Adversarial Example</p>"
    )
    with gr.Group():
        with gr.Column():
            with gr.Row():
                output_original_example = gr.Textbox(label="Original Example")
                output_original_label = gr.Textbox(label="Original Label")
            with gr.Row():
                output_adv_example = gr.Textbox(label="Adversarial Example")
                output_adv_label = gr.Textbox(label="Perturbed Label")
            with gr.Row():
                output_repaired_example = gr.Textbox(
                    label="Repaired Adversarial Example by RPD"
                )
                output_repaired_label = gr.Textbox(label="Repaired Label")

    gr.Markdown(
        "## <p align='center'>The Output of Reactive Perturbation Defocusing</p>"
    )
    with gr.Group():
        output_is_adv_df = gr.DataFrame(label="Adversarial Example Detection Result")
        gr.Markdown(
            "The is_adversarial field indicates an adversarial example is detected. "
            "The perturbed_label is the predicted label of the adversarial example. "
            "The confidence field represents the confidence of the predicted adversarial example detection. "
        )
        output_df = gr.DataFrame(label="Repaired Standard Classification Result")
        gr.Markdown(
            "If is_repaired=true, it has been repaired by RPD. "
            "The pred_label field indicates the standard classification result. "
            "The confidence field represents the confidence of the predicted label. "
            "The is_correct field indicates whether the predicted label is correct."
        )

    gr.Markdown("## <p align='center'>Example Comparisons</p>")
    ori_text_diff = gr.HighlightedText(
        label="The Original Natural Example",
        combine_adjacent=True,
    )
    adv_text_diff = gr.HighlightedText(
        label="Character Editions of Adversarial Example Compared to the Natural Example",
        combine_adjacent=True,
    )
    restored_text_diff = gr.HighlightedText(
        label="Character Editions of Repaired Adversarial Example Compared to the Natural Example",
        combine_adjacent=True,
    )

    # Bind functions to buttons
    button_gen.click(
        fn=generate_adversarial_example,
        inputs=[input_dataset, input_attacker, input_sentence, input_label],
        outputs=[
            output_original_example,
            output_original_label,
            output_repaired_example,
            output_repaired_label,
            output_adv_example,
            ori_text_diff,
            adv_text_diff,
            restored_text_diff,
            output_adv_label,
            output_df,
            output_is_adv_df,
        ],
    )

demo.launch()