### Clone and Install Detoxify from Source Source: https://github.com/unitaryai/detoxify/blob/master/README.md Instructions for cloning the repository and setting up the environment for development or training. ```bash # clone project git clone https://github.com/unitaryai/detoxify # create virtual env python3 -m venv toxic-env source toxic-env/bin/activate # install project pip install -e detoxify # or for training pip install -e 'detoxify[dev]' cd detoxify ``` -------------------------------- ### Install Detoxify via pip Source: https://github.com/unitaryai/detoxify/blob/master/README.md Standard installation command for the library. ```bash # install detoxify pip install detoxify ``` -------------------------------- ### Install Package Dependencies Source: https://github.com/unitaryai/detoxify/blob/master/CONTRIBUTING.md Installs the necessary Python packages for development and testing. Ensure you have activated a virtual environment before running these commands. ```sh pip install -r requirements.txt ``` ```sh pip install -r tests/requirements.txt ``` -------------------------------- ### Monitor Training with TensorBoard Source: https://github.com/unitaryai/detoxify/blob/master/README.md Launch TensorBoard to visualize training progress. Ensure the log directory is correctly specified. ```bash tensorboard --logdir=./saved ``` -------------------------------- ### Run Tests Source: https://github.com/unitaryai/detoxify/blob/master/CONTRIBUTING.md Executes the test suite for the project. This command may take approximately 90 seconds to complete. Warnings are suppressed. ```sh pytest tests --disable-pytest-warnings ``` -------------------------------- ### Initialize and Predict with Original Model Source: https://context7.com/unitaryai/detoxify/llms.txt Demonstrates initializing the 'original' Detoxify model and predicting toxicity scores for a given text. The 'original' model returns 6 toxicity categories. ```python from detoxify import Detoxify # Original model classes (6 categories) original = Detoxify('original') result = original.predict("test") print(result.keys()) # dict_keys(['toxicity', 'severe_toxicity', 'obscene', 'threat', 'insult', 'identity_attack']) ``` -------------------------------- ### Preprocess Jigsaw Data with Utilities Source: https://context7.com/unitaryai/detoxify/llms.txt Use these command-line utilities to combine test files with labels and create validation splits from training data. Specify input CSV files and desired operations. ```bash # Combine test.csv with test_labels.csv into a single file python preprocessing_utils.py \ --test_csv jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv \ --update_test # Create a validation set (10% of training data) python preprocessing_utils.py \ --val_csv jigsaw_data/jigsaw-toxic-comment-classification-challenge/train.csv \ --create_val_set ``` -------------------------------- ### Initialize Detoxify Models Source: https://context7.com/unitaryai/detoxify/llms.txt Load various pre-trained models by specifying the model type and optional device or configuration paths. ```python from detoxify import Detoxify # Load the original BERT-based model on CPU (default) model = Detoxify('original') # Load the unbiased RoBERTa-based model on GPU model_gpu = Detoxify('unbiased', device='cuda') # Load the multilingual XLM-RoBERTa model multilingual_model = Detoxify('multilingual', device='cuda:0') # Load lightweight Albert-based models for faster inference small_model = Detoxify('original-small') unbiased_small = Detoxify('unbiased-small') # Load from a custom checkpoint with offline Hugging Face config custom_model = Detoxify( model_type='original', checkpoint='/path/to/checkpoint.ckpt', device='cuda', huggingface_config_path='/path/to/hf_config/' ) ``` -------------------------------- ### Initialize and Predict with Multilingual Model Source: https://context7.com/unitaryai/detoxify/llms.txt Illustrates initializing the 'multilingual' Detoxify model and predicting toxicity. This model also returns 7 categories, similar to the 'unbiased' model. ```python # Multilingual model (same as unbiased - 7 categories) multilingual = Detoxify('multilingual') result = multilingual.predict("test") print(result.keys()) # dict_keys(['toxicity', 'severe_toxicity', 'obscene', 'threat', 'insult', 'identity_attack', 'sexual_explicit']) ``` -------------------------------- ### Download and Prepare Kaggle Datasets Source: https://github.com/unitaryai/detoxify/blob/master/README.md Download datasets for toxic comment classification challenges from Kaggle. Requires a Kaggle account and API token configured in ~/.kaggle/kaggle.json. This script combines test and label CSVs. ```bash # create data directory mkdir jigsaw_data cd jigsaw_data # download data kaggle competitions download -c jigsaw-toxic-comment-classification-challenge unzip jigsaw-toxic-comment-classification-challenge.zip -d jigsaw-toxic-comment-classification-challenge find jigsaw-toxic-comment-classification-challenge -name '*.csv.zip' | xargs -n1 unzip -d jigsaw-toxic-comment-classification-challenge kaggle competitions download -c jigsaw-unintended-bias-in-toxicity-classification unzip jigsaw-unintended-bias-in-toxicity-classification.zip -d jigsaw-unintended-bias-in-toxicity-classification kaggle competitions download -c jigsaw-multilingual-toxic-comment-classification unzip jigsaw-multilingual-toxic-comment-classification.zip -d jigsaw-multilingual-toxic-comment-classification ``` ```bash # combine test.csv and test_labels.csv python preprocessing_utils.py --test_csv jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv --update_test ``` ```bash python train.py --config configs/Toxic_comment_classification_BERT.json ``` ```bash python train.py --config configs/Unintended_bias_toxic_comment_classification_RoBERTa_combined.json ``` ```bash # combine test.csv and test_labels.csv python preprocessing_utils.py --test_csv jigsaw_data/jigsaw-multilingual-toxic-comment-classification/test.csv --update_test ``` ```bash python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json ``` -------------------------------- ### Initialize and Predict with Unbiased Model Source: https://context7.com/unitaryai/detoxify/llms.txt Shows how to initialize the 'unbiased' Detoxify model and predict toxicity. The 'unbiased' model returns 7 categories, including 'sexual_explicit'. ```python # Unbiased model classes (7 categories) unbiased = Detoxify('unbiased') result = unbiased.predict("test") print(result.keys()) # dict_keys(['toxicity', 'severe_toxicity', 'obscene', 'threat', 'insult', 'identity_attack', 'sexual_explicit']) ``` -------------------------------- ### Train custom toxicity classifiers Source: https://context7.com/unitaryai/detoxify/llms.txt Commands to download datasets, preprocess data, and execute training or monitoring using the train.py script. ```bash # Download Jigsaw datasets using Kaggle API mkdir jigsaw_data && cd jigsaw_data kaggle competitions download -c jigsaw-toxic-comment-classification-challenge unzip jigsaw-toxic-comment-classification-challenge.zip -d jigsaw-toxic-comment-classification-challenge kaggle competitions download -c jigsaw-unintended-bias-in-toxicity-classification unzip jigsaw-unintended-bias-in-toxicity-classification.zip -d jigsaw-unintended-bias-in-toxicity-classification kaggle competitions download -c jigsaw-multilingual-toxic-comment-classification unzip jigsaw-multilingual-toxic-comment-classification.zip -d jigsaw-multilingual-toxic-comment-classification # Preprocess test data cd .. python preprocessing_utils.py --test_csv jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv --update_test # Train original BERT model python train.py --config configs/Toxic_comment_classification_BERT.json # Train unbiased RoBERTa model python train.py --config configs/Unintended_bias_toxic_comment_classification_RoBERTa_combined.json # Train multilingual XLM-RoBERTa model python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json # Train with specific GPU(s) python train.py --config configs/Toxic_comment_classification_BERT.json --device 0,1 # Resume training from checkpoint python train.py --config configs/Toxic_comment_classification_BERT.json --resume /path/to/checkpoint.ckpt # Adjust number of epochs and workers python train.py --config configs/Toxic_comment_classification_BERT.json --n_epochs 50 --num_workers 8 # Monitor training with TensorBoard tensorboard --logdir=./saved ``` -------------------------------- ### Initialize and Use ToxicClassifier Source: https://context7.com/unitaryai/detoxify/llms.txt Instantiate the ToxicClassifier PyTorch Lightning module with a configuration file. Perform forward passes for predictions and use PyTorch Lightning Trainer for training. ```python from train import ToxicClassifier import pytorch_lightning as pl import json # Load configuration config = json.load(open('configs/Toxic_comment_classification_BERT.json')) # Initialize classifier model = ToxicClassifier(config) # Forward pass texts = ["This is a test comment", "Another comment here"] logits = model(texts) # Shape: (batch_size, num_classes) # The model handles tokenization internally # Training is managed by PyTorch Lightning Trainer trainer = pl.Trainer( devices='auto', max_epochs=10, accumulate_grad_batches=3, default_root_dir='saved/my_model', deterministic=True ) # Fit the model (requires DataLoaders) # trainer.fit(model, train_dataloaders=train_loader, val_dataloaders=val_loader) ``` -------------------------------- ### Run Predictions via Command Line Source: https://github.com/unitaryai/detoxify/blob/master/README.md Execute predictions using the run_prediction.py script, supporting model loading via torch.hub or local checkpoints and CSV output. ```bash # load model via torch.hub python run_prediction.py --input 'example' --model_name original # load model from from checkpoint path python run_prediction.py --input 'example' --from_ckpt_path model_path # save results to a .csv file python run_prediction.py --input test_set.txt --model_name original --save_to results.csv ``` -------------------------------- ### Load Models via PyTorch Hub Source: https://context7.com/unitaryai/detoxify/llms.txt Access underlying transformer models directly using PyTorch Hub for automatic weight management. ```python import torch ``` -------------------------------- ### Define training configuration Source: https://context7.com/unitaryai/detoxify/llms.txt JSON configuration file defining model architecture, dataset paths, and optimizer settings for the ToxicClassifier module. ```json { "name": "Jigsaw_BERT", "n_gpu": 1, "batch_size": 10, "accumulate_grad_batches": 3, "loss": "binary_cross_entropy", "arch": { "type": "BERT", "args": { "num_classes": 6, "model_type": "bert-base-uncased", "model_name": "BertForSequenceClassification", "tokenizer_name": "BertTokenizer" } }, "dataset": { "type": "JigsawDataOriginal", "args": { "train_csv_file": "jigsaw_data/jigsaw-toxic-comment-classification-challenge/train.csv", "test_csv_file": "jigsaw_data/jigsaw-toxic-comment-classification-challenge/val.csv", "add_test_labels": false, "classes": [ "toxicity", "severe_toxicity", "obscene", "threat", "insult", "identity_attack" ] } }, "optimizer": { "type": "Adam", "args": { "lr": 3e-5, "weight_decay": 3e-6, "amsgrad": true } } } ``` -------------------------------- ### Detoxify Class Initialization Source: https://context7.com/unitaryai/detoxify/llms.txt Initializes the Detoxify model interface to load pre-trained toxic comment classification models. ```APIDOC ## Detoxify Class Initialization ### Description Initializes the main interface for loading pre-trained toxic comment classification models. ### Parameters #### Request Body - **model_type** (string) - Required - The type of model to load (e.g., 'original', 'unbiased', 'multilingual', 'original-small', 'unbiased-small'). - **checkpoint** (string) - Optional - Path to a custom model checkpoint. - **device** (string) - Optional - Device specification for GPU/CPU allocation (e.g., 'cpu', 'cuda', 'cuda:0'). - **huggingface_config_path** (string) - Optional - Path to a Hugging Face config for offline loading. ### Request Example model = Detoxify('original', device='cuda') ``` -------------------------------- ### Load Jigsaw Datasets with PyTorch Source: https://context7.com/unitaryai/detoxify/llms.txt Utilize specialized PyTorch Dataset classes for Jigsaw challenges. Handles data loading, preprocessing, and label standardization. Configure classes and identity columns as needed. ```python from src.data_loaders import JigsawDataOriginal, JigsawDataBias, JigsawDataMultilingual from torch.utils.data import DataLoader # Load original Jigsaw challenge data original_dataset = JigsawDataOriginal( train_csv_file='jigsaw_data/jigsaw-toxic-comment-classification-challenge/train.csv', test_csv_file='jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv', train=True, add_test_labels=True, classes=['toxicity', 'severe_toxicity', 'obscene', 'threat', 'insult', 'identity_attack'] ) # Load unintended bias challenge data with identity columns bias_dataset = JigsawDataBias( train_csv_file='jigsaw_data/jigsaw-unintended-bias-in-toxicity-classification/train.csv', test_csv_file='jigsaw_data/jigsaw-unintended-bias-in-toxicity-classification/test.csv', train=True, compute_bias_weights=True, loss_weight=0.75, classes=['toxicity', 'severe_toxicity', 'obscene', 'threat', 'insult', 'identity_attack', 'sexual_explicit'], identity_classes=['male', 'female', 'homosexual_gay_or_lesbian', 'christian', 'jewish', 'muslim', 'black', 'white', 'psychiatric_or_mental_illness'] ) # Load multilingual challenge data multilingual_dataset = JigsawDataMultilingual( train_csv_file='jigsaw_data/multilingual_challenge/jigsaw-toxic-comment-train.csv', test_csv_file='jigsaw_data/multilingual_challenge/validation.csv', train=True, classes=['toxicity'] ) # Create DataLoader for training train_loader = DataLoader( original_dataset, batch_size=32, shuffle=True, num_workers=4, drop_last=True, pin_memory=True ) # Iterate through batches for text, meta in train_loader: # text: list of comment strings # meta: dict containing 'multi_target' tensor and 'text_id' targets = meta['multi_target'] # Shape: (batch_size, num_classes) break ``` -------------------------------- ### Preprocess Jigsaw Data with Python Functions Source: https://context7.com/unitaryai/detoxify/llms.txt Import and use Python functions for data preprocessing, including combining test CSVs with labels and generating validation sets. ```python from preprocessing_utils import update_test, create_val_set # Combine test and labels CSV files test_df = update_test('jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv') # Creates: jigsaw_data/jigsaw-toxic-comment-classification-challenge/test_updated.csv # Create validation split from training data create_val_set('jigsaw_data/jigsaw-toxic-comment-classification-challenge/train.csv', val_fraction=0.1) # Creates: jigsaw_data/jigsaw-toxic-comment-classification-challenge/val.csv ``` -------------------------------- ### Run toxicity predictions via CLI Source: https://context7.com/unitaryai/detoxify/llms.txt Use the run_prediction.py script to perform inference on text strings or files, with support for GPU acceleration and custom checkpoints. ```bash # Predict on a single text string python run_prediction.py --input "This is an example comment" --model_name original # Predict using the unbiased model python run_prediction.py --input "Another example text" --model_name unbiased # Predict on a text file containing multiple comments (one per line) python run_prediction.py --input comments.txt --model_name multilingual # Save predictions to CSV file python run_prediction.py --input comments.txt --model_name original --save_to results.csv # Use GPU for faster inference python run_prediction.py --input "Test comment" --model_name unbiased --device cuda # Load from a custom checkpoint python run_prediction.py --input "Test comment" --from_ckpt_path /path/to/checkpoint.ckpt ``` -------------------------------- ### Load Detoxify models via torch.hub Source: https://context7.com/unitaryai/detoxify/llms.txt Load various pre-trained toxicity models directly using PyTorch Hub. The returned objects are transformer models. ```python # Load models via torch.hub toxic_bert = torch.hub.load('unitaryai/detoxify', 'toxic_bert') unbiased_roberta = torch.hub.load('unitaryai/detoxify', 'unbiased_toxic_roberta') multilingual_xlm = torch.hub.load('unitaryai/detoxify', 'multilingual_toxic_xlm_r') # Load lightweight Albert models toxic_albert = torch.hub.load('unitaryai/detoxify', 'toxic_albert') unbiased_albert = torch.hub.load('unitaryai/detoxify', 'unbiased_albert') # Use the model directly (returns transformer model, not Detoxify wrapper) model = toxic_bert model.eval() ``` -------------------------------- ### Perform Toxicity Predictions with Python Source: https://github.com/unitaryai/detoxify/blob/master/README.md Use the Detoxify class to load models and predict toxicity for strings or lists of strings. Supports specifying hardware devices and optional pandas integration for formatting. ```python from detoxify import Detoxify # each model takes in either a string or a list of strings results = Detoxify('original').predict('example text') results = Detoxify('unbiased').predict(['example text 1','example text 2']) results = Detoxify('multilingual').predict(['example text','exemple de texte','texto de ejemplo','testo di esempio','texto de exemplo','örnek metin','пример текста']) # to specify the device the model will be allocated on (defaults to cpu), accepts any torch.device input model = Detoxify('original', device='cuda') # optional to display results nicely (will need to pip install pandas) import pandas as pd print(pd.DataFrame(results, index=input_text).round(5)) ``` -------------------------------- ### Evaluate Detoxify Models Source: https://context7.com/unitaryai/detoxify/llms.txt Use this script to evaluate trained Detoxify models on Jigsaw datasets. Specify configuration, checkpoint, and test data. Supports CPU evaluation. ```bash python model_eval/evaluate.py \ --config configs/Toxic_comment_classification_BERT.json \ --checkpoint saved/lightning_logs/checkpoints/model.ckpt \ --test_csv jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv ``` ```bash python model_eval/evaluate.py \ --config configs/Toxic_comment_classification_BERT.json \ --checkpoint saved/checkpoint.ckpt \ --test_csv test.csv \ --device cpu ``` ```bash python model_eval/evaluate.py \ --config configs/Unintended_bias_toxic_comment_classification_RoBERTa_combined.json \ --checkpoint saved/unbiased_checkpoint.ckpt \ --test_csv jigsaw_data/jigsaw-unintended-bias-in-toxicity-classification/test.csv ``` -------------------------------- ### Evaluate Detoxify Models Source: https://github.com/unitaryai/detoxify/blob/master/README.md Evaluate the performance of trained Detoxify models on test datasets. Specify the checkpoint path and the test CSV file. ```bash python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv ``` ```bash # to get the final bias metric python model_eval/compute_bias_metric.py ``` -------------------------------- ### Load Detoxify Model via PyTorch Hub Source: https://github.com/unitaryai/detoxify/blob/master/README.md Load a pre-trained Detoxify model using the PyTorch Hub API. Available models include 'toxic_bert', 'unbiased_toxic_roberta', and 'multilingual_toxic_xlm_r'. ```bash model = torch.hub.load('unitaryai/detoxify','toxic_bert') ``` -------------------------------- ### Compute Bias Metrics Source: https://context7.com/unitaryai/detoxify/llms.txt Script to compute the final bias metric after evaluating an unbiased model. ```bash python model_eval/compute_bias_metric.py saved/unbiased_checkpoint_results_test.csv.json ``` -------------------------------- ### Detoxify Project Citation Source: https://github.com/unitaryai/detoxify/blob/master/README.md BibTeX entry for citing the Detoxify project in academic work. ```bibtex @misc{Detoxify, title={Detoxify}, author={Hanu, Laura and {Unitary team}}, howpublished={Github. https://github.com/unitaryai/detoxify}, year={2020} } ``` -------------------------------- ### Perform Toxicity Predictions Source: https://context7.com/unitaryai/detoxify/llms.txt Use the predict method to classify text strings and retrieve probability scores for various toxicity categories. ```python from detoxify import Detoxify import pandas as pd # Initialize model model = Detoxify('original') # Single text prediction result = model.predict("You are a great person!") print(result) # Output: { # 'toxicity': 0.0001, # 'severe_toxicity': 0.00001, # 'obscene': 0.00005, # 'threat': 0.00002, # 'insult': 0.00008, # 'identity_attack': 0.00003 # } # Batch prediction with multiple texts texts = [ "Thanks for your help!", "You are an idiot", "I will find you and hurt you" ] results = model.predict(texts) # Display results as a DataFrame df = pd.DataFrame(results, index=texts).round(5) print(df) # toxicity severe_toxicity obscene threat insult identity_attack # Thanks for your help! 0.00012 0.00001 0.00003 0.00001 0.00005 0.00002 # You are an idiot 0.97521 0.00234 0.12453 0.00123 0.95234 0.01234 # I will find you and hurt you 0.98234 0.45123 0.02345 0.89234 0.12345 0.00234 ``` -------------------------------- ### Detoxify.predict() Source: https://context7.com/unitaryai/detoxify/llms.txt Performs toxicity classification on input text and returns probability scores. ```APIDOC ## Detoxify.predict() ### Description Performs toxicity classification on input text and returns a dictionary containing probability scores for each toxicity class. ### Parameters #### Request Body - **text** (string or list of strings) - Required - The text or list of texts to classify. ### Response #### Success Response (200) - **toxicity** (float) - Probability score for general toxicity. - **severe_toxicity** (float) - Probability score for severe toxicity. - **obscene** (float) - Probability score for obscenity. - **threat** (float) - Probability score for threats. - **insult** (float) - Probability score for insults. - **identity_attack** (float) - Probability score for identity-based hate. ### Response Example { "toxicity": 0.0001, "severe_toxicity": 0.00001, "obscene": 0.00005, "threat": 0.00002, "insult": 0.00008, "identity_attack": 0.00003 } ``` -------------------------------- ### Predict Toxicity with Detoxify Source: https://github.com/unitaryai/detoxify/blob/master/README.md Use the Detoxify library to predict toxicity scores for single texts or lists of texts. Supports 'original', 'unbiased', and 'multilingual' models. ```python from detoxify import Detoxify results = Detoxify('original').predict('some text') ``` ```python results = Detoxify('unbiased').predict(['example text 1','example text 2']) ``` ```python results = Detoxify('multilingual').predict(['example text','exemple de texte','texto de ejemplo','testo di esempio','texto de exemplo','örnek metin','пример текста']) ``` -------------------------------- ### Thresholding Toxicity Predictions Source: https://context7.com/unitaryai/detoxify/llms.txt Provides a function to check if a text is toxic based on a given threshold. It uses the 'original' model and returns True if any toxicity score meets or exceeds the threshold, False otherwise. ```python # Thresholding predictions def is_toxic(text, threshold=0.5): result = original.predict(text) return any(score >= threshold for score in result.values()) print(is_toxic("You are wonderful")) # False print(is_toxic("You are an idiot")) # True ``` -------------------------------- ### Display Prediction Results with Pandas Source: https://github.com/unitaryai/detoxify/blob/master/README.md Format and display the prediction results from Detoxify in a readable table using the pandas library. Results are rounded to 5 decimal places. ```python import pandas as pd print(pd.DataFrame(results,index=input_text).round(5)) ``` -------------------------------- ### Detect Multilingual Toxicity Source: https://context7.com/unitaryai/detoxify/llms.txt Use the multilingual model to classify text across seven supported languages. ```python from detoxify import Detoxify import pandas as pd # Initialize multilingual model model = Detoxify('multilingual', device='cuda') # Predict toxicity across multiple languages texts = [ "Thank you for your help", # English "Merci pour votre aide", # French "Gracias por tu ayuda", # Spanish "Grazie per il tuo aiuto", # Italian "Obrigado pela sua ajuda", # Portuguese "Yardımın için teşekkürler", # Turkish "Спасибо за вашу помощь" # Russian ] results = model.predict(texts) # Display results df = pd.DataFrame(results, index=texts).round(5) print(df) # Check for toxic content in French toxic_french = model.predict("Tu es vraiment stupide") print(f"French toxicity score: {toxic_french['toxicity']}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.