TextAttack
TextAttack 🐙 is a Python framework for adversarial attacks, data augmentation, and model training in NLP https://textattack.readthedocs.io/en/master/
Install / Use
/learn @QData/TextAttackREADME
About
TextAttack is a Python framework for adversarial attacks, data augmentation, and model training in NLP.
If you're looking for information about TextAttack's menagerie of pre-trained models, you might want the TextAttack Model Zoo page.
Slack Channel
For help and realtime updates related to TextAttack, please join the TextAttack Slack!
Why TextAttack?
There are lots of reasons to use TextAttack:
- Understand NLP models better by running different adversarial attacks on them and examining the output
- Research and develop different NLP adversarial attacks using the TextAttack framework and library of components
- Augment your dataset to increase model generalization and robustness downstream
- Train NLP models using just a single command (all downloads included!)
Setup
Installation
You should be running Python 3.6+ to use this package. A CUDA-compatible GPU is optional but will greatly improve code speed. TextAttack is available through pip:
pip install textattack
Once TextAttack is installed, you can run it via command-line (textattack ...)
or via python module (python -m textattack ...).
Tip: TextAttack downloads files to
~/.cache/textattack/by default. This includes pretrained models, dataset samples, and the configuration fileconfig.yaml. To change the cache path, set the environment variableTA_CACHE_DIR. (for example:TA_CACHE_DIR=/tmp/ textattack attack ...).
Usage
Help: textattack --help
TextAttack's main features can all be accessed via the textattack command. Two very
common commands are textattack attack <args>, and textattack augment <args>. You can see more
information about all commands using
textattack --help
or a specific command using, for example,
textattack attack --help
The examples/ folder includes scripts showing common TextAttack usage for training models, running attacks, and augmenting a CSV file.
The documentation website contains walkthroughs explaining basic usage of TextAttack, including building a custom transformation and a custom constraint..
Running Attacks: textattack attack --help
The easiest way to try out an attack is via the command-line interface, textattack attack.
Tip: If your machine has multiple GPUs, you can distribute the attack across them using the
--paralleloption. For some attacks, this can really help performance. (If you want to attack Keras models in parallel, please check outexamples/attack/attack_keras_parallel.pyinstead)
Here are some concrete examples:
TextFooler on BERT trained on the MR sentiment classification dataset:
textattack attack --recipe textfooler --model bert-base-uncased-mr --num-examples 100
DeepWordBug on DistilBERT trained on the Quora Question Pairs paraphrase identification dataset:
textattack attack --model distilbert-base-uncased-cola --recipe deepwordbug --num-examples 100
Beam search with beam width 4 and word embedding transformation and untargeted goal function on an LSTM:
textattack attack --model lstm-mr --num-examples 20 \
--search-method beam-search^beam_width=4 --transformation word-swap-embedding \
--constraints repeat stopword max-words-perturbed^max_num_words=2 embedding^min_cos_sim=0.8 part-of-speech \
--goal-function untargeted-classification
Tip: Instead of specifying a dataset and number of examples, you can pass
--interactiveto attack samples inputted by the user.
Attacks and Papers Implemented ("Attack Recipes"): textattack attack --recipe [recipe_name]
We include attack recipes which implement attacks from the literature. You can list attack recipes using textattack list attack-recipes.
To run an attack recipe: textattack attack --recipe [recipe_name]
Related Skills
healthcheck
339.1kHost security hardening and risk-tolerance configuration for OpenClaw deployments
prose
339.1kOpenProse VM skill pack. Activate on any `prose` command, .prose files, or OpenProse mentions; orchestrates multi-agent workflows.
Writing Hookify Rules
83.8kThis skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns.
Agent Development
83.8kThis skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
