<jupyter_start><jupyter_text><jupyter_code>!git clone https://github.com/scott-deverinne/TextAttack.git
import sys
sys.path.insert(0,'/content/TextAttack')
cd TextAttack
pip install -e .[dev]
!pip3 install transformers
import csv
 
# field names 
fields = ['Number of successful attacks', 'Number of failed attacks',
          'Original accuracy', 'Accuracy under attack', 'Attack success rate',
          'Average perturbed word %', 'Average num. words per input']
    
# name of csv file 
filename = "/content/test_results.csv"
    
# writing to csv file 
with open(filename, 'w') as csvfile: 
    # creating a csv writer object 
    csvwriter = csv.writer(csvfile) 
        
    # writing the fields 
    csvwriter.writerow(fields)
import textattack
# Import the model
import transformers
from textattack.models.wrappers import HuggingFaceModelWrapper

model = transformers.AutoModelForSequenceClassification.from_pretrained("textattack/bert-base-uncased-ag-news")
tokenizer = transformers.AutoTokenizer.from_pretrained("textattack/bert-base-uncased-ag-news")

model_wrapper = HuggingFaceModelWrapper(model, tokenizer)

# Create the goal function using the model
from textattack.goal_functions import UntargetedClassification
goal_function = UntargetedClassification(model_wrapper)

# Import the dataset
from textattack.datasets import HuggingFaceDataset
dataset = HuggingFaceDataset("ag_news", None, "test")<jupyter_output><empty_output><jupyter_text>attack using textfooler<jupyter_code>from textattack.attack_recipes import TextFoolerJin2019
attack = TextFoolerJin2019.build(model_wrapper)
from tqdm import tqdm # tqdm provides us a nice progress bar.
from textattack.loggers import CSVLogger # tracks a dataframe for us.
from textattack.attack_results import SuccessfulAttackResult
from textattack import Attacker
from textattack import AttackArgs
from textattack.datasets import Dataset

attack_args = AttackArgs(num_examples=10)

attacker = Attacker(attack, dataset, attack_args)

attack_results = attacker.attack_dataset()

#The following legacy tutorial code shows how the Attack API works in detail.

#logger = CSVLogger(color_method='html')

#num_successes = 0
#i = 0
#while num_successes < 10:
    #result = next(results_iterable)
#    example, ground_truth_output = dataset[i]
#    i += 1
#    result = attack.attack(example, ground_truth_output)
#    if isinstance(result, SuccessfulAttackResult):
#        logger.log_attack_result(result)
#        num_successes += 1
#       print(f'{num_successes} of 10 successes complete.')
# open file for reading
with open('test_results.csv') as csvDataFile:

    # read file as csv file 
    csvReader = csv.reader(csvDataFile)

    # for every row, print the row
    for row in csvReader:
        print(row)<jupyter_output>['8', '2', '100.0', '20.0', '80.0', '18.27', '63.0']
{'fraction_non_alphanumeric': 0.09110169491525423, 'fraction_numerical': 0.013771186440677966, 'mean_word_length': 4.976793248945148, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '237757', 'license': 'MIT', 'n_tokens_mistral': 941, 'n_tokens_neox': 855, 'n_words': 238, 'path': 'Code_for_tuesday_presentation.ipynb', 'repo_name': 'scott-deverinne/TextAttack', 'size': '2832'}