# 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:
```bash
pip install iso-adverse
```
## Quickstart
Here’s a quick example of how to use IsoAdverse to train a BERT model with adversarial training:
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
from isoadverse.utils.model_loader import get_model_and_tokenizer
model, tokenizer = get_model_and_tokenizer(model_name='bert-base-uncased')
```
Raw data
{
"_id": null,
"home_page": "https://github.com/iso-ai/isoadverse",
"name": "iso-adverse",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Jazmia Henry",
"author_email": "isojaz@isoai.co",
"download_url": "https://files.pythonhosted.org/packages/ae/4e/287943f0cb87cab2bc593ed6a1f7dbec2dfd29c894a470fa55af8df849bc/iso-adverse-0.2.0.tar.gz",
"platform": null,
"description": "# IsoAdverse Documentation\n\n## Introduction\n\nWelcome 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.\n\n## Installation\n\nTo install the IsoAdverse package, you can use pip:\n\n```bash\npip install iso-adverse\n```\n\n## Quickstart\n\nHere\u2019s a quick example of how to use IsoAdverse to train a BERT model with adversarial training:\n\n```python\nimport torch\nfrom isoadverse.utils.data_loader import get_data_loader\nfrom isoadverse.utils.model_loader import get_model_and_tokenizer\n\n# Load data and model\ntexts = [\"This is a positive sentence.\", \"This is a negative sentence.\"]\nlabels = torch.tensor([1, 0])\ntrain_loader = get_data_loader(texts, labels, batch_size=2)\n\nmodel, tokenizer = get_model_and_tokenizer(model_name='bert-base-uncased')\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel.to(device)\n```\n\n## Attacks\n\nIsoAdverse implements several adversarial attacks on text data. Below are the details of each attack.\n\n### Fast Gradient Sign Method (FGSM)\n\nThe FGSM attack perturbs the input text by leveraging the gradients of the loss with respect to the input.\n\n```python\nfrom isoadverse.attacks.text_fgsm import text_fgsm_attack\nprint(\"Running FGSM Attack...\")\nperturbed_text = text_fgsm_attack(model, tokenizer, texts[0], torch.tensor([labels[0]]), epsilon=0.3)\nprint(\"Original Text:\", texts[0])\nprint(\"Perturbed Text:\", tokenizer.decode(perturbed_text[0]))\n```\n\n### Projected Gradient Descent (PGD)\n\nThe PGD attack is an iterative attack method that performs multiple steps of FGSM.\n\n```python\nfrom isoadverse.attacks.text_pgd import text_pgd_attack\n\nprint(\"\\nRunning PGD Attack...\")\nperturbed_ids = text_fgsm_attack(model, tokenizer, texts[0], torch.tensor([labels[0]]), epsilon=0.3)\nprint(\"Original Text:\", texts[0])\nprint(\"Perturbed Text:\", tokenizer.decode(perturbed_ids[0]))\n```\n\n### TextBugger\n\nTextBugger perturbs the text by introducing character-level changes.\n\n```python\nfrom isoadverse.attacks.textbugger import textbugger_attack\n\nprint(\"\\nRunning TextBugger Attack...\")\nperturbed_text = textbugger_attack(texts[0], num_bugs=5)\nprint(\"Original Text:\", texts[0])\nprint(\"Perturbed Text:\", perturbed_text)\n```\n\n### DeepWordBug\n\nDeepWordBug introduces word-level perturbations by modifying words in the text.\n\n```python\nfrom isoadverse.attacks.deepwordbug import deepwordbug_attack\n\nprint(\"\\nRunning DeepWordBug Attack...\")\nperturbed_text = deepwordbug_attack(texts[0], num_bugs=5)\nprint(\"Original Text:\", texts[0])\nprint(\"Perturbed Text:\", perturbed_text)\n```\n\n\n## Utilities\n\nIsoAdverse includes utility functions for loading data and models, making it easier to integrate into your existing workflow.\n\n### Data Loader\n\nThe data loader utility helps load and prepare text datasets for training and evaluation.\n\n```python\nfrom isoadverse.utils.data_loader import get_data_loader\n\ntrain_loader = get_data_loader(texts, labels, batch_size=2)\n```\n\n### Model Loader\n\nThe model loader utility provides pre-trained models and tokenizers.\n\n```python\nfrom isoadverse.utils.model_loader import get_model_and_tokenizer\n\nmodel, tokenizer = get_model_and_tokenizer(model_name='bert-base-uncased')\n```\n\n\n\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "This package is designed to simulate adversarial on pre-trained language models (pre-LLM models)",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/iso-ai/isoadverse"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f9505b73c65e2aacf1fa7549dcc0ff53897d52873909e33971fbbf5349133edf",
"md5": "ef99167e4e2b910014a3adb9f8c201a8",
"sha256": "3aa8a0f7d607e54fbf15af7dbaade7e611f8c1bbe1681c599deb19d8725c35f7"
},
"downloads": -1,
"filename": "iso_adverse-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ef99167e4e2b910014a3adb9f8c201a8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 12098,
"upload_time": "2024-08-02T04:32:34",
"upload_time_iso_8601": "2024-08-02T04:32:34.905759Z",
"url": "https://files.pythonhosted.org/packages/f9/50/5b73c65e2aacf1fa7549dcc0ff53897d52873909e33971fbbf5349133edf/iso_adverse-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ae4e287943f0cb87cab2bc593ed6a1f7dbec2dfd29c894a470fa55af8df849bc",
"md5": "1fba7b11338658a5db312694a372ada9",
"sha256": "2c3871d97eb4d412a66c40e978ee56ca3fbf22d9daf0613ed497e7074f94ae7a"
},
"downloads": -1,
"filename": "iso-adverse-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "1fba7b11338658a5db312694a372ada9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 8577,
"upload_time": "2024-08-02T04:32:36",
"upload_time_iso_8601": "2024-08-02T04:32:36.238445Z",
"url": "https://files.pythonhosted.org/packages/ae/4e/287943f0cb87cab2bc593ed6a1f7dbec2dfd29c894a470fa55af8df849bc/iso-adverse-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-02 04:32:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "iso-ai",
"github_project": "isoadverse",
"github_not_found": true,
"lcname": "iso-adverse"
}