Spaces:
Sleeping
Sleeping
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() |