OpenAttack
/
demo_deo.py
59 строк · 2.1 Кб
1import OpenAttack2import nltk3from nltk.sentiment.vader import SentimentIntensityAnalyzer4import numpy as np5import datasets6import transformers7
8def make_model():9class MyClassifier(OpenAttack.Classifier):10def __init__(self):11try:12self.model = SentimentIntensityAnalyzer()13except LookupError:14nltk.download('vader_lexicon')15self.model = SentimentIntensityAnalyzer()16
17def get_pred(self, input_):18return self.get_prob(input_).argmax(axis=1)19
20def get_prob(self, input_):21ret = []22for sent in input_:23res = self.model.polarity_scores(sent)24prob = (res["pos"] + 1e-6) / (res["neg"] + res["pos"] + 1e-6)25ret.append(np.array([1 - prob, prob]))26return np.array(ret)27return MyClassifier()28
29def dataset_mapping(x):30return {31"x": x["sentence"],32"y": 1 if x["label"] > 0.5 else 0,33}34
35def main():36print("New Attacker")37#attacker = OpenAttack.attackers.PWWSAttacker()38print("Build model")39#clsf = OpenAttack.loadVictim("BERT.SST")40clsf = OpenAttack.DataManager.loadVictim("BERT.SST")41#tokenizer = transformers.AutoTokenizer.from_pretrained("./data/Victim.BERT.SST")42#model = transformers.AutoModelForSequenceClassification.from_pretrained("./data/Victim.BERT.SST", num_labels=2, output_hidden_states=True)43#clsf = OpenAttack.classifiers.TransformersClassifier(model, tokenizer=tokenizer, max_length=100, embedding_layer=model.bert.embeddings.word_embeddings)44
45dataset = datasets.load_dataset("sst", split="train[:100]").map(function=dataset_mapping)46print("New Attacker")47attacker = OpenAttack.attackers.UATAttacker()48attacker.set_triggers(clsf, dataset)49print("Start attack")50attack_eval = OpenAttack.AttackEval( attacker, clsf, metrics=[51OpenAttack.metric.Fluency(),52OpenAttack.metric.GrammaticalErrors(),53OpenAttack.metric.EditDistance(),54OpenAttack.metric.ModificationRate()55] )56attack_eval.eval(dataset, visualize=True, progress_bar=True)57
58if __name__ == "__main__":59main()60