From 6176650b12c75f4bdc96e54832249f03c5985909 Mon Sep 17 00:00:00 2001 From: HENG Date: Thu, 31 Aug 2023 00:45:57 +0100 Subject: [PATCH] reformat --- examples/attack/attack_keras_parallel.py | 1 - textattack/attack_recipes/morpheus_tan_2020.py | 1 - textattack/attack_recipes/seq2sick_cheng_2018_blackbox.py | 1 - textattack/commands/augment_command.py | 1 - textattack/commands/eval_model_command.py | 2 +- textattack/constraints/overlap/max_words_perturbed.py | 1 - .../multilingual_universal_sentence_encoder.py | 4 ++-- .../universal_sentence_encoder/universal_sentence_encoder.py | 4 ++-- .../classification_goal_function_result.py | 1 - .../text_to_text_goal_function_result.py | 1 - textattack/loggers/weights_and_biases_logger.py | 1 - textattack/metrics/quality_metrics/perplexity.py | 1 - textattack/search_methods/greedy_word_swap_wir.py | 1 - textattack/shared/attacked_text.py | 1 + textattack/shared/validators.py | 5 ++++- textattack/trainer.py | 1 - textattack/training_args.py | 1 - .../transformations/word_swaps/word_swap_change_name.py | 1 - .../transformations/word_swaps/word_swap_change_number.py | 2 +- 19 files changed, 11 insertions(+), 20 deletions(-) diff --git a/examples/attack/attack_keras_parallel.py b/examples/attack/attack_keras_parallel.py index f05fcc2a5..617e08422 100644 --- a/examples/attack/attack_keras_parallel.py +++ b/examples/attack/attack_keras_parallel.py @@ -70,7 +70,6 @@ def __init__(self, model): self.model = model def __call__(self, text_input_list): - x_transform = [] for i, review in enumerate(text_input_list): tokens = [x.strip(",") for x in review.split()] diff --git a/textattack/attack_recipes/morpheus_tan_2020.py b/textattack/attack_recipes/morpheus_tan_2020.py index edf8ae790..b98360a53 100644 --- a/textattack/attack_recipes/morpheus_tan_2020.py +++ b/textattack/attack_recipes/morpheus_tan_2020.py @@ -27,7 +27,6 @@ class MorpheusTan2020(AttackRecipe): @staticmethod def build(model_wrapper): - # # Goal is to minimize BLEU score between the model output given for the # perturbed input sequence and the reference translation diff --git a/textattack/attack_recipes/seq2sick_cheng_2018_blackbox.py b/textattack/attack_recipes/seq2sick_cheng_2018_blackbox.py index de800c522..86b79aa23 100644 --- a/textattack/attack_recipes/seq2sick_cheng_2018_blackbox.py +++ b/textattack/attack_recipes/seq2sick_cheng_2018_blackbox.py @@ -31,7 +31,6 @@ class Seq2SickCheng2018BlackBox(AttackRecipe): @staticmethod def build(model_wrapper, goal_function="non_overlapping"): - # # Goal is non-overlapping output. # diff --git a/textattack/commands/augment_command.py b/textattack/commands/augment_command.py index 118fe0150..2883ded76 100644 --- a/textattack/commands/augment_command.py +++ b/textattack/commands/augment_command.py @@ -32,7 +32,6 @@ def run(self, args): args = textattack.AugmenterArgs(**vars(args)) if args.interactive: - print("\nRunning in interactive mode...\n") augmenter = eval(AUGMENTATION_RECIPE_NAMES[args.recipe])( pct_words_to_swap=args.pct_words_to_swap, diff --git a/textattack/commands/eval_model_command.py b/textattack/commands/eval_model_command.py index 16cbfd2fa..7957fbfee 100644 --- a/textattack/commands/eval_model_command.py +++ b/textattack/commands/eval_model_command.py @@ -56,7 +56,7 @@ def test_model_on_dataset(self, args): while i < min(args.num_examples, len(dataset)): dataset_batch = dataset[i : min(args.num_examples, i + args.batch_size)] batch_inputs = [] - for (text_input, ground_truth_output) in dataset_batch: + for text_input, ground_truth_output in dataset_batch: attacked_text = textattack.shared.AttackedText(text_input) batch_inputs.append(attacked_text.tokenizer_input) ground_truth_outputs.append(ground_truth_output) diff --git a/textattack/constraints/overlap/max_words_perturbed.py b/textattack/constraints/overlap/max_words_perturbed.py index b919978c9..8d09a4108 100644 --- a/textattack/constraints/overlap/max_words_perturbed.py +++ b/textattack/constraints/overlap/max_words_perturbed.py @@ -38,7 +38,6 @@ def __init__( self.max_percent = max_percent def _check_constraint(self, transformed_text, reference_text): - num_words_diff = len(transformed_text.all_words_diff(reference_text)) if self.max_percent: min_num_words = min(len(transformed_text.words), len(reference_text.words)) diff --git a/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/multilingual_universal_sentence_encoder.py b/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/multilingual_universal_sentence_encoder.py index 06dd84781..0249588ec 100644 --- a/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/multilingual_universal_sentence_encoder.py +++ b/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/multilingual_universal_sentence_encoder.py @@ -31,7 +31,7 @@ def __init__(self, threshold=0.8, large=False, metric="angular", **kwargs): try: self.model = hub.load(self._tfhub_url) except Exception as e: - print('Error loading model from tfhub, trying mirror url') + print("Error loading model from tfhub, trying mirror url") self.model = hub.load(self.mirror_tfhub_url) def encode(self, sentences): @@ -47,5 +47,5 @@ def __setstate__(self, state): try: self.model = hub.load(self._tfhub_url) except Exception as e: - print('Error loading model from tfhub, trying mirror url') + print("Error loading model from tfhub, trying mirror url") self.model = hub.load(self.mirror_tfhub_url) diff --git a/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/universal_sentence_encoder.py b/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/universal_sentence_encoder.py index 6b8327a7f..a1aae3377 100644 --- a/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/universal_sentence_encoder.py +++ b/textattack/constraints/semantics/sentence_encoders/universal_sentence_encoder/universal_sentence_encoder.py @@ -35,7 +35,7 @@ def encode(self, sentences): try: self.model = hub.load(self._tfhub_url) except Exception as e: - print('Error loading model from tfhub, trying mirror url') + print("Error loading model from tfhub, trying mirror url") self.model = hub.load(self.mirror_tfhub_url) return self.model(sentences).numpy() @@ -49,5 +49,5 @@ def __setstate__(self, state): try: self.model = hub.load(self._tfhub_url) except Exception as e: - print('Error loading model from tfhub, trying mirror url') + print("Error loading model from tfhub, trying mirror url") self.model = hub.load(self.mirror_tfhub_url) diff --git a/textattack/goal_function_results/classification_goal_function_result.py b/textattack/goal_function_results/classification_goal_function_result.py index 3a70ded8e..1b9aaf532 100644 --- a/textattack/goal_function_results/classification_goal_function_result.py +++ b/textattack/goal_function_results/classification_goal_function_result.py @@ -26,7 +26,6 @@ def __init__( num_queries, ground_truth_output, ): - super().__init__( attacked_text, raw_output, diff --git a/textattack/goal_function_results/text_to_text_goal_function_result.py b/textattack/goal_function_results/text_to_text_goal_function_result.py index eae8d91e5..c50e2c11f 100644 --- a/textattack/goal_function_results/text_to_text_goal_function_result.py +++ b/textattack/goal_function_results/text_to_text_goal_function_result.py @@ -23,7 +23,6 @@ def __init__( num_queries, ground_truth_output, ): - super().__init__( attacked_text, raw_output, diff --git a/textattack/loggers/weights_and_biases_logger.py b/textattack/loggers/weights_and_biases_logger.py index 6a8303117..7b9990421 100644 --- a/textattack/loggers/weights_and_biases_logger.py +++ b/textattack/loggers/weights_and_biases_logger.py @@ -13,7 +13,6 @@ class WeightsAndBiasesLogger(Logger): """Logs attack results to Weights & Biases.""" def __init__(self, **kwargs): - global wandb wandb = LazyLoader("wandb", globals(), "wandb") diff --git a/textattack/metrics/quality_metrics/perplexity.py b/textattack/metrics/quality_metrics/perplexity.py index e22175219..f1572591f 100644 --- a/textattack/metrics/quality_metrics/perplexity.py +++ b/textattack/metrics/quality_metrics/perplexity.py @@ -94,7 +94,6 @@ def calculate(self, results): return self.all_metrics def calc_ppl(self, texts): - with torch.no_grad(): text = " ".join(texts) eval_loss = [] diff --git a/textattack/search_methods/greedy_word_swap_wir.py b/textattack/search_methods/greedy_word_swap_wir.py index ac17fbf30..5721ce6b6 100644 --- a/textattack/search_methods/greedy_word_swap_wir.py +++ b/textattack/search_methods/greedy_word_swap_wir.py @@ -65,7 +65,6 @@ def _get_index_order(self, initial_text): # compute the largest change in score we can find by swapping each word delta_ps = [] for idx in indices_to_order: - # Exit Loop when search_over is True - but we need to make sure delta_ps # is the same size as softmax_saliency_scores if search_over: diff --git a/textattack/shared/attacked_text.py b/textattack/shared/attacked_text.py index 11d27bfb2..4616b467e 100644 --- a/textattack/shared/attacked_text.py +++ b/textattack/shared/attacked_text.py @@ -259,6 +259,7 @@ def ith_word_diff(self, other_attacked_text: AttackedText, i: int) -> bool: def words_diff_num(self, other_attacked_text: AttackedText) -> int: """The number of words different between two AttackedText objects.""" + # using edit distance to calculate words diff num def generate_tokens(words): result = {} diff --git a/textattack/shared/validators.py b/textattack/shared/validators.py index 4d9611d5a..fcf08e150 100644 --- a/textattack/shared/validators.py +++ b/textattack/shared/validators.py @@ -24,7 +24,10 @@ r"^textattack.models.helpers.word_cnn_for_classification.*", r"^transformers.modeling_\w*\.\w*ForSequenceClassification$", ], - (NonOverlappingOutput, MinimizeBleu,): [ + ( + NonOverlappingOutput, + MinimizeBleu, + ): [ r"^textattack.models.helpers.t5_for_text_to_text.*", ], } diff --git a/textattack/trainer.py b/textattack/trainer.py index 9c3198ae3..26d72d315 100644 --- a/textattack/trainer.py +++ b/textattack/trainer.py @@ -407,7 +407,6 @@ def collate_fn(data): is_adv_sample = [] for item in data: if "_example_type" in item[0].keys(): - # Get example type value from OrderedDict and remove it adv = item[0].pop("_example_type") diff --git a/textattack/training_args.py b/textattack/training_args.py index 6c5aa034d..c6e02c171 100644 --- a/textattack/training_args.py +++ b/textattack/training_args.py @@ -547,7 +547,6 @@ def _create_dataset_from_args(cls, args): train_dataset.output_column == "label" and eval_dataset.output_column == "label" ): - train_dataset_labels = train_dataset._dataset["label"] eval_dataset_labels = eval_dataset._dataset["label"] diff --git a/textattack/transformations/word_swaps/word_swap_change_name.py b/textattack/transformations/word_swaps/word_swap_change_name.py index d54b755a5..c4feeff48 100644 --- a/textattack/transformations/word_swaps/word_swap_change_name.py +++ b/textattack/transformations/word_swaps/word_swap_change_name.py @@ -64,7 +64,6 @@ def _get_transformations(self, current_text, indices_to_modify): return transformed_texts def _get_replacement_words(self, word, word_part_of_speech): - replacement_words = [] tag = word_part_of_speech if ( diff --git a/textattack/transformations/word_swaps/word_swap_change_number.py b/textattack/transformations/word_swaps/word_swap_change_number.py index 1ced0f84d..b885b6fa4 100644 --- a/textattack/transformations/word_swaps/word_swap_change_number.py +++ b/textattack/transformations/word_swaps/word_swap_change_number.py @@ -70,7 +70,7 @@ def _get_transformations(self, current_text, indices_to_modify): # replace original numbers with new numbers transformed_texts = [] - for (idx, word) in num_words: + for idx, word in num_words: replacement_words = self._get_new_number(word) for r in replacement_words: if r == word: