unitaryai/detoxify

Trained models & code to predict toxic comments on all 3 Jigsaw Toxic Comment Challenges. Built using ⚡ Pytorch Lightning and 🤗 Transformers.

What is Detoxify?

Detoxify is a Python library that automatically detects toxic comments — text that is rude, disrespectful, aggressive, or hateful. You give it a sentence (or a list of sentences), and it returns scores showing how likely the text is to be toxic in several categories.

It was built by Laura Hanu and James Thewlis at Unitary, a company working to stop harmful content online.

How it works

Detoxify comes with three pre-trained models, each based on a different machine-learning "transformer" architecture and trained on data from a different Kaggle competition:

Model name Underlying model Training data source
original BERT Wikipedia comments (2018 Toxic Comment Classification Challenge)
unbiased RoBERTa Civil Comments (2019 Unintended Bias in Toxicity Classification)
multilingual XLM-RoBERTa Combined data from the first two challenges, translated into multiple languages (2020 Multilingual Challenge)

Each model returns a set of scores (one per label) between 0 and 1, where higher means more likely to be that type of toxicity.

What it predicts

The labels depend on which model you use:

  • original predicts: toxic, severe_toxic, obscene, threat, insult, identity_hate
  • unbiased predicts: toxicity, severe_toxicity, obscene, threat, insult, identity_attack, sexual_explicit — plus identity labels like male, female, christian, muslim, black, white, and others
  • multilingual predicts the same labels as unbiased, but the final evaluation only uses the main toxicity score

The multilingual model was trained on 7 languages: English, French, Spanish, Italian, Portuguese, Turkish, and Russian.

How accurate is it?

The README reports AUC scores (a measure of how well the model separates toxic from non-toxic text, where 100% is perfect):

  • original: 98.64%
  • unbiased: 93.74%
  • multilingual: 92.11%

Note: the top Kaggle leaderboard scores were achieved with model ensembles (combining many models), while Detoxify uses a single model per name, so its scores are slightly lower but still strong.

Known limitations

If a comment contains swear words, insults, or profanity, Detoxify will likely classify it as toxic regardless of tone or intent — even if the author is being humorous or self-deprecating. This can create bias against already vulnerable minority groups. The README explicitly states the library is intended for research, fine-tuning on carefully constructed datasets, and helping human content moderators — not as a standalone moderation system.

How to use it

Install: pip install detoxify

Predict in Python:

from detoxify import Detoxify

results = Detoxify("original").predict("example text")
results = Detoxify("unbiased").predict(["text 1", "text 2"])
results = Detoxify("multilingual").predict(["example text", "exemple de texte", "texto de ejemplo"])

You can also specify a device (e.g., device="cuda" for GPU), load models from a checkpoint path, or run predictions from the command line with run_prediction.py. Results can be saved to a CSV file.

Training your own models

If you want to retrain the models yourself, you need a Kaggle account and API token to download the competition datasets. The README provides commands to download the data, then run train.py with a config file for each challenge. Training also requires the Kaggle API and additional dependencies (pip install -e 'detoxify[dev]').

Evaluation

Each challenge uses a different metric:

  • original: mean AUC across all labels
  • unbiased: a custom bias metric that combines multiple AUC scores to balance overall performance with fairness across identity groups
  • multilingual: AUC of the main toxicity label only

Citation

The README includes a citation for the project (Hanu, Thewlis, and the Unitary team, 2020) and a related Scientific American article (2021).

Related

  • Project
  • Project
  • Dispatch
  • Project
  • Dispatch