IsoAdverse Documentation
Introduction
Welcome to IsoAdverse, a Python package designed to simulate adversarial on pre-trained language models (pre-LLM models). This package implements a range of attacks as described in recent research to help secure your AI Agents and LLMs.
Installation
To install the IsoAdverse package, you can use pip:
pip install iso-adverse
Quickstart
Here’s a quick example of how to use IsoAdverse to train a BERT model with adversarial training:
import torch
from isoadverse.utils.data_loader import get_data_loader
from isoadverse.utils.model_loader import get_model_and_tokenizer
# Load data and model
texts = ["This is a positive sentence.", "This is a negative sentence."]
labels = torch.tensor([1, 0])
train_loader = get_data_loader(texts, labels, batch_size=2)
model, tokenizer = get_model_and_tokenizer(model_name='bert-base-uncased')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
Attacks
IsoAdverse implements several adversarial attacks on text data. Below are the details of each attack.
Fast Gradient Sign Method (FGSM)
The FGSM attack perturbs the input text by leveraging the gradients of the loss with respect to the input.
from isoadverse.attacks.text_fgsm import text_fgsm_attack
print("Running FGSM Attack...")
perturbed_text = text_fgsm_attack(model, tokenizer, texts[0], torch.tensor([labels[0]]), epsilon=0.3)
print("Original Text:", texts[0])
print("Perturbed Text:", tokenizer.decode(perturbed_text[0]))
Projected Gradient Descent (PGD)
The PGD attack is an iterative attack method that performs multiple steps of FGSM.
from isoadverse.attacks.text_pgd import text_pgd_attack
print("\nRunning PGD Attack...")
perturbed_ids = text_fgsm_attack(model, tokenizer, texts[0], torch.tensor([labels[0]]), epsilon=0.3)
print("Original Text:", texts[0])
print("Perturbed Text:", tokenizer.decode(perturbed_ids[0]))
TextBugger
TextBugger perturbs the text by introducing character-level changes.
from isoadverse.attacks.textbugger import textbugger_attack
print("\nRunning TextBugger Attack...")
perturbed_text = textbugger_attack(texts[0], num_bugs=5)
print("Original Text:", texts[0])
print("Perturbed Text:", perturbed_text)
DeepWordBug
DeepWordBug introduces word-level perturbations by modifying words in the text.
from isoadverse.attacks.deepwordbug import deepwordbug_attack
print("\nRunning DeepWordBug Attack...")
perturbed_text = deepwordbug_attack(texts[0], num_bugs=5)
print("Original Text:", texts[0])
print("Perturbed Text:", perturbed_text)
Utilities
IsoAdverse includes utility functions for loading data and models, making it easier to integrate into your existing workflow.
Data Loader
The data loader utility helps load and prepare text datasets for training and evaluation.
from isoadverse.utils.data_loader import get_data_loader
train_loader = get_data_loader(texts, labels, batch_size=2)
Model Loader
The model loader utility provides pre-trained models and tokenizers.
from isoadverse.utils.model_loader import get_model_and_tokenizer
model, tokenizer = get_model_and_tokenizer(model_name='bert-base-uncased')
Metadata
Release files for iso-adverse 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| iso-adverse-0.2.0.tar.gz | 8.6 kB | Without provenance | Details for iso-adverse-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| iso_adverse-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for iso_adverse-0.2.0-py3-none-any.whl |
Total release size: 20.7 kB
Release files / iso-adverse-0.2.0.tar.gz
| Download URL | iso-adverse-0.2.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c3871d97eb4d412a66c40e978ee56ca3fbf22d9daf0613ed497e7074f94ae7a
|
|
BLAKE2b-256 checksum How to use checksums |
ae4e287943f0cb87cab2bc593ed6a1f7dbec2dfd29c894a470fa55af8df849bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.8.8
|
Release files / iso_adverse-0.2.0-py3-none-any.whl
| Download URL | iso_adverse-0.2.0-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3aa8a0f7d607e54fbf15af7dbaade7e611f8c1bbe1681c599deb19d8725c35f7
|
|
BLAKE2b-256 checksum How to use checksums |
f9505b73c65e2aacf1fa7549dcc0ff53897d52873909e33971fbbf5349133edf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.8.8
|
Security for iso-adverse 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.