detoxify

repository·master·Indexed 23 days ago

https://github.com/unitaryai/detoxify

A Python library for detecting toxic comments using PyTorch Lightning and Hugging Face Transformers. It provides pre-trained models for English and multilingual toxicity classification, including versions optimized to minimize unintended bias. Supported models include BERT, RoBERTa, XLM-RoBERTa, and ALBERT. The library features a Detoxify class for predictions, a CLI for command-line usage, and support for loading models via Torch Hub.

Tokens
3K
Snippets
11
Records
19
Agent score
79%

What's inside detoxify

  1. Train the Toxic Comment Classification model

    master

    To train a model for the Toxic Comment Classification challenge, first combine the test data and labels using preprocessing_utils.py, then run the training script with the BERT configuration.

    1. Combine test files:

    python preprocessing_utils.py --test_csv <path_to_test_csv> --update_test 2. Start training: python train.py --config configs/Toxic_comment_classification_BERT.json

    # combine test.csv and test_labels.csv
    python preprocessing_utils.py --test_csv jigsaw_data/jigsaw-toxic-comment-classification-challenge/test.csv --update_test
    
    python train.py --config configs/Toxic_comment_classification_BERT.json
  2. Evaluate the Unintended Bias in Toxicity model

    master

    To evaluate a model for the Unintended Bias challenge, use evaluate.py to get AUC scores, and then run the bias metric script to calculate the final specialized bias metric.

    1. Run evaluation:

    python evaluate.py --checkpoint <path_to_checkpoint> --test_csv <path_to_test_csv> 2. Compute final bias metric: python model_eval/compute_bias_metric.py

    python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
    
    # to get the final bias metric
    python model_eval/compute_bias_metric.py
  3. Train the Unintended Bias in Toxicity model

    master

    To train a model specifically for the Unintended Bias in Toxicity challenge, use the RoBERTa combined configuration.

    python train.py --config configs/Unintended_bias_toxic_comment_classification_RoBERTa_combined.json

    python train.py --config configs/Unintended_bias_toxic_comment_classification_RoBERTa_combined.json
  4. Evaluate the Multilingual Toxic Comment Classification model

    master

    Evaluate a trained checkpoint for the Multilingual challenge using the evaluate.py script. This challenge is evaluated on the AUC score of the main toxic label.

    python evaluate.py --checkpoint <path_to_checkpoint> --test_csv <path_to_test_csv>

    python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
  5. Evaluate the Toxic Comment Classification model

    master

    Evaluate a trained checkpoint for the Toxic Comment Classification challenge using the evaluate.py script. This challenge is evaluated on the mean AUC score of all labels.

    python evaluate.py --checkpoint <path_to_checkpoint> --test_csv <path_to_test_csv>

    python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
  6. Perform quick toxicity predictions with Detoxify

    master

    Use the Detoxify class to load a model and predict toxicity scores for a single string or a list of strings.

    Supported model names include:

    • original (BERT-based)
    • unbiased (RoBERTa-based)
    • multilingual (XLM-RoBERTa-based)
    • original-small (Albert-based)
    • unbiased-small (Albert-based)

    You can also specify a device (e.g., 'cuda') to allocate the model to a specific hardware device.

    from detoxify import Detoxify
    
    # Predict on a single string
    results = Detoxify('original').predict('example text')
    
    # Predict on a list of strings
    results = Detoxify('unbiased').predict(['example text 1','example text 2'])
    
    # Use the multilingual model
    results = Detoxify('multilingual').predict(['example text','exemple de texte'])
    
    # Specify a device (e.g., GPU)
    model = Detoxify('original', device='cuda')
    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))
  7. Train the Multilingual Toxic Comment Classification model

    master

    To train the multilingual model (supporting French, Spanish, Italian, Portuguese, Turkish, and Russian), first preprocess the multilingual test data and then run the training script with the XLMR configuration.

    1. Combine test files:

    python preprocessing_utils.py --test_csv <path_to_multilingual_test_csv> --update_test 2. Start training: python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json

    # combine test.csv and test_labels.csv
    python preprocessing_utils.py --test_csv jigsaw_data/jigsaw-multilingual-toxic-comment-classification/test.csv --update_test
    
    python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json
  8. Understand Detoxify class names and standardization

    master

    Detoxify standardizes class names across its different models to ensure consistent API usage. When you receive results from predict(), the keys in the returned dictionary will follow these mappings:

    Original NameStandardized Name
    toxictoxicity
    identity_hateidentity_attack
    severe_toxicsevere_toxicity
  9. Load Detoxify models via Torch Hub

    master

    You can load pre-trained Detoxify checkpoints directly using torch.hub.load with the following names:

    • toxic_bert (for the original model)
    • unbiased_toxic_roberta (for the unbiased model)
    • multilingual_toxic_xlm_r (for the multilingual model)
    import torch
    model = torch.hub.load('unitaryai/detoxify','toxic_bert')
    model = torch.hub.load('unitaryai/detoxify','toxic_bert')
  10. Reference: Detoxify model names and types

    master
    Model nameTransformer typeData from
    originalbert-base-uncasedToxic Comment Classification Challenge
    unbiasedroberta-baseUnintended Bias in Toxicity Classification
    multilingualxlm-roberta-baseMultilingual Toxic Comment Classification