PFEemp2024 commited on
Commit
8aab5f3
1 Parent(s): d03ef4c

highlighting deleted/added words

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -20,6 +20,10 @@ from textattack.attack_recipes import (
20
  from textattack.attack_results import SuccessfulAttackResult
21
  from utils import SentAttacker, get_agnews_example, get_sst2_example, get_amazon_example, get_imdb_example, diff_texts
22
  # from utils import get_yahoo_example
 
 
 
 
23
 
24
  sent_attackers = {}
25
  tad_classifiers = {}
@@ -289,21 +293,9 @@ if __name__ == "__main__":
289
  gr.Markdown("""
290
  <p align='center'>The (+) and (-) in the boxes indicate the added and deleted characters in the adversarial example compared to the original input natural example.</p>
291
  """)
292
- ori_text_diff = gr.HighlightedText(
293
- label="The Original Natural Example",
294
- combine_adjacent=True,
295
- highlight_words=True,
296
- )
297
- adv_text_diff = gr.HighlightedText(
298
- label="Character Editions of Adversarial Example Compared to the Natural Example",
299
- combine_adjacent=True,
300
- highlight_words=True,
301
- )
302
- restored_text_diff = gr.HighlightedText(
303
- label="Character Editions of Repaired Adversarial Example Compared to the Natural Example",
304
- combine_adjacent=True,
305
- highlight_words=True,
306
- )
307
 
308
  gr.Markdown(
309
  "## <h2 align='center'>The Output of Reactive Perturbation Defocusing</p>"
@@ -333,22 +325,30 @@ if __name__ == "__main__":
333
 
334
  # Bind functions to buttons
335
  button_gen.click(
336
- fn=run_demo,
337
- inputs=[input_dataset, input_attacker, input_sentence, input_label],
338
- outputs=[
339
- output_original_example,
340
- output_original_label,
341
- output_repaired_example,
342
- output_repaired_label,
343
- output_adv_example,
344
- ori_text_diff,
345
- adv_text_diff,
346
- restored_text_diff,
347
- output_adv_label,
348
- output_df,
349
- output_is_adv_df,
350
- msg_text
351
- ],
352
- )
 
 
 
 
 
 
 
 
353
 
354
  demo.queue(2).launch()
 
20
  from textattack.attack_results import SuccessfulAttackResult
21
  from utils import SentAttacker, get_agnews_example, get_sst2_example, get_amazon_example, get_imdb_example, diff_texts
22
  # from utils import get_yahoo_example
23
+ import difflib
24
+ def render_diff(text1, text2):
25
+ diff = difflib.ndiff(text1.splitlines(keepends=True), text2.splitlines(keepends=True))
26
+ return ''.join([line.replace(' ', '&nbsp;').replace('\n', '<br>') for line in diff])
27
 
28
  sent_attackers = {}
29
  tad_classifiers = {}
 
293
  gr.Markdown("""
294
  <p align='center'>The (+) and (-) in the boxes indicate the added and deleted characters in the adversarial example compared to the original input natural example.</p>
295
  """)
296
+ ori_text_diff = gr.HTML(label="The Original Natural Example")
297
+ adv_text_diff = gr.HTML(label="Character Editions of Adversarial Example Compared to the Natural Example")
298
+ restored_text_diff = gr.HTML(label="Character Editions of Repaired Adversarial Example Compared to the Natural Example")
 
 
 
 
 
 
 
 
 
 
 
 
299
 
300
  gr.Markdown(
301
  "## <h2 align='center'>The Output of Reactive Perturbation Defocusing</p>"
 
325
 
326
  # Bind functions to buttons
327
  button_gen.click(
328
+ fn=run_demo,
329
+ inputs=[input_dataset, input_attacker, input_sentence, input_label],
330
+ outputs=[
331
+ output_original_example,
332
+ output_original_label,
333
+ output_repaired_example,
334
+ output_repaired_label,
335
+ output_adv_example,
336
+ ori_text_diff,
337
+ adv_text_diff,
338
+ restored_text_diff,
339
+ output_adv_label,
340
+ output_df,
341
+ output_is_adv_df,
342
+ msg_text
343
+ ],
344
+ _js="""
345
+ (original_example, original_label, repaired_example, repaired_label, adv_example, ori_diff, adv_diff, restored_diff, adv_label, df, is_adv_df, msg) => {
346
+ ori_diff.value = render_diff(original_example, original_example);
347
+ adv_diff.value = render_diff(original_example, adv_example);
348
+ restored_diff.value = render_diff(original_example, repaired_example);
349
+ return [original_example, original_label, repaired_example, repaired_label, adv_example, ori_diff, adv_diff, restored_diff, adv_label, df, is_adv_df, msg]
350
+ }
351
+ """
352
+ )
353
 
354
  demo.queue(2).launch()