Spaces:
Runtime error
Runtime error
Commit
·
0d26b1d
1
Parent(s):
1e91478
highlighting delted words
Browse files
app.py
CHANGED
|
@@ -20,17 +20,6 @@ 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 |
-
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(' ', ' ').replace('\n', '<br>') for line in diff])
|
| 27 |
-
|
| 28 |
-
def update_diff(original_example, repaired_example, adv_example):
|
| 29 |
-
ori_diff = render_diff(original_example, original_example)
|
| 30 |
-
adv_diff = render_diff(original_example, adv_example)
|
| 31 |
-
restored_diff = render_diff(original_example, repaired_example)
|
| 32 |
-
return ori_diff, adv_diff, restored_diff
|
| 33 |
-
|
| 34 |
|
| 35 |
sent_attackers = {}
|
| 36 |
tad_classifiers = {}
|
|
@@ -300,9 +289,21 @@ if __name__ == "__main__":
|
|
| 300 |
gr.Markdown("""
|
| 301 |
<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>
|
| 302 |
""")
|
| 303 |
-
ori_text_diff = gr.
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
gr.Markdown(
|
| 308 |
"## <h2 align='center'>The Output of Reactive Perturbation Defocusing</p>"
|
|
@@ -332,28 +333,22 @@ if __name__ == "__main__":
|
|
| 332 |
|
| 333 |
# Bind functions to buttons
|
| 334 |
button_gen.click(
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
(original_example, original_label, repaired_example, repaired_label, adv_example, ori_diff, adv_diff, restored_diff, adv_label, df, is_adv_df, msg) => {
|
| 353 |
-
return [original_example, original_label, repaired_example, repaired_label, adv_example, ori_diff, adv_diff, restored_diff, adv_label, df, is_adv_df, msg]
|
| 354 |
-
}
|
| 355 |
-
""",
|
| 356 |
-
_fn=update_diff
|
| 357 |
-
)
|
| 358 |
|
| 359 |
-
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 |
|
| 24 |
sent_attackers = {}
|
| 25 |
tad_classifiers = {}
|
|
|
|
| 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=False,
|
| 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=False,
|
| 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=False,
|
| 305 |
+
highlight_words=True,
|
| 306 |
+
)
|
| 307 |
|
| 308 |
gr.Markdown(
|
| 309 |
"## <h2 align='center'>The Output of Reactive Perturbation Defocusing</p>"
|
|
|
|
| 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()
|