File size: 1,786 Bytes
0ca8092
 
 
 
720b38a
0ca8092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501a146
9d12e5b
 
 
720b38a
9d12e5b
 
 
 
501a146
 
 
 
 
0ca8092
9d12e5b
 
 
 
 
 
501a146
0ca8092
 
 
 
 
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
import gradio as gr
from textattack.attack_recipes import TextFoolerJin2019
from textattack.models.wrappers import HuggingFaceModelWrapper
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import textattack
import torch

# Load Hugging Face model (e.g., distilbert for demo)
model_name = "textattack/distilbert-base-uncased-SST-2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Wrap model for TextAttack
model_wrapper = HuggingFaceModelWrapper(model, tokenizer)

# Load Attack
attack = TextFoolerJin2019.build(model_wrapper)

# Function to run attack
def run_attack(input_text):
  def run_attack(input_text):
    try:
        # Attempt to modify the input text
        result = attack.attack(input_text, ground_truth_output=1)

        # Initialize a message to describe what happened
        attack_info = ""
        skipped_info = ""

        # Check the status of the result
        if result.goal_status == textattack.shared.AttackResultStatus.SUCCEEDED:
            attack_info += f"Attack Succeeded!\nOriginal Text: {input_text}\nModified Text: {result.attacked_text.text}\n"
        elif result.goal_status == textattack.shared.AttackResultStatus.SKIPPED:
            skipped_info += f"Skipped Text: {result.attacked_text.text}\n"

        # Format the output
        output = f"TextAttack Results:\n{attack_info}\nSkipped Details:\n{skipped_info}"
        return output
    
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Gradio UI
gr.Interface(fn=run_attack, 
             inputs=gr.Textbox(lines=4, placeholder="Enter sentence to attack..."), 
             outputs="text",
             title="TextAttack Demo on Hugging Face Model").launch()