Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,48 +2,27 @@ import gradio as gr
|
|
| 2 |
from textattack.attack_recipes import TextFoolerJin2019
|
| 3 |
from textattack.models.wrappers import HuggingFaceModelWrapper
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
-
import textattack
|
| 6 |
import torch
|
| 7 |
|
| 8 |
-
|
| 9 |
model_name = "textattack/distilbert-base-uncased-SST-2"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 12 |
|
| 13 |
-
|
| 14 |
model_wrapper = HuggingFaceModelWrapper(model, tokenizer)
|
| 15 |
|
| 16 |
-
|
| 17 |
attack = TextFoolerJin2019.build(model_wrapper)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
result = attack.attack(input_text, ground_truth_output=1)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# Check the status of the result
|
| 30 |
-
if result.goal_status == textattack.shared.AttackResultStatus.SUCCEEDED:
|
| 31 |
-
attack_info += f"Attack Succeeded!\nOriginal Text: {input_text}\nModified Text: {result.attacked_text.text}\n"
|
| 32 |
-
elif result.goal_status == textattack.shared.AttackResultStatus.SKIPPED:
|
| 33 |
-
skipped_info += f"Skipped Text: {result.attacked_text.text}\n"
|
| 34 |
-
|
| 35 |
-
# Format the output
|
| 36 |
-
output = f"TextAttack Results:\n{attack_info}\nSkipped Details:\n{skipped_info}"
|
| 37 |
-
print(output) # Debugging
|
| 38 |
-
return output
|
| 39 |
-
|
| 40 |
-
except Exception as e:
|
| 41 |
-
error_message = f"An error occurred: {str(e)}"
|
| 42 |
-
print(error_message) # Debugging
|
| 43 |
-
return error_message
|
| 44 |
-
|
| 45 |
-
# Gradio UI
|
| 46 |
-
gr.Interface(fn=run_attack,
|
| 47 |
-
inputs=gr.Textbox(lines=4, placeholder="Enter sentence to attack..."),
|
| 48 |
-
outputs="text",
|
| 49 |
-
title="TextAttack Demo on Hugging Face Model").launch()
|
|
|
|
| 2 |
from textattack.attack_recipes import TextFoolerJin2019
|
| 3 |
from textattack.models.wrappers import HuggingFaceModelWrapper
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
Load Hugging Face model (e.g., distilbert for demo)
|
| 8 |
model_name = "textattack/distilbert-base-uncased-SST-2"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 11 |
|
| 12 |
+
Wrap model for TextAttack
|
| 13 |
model_wrapper = HuggingFaceModelWrapper(model, tokenizer)
|
| 14 |
|
| 15 |
+
Load Attack
|
| 16 |
attack = TextFoolerJin2019.build(model_wrapper)
|
| 17 |
|
| 18 |
+
Function to run attack
|
| 19 |
+
def run_attack(input_text):
|
| 20 |
+
result = attack.attack(input_text, ground_truth_output=1)
|
| 21 |
+
return str(result)
|
|
|
|
| 22 |
|
| 23 |
+
Gradio UI
|
| 24 |
+
gr.Interface(fn=run_attack,
|
| 25 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter sentence to attack..."),
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="TextAttack Demo on Hugging Face Model").launch()
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|