Spaces:
Sleeping
Sleeping
File size: 926 Bytes
0ca8092 8dd18ce 0ca8092 8dd18ce 0ca8092 8dd18ce 0ca8092 8dd18ce 00cdc8d 320f116 720b38a 8dd18ce 00cdc8d 9d12e5b |
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 |
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()
|