import gradio as gr from textattack.attack_recipes import TextFoolerJin2019 from textattack.models.wrappers import HuggingFaceModelWrapper from transformers import AutoTokenizer, AutoModelForSequenceClassification 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): result = attack.attack(input_text, ground_truth_output=1) return str(result) #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()