toxic-bert for Kjarni

Flags toxic comments in English, packaged to run locally with Kjarni: one native library, with no Python runtime, server or cloud service. Each comment gets a score from 0 to 1 for six labels, each scored on its own: toxic, severe_toxic, obscene, threat, insult and identity_hate. The model is about 440 MB and downloads on first use.

Usage

Tested with Kjarni 0.8.2. Each example prints the labels scoring above 0.5.

Command line

curl -fsSL https://kjarni.ai/install.sh | sh    # Windows: irm https://kjarni.ai/install.ps1 | iex
kjarni classify -m toxic-bert "You are an idiot and nobody wants you here."
Model: toxic-bert  Device: Cpu

  Input "You are an idiot and nobody wants you here."

  βœ“          toxic  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ   98.8%
            insult  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘   94.8%
           obscene  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘   68.8%
      severe_toxic  β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘    3.2%
     identity_hate  β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘    1.2%

Python

# pip install kjarni
from kjarni import Classifier

classifier = Classifier(model="toxic-bert", quiet=True)
for text in ["Thanks, that fixed it!", "You are an idiot and nobody wants you here."]:
    result = classifier.classify(text)
    flagged = [label for label, score in result.all_scores if score > 0.5]
    print(flagged, text)
[] Thanks, that fixed it!
['toxic', 'insult', 'obscene'] You are an idiot and nobody wants you here.

Rust

// cargo add kjarni anyhow tokio --features tokio/full
use kjarni::Classifier;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let classifier = Classifier::new("toxic-bert").await?;
    for text in [
        "Thanks, that fixed it!",
        "You are an idiot and nobody wants you here.",
    ] {
        let result = classifier.classify(text).await?;
        let flagged: Vec<_> = result
            .all_scores
            .iter()
            .filter(|(_, s)| *s > 0.5)
            .map(|(l, _)| l)
            .collect();
        println!("{flagged:?} {text}");
    }
    Ok(())
}

C#

// dotnet add package Kjarni
using Kjarni;

using var classifier = new Classifier("toxic-bert", quiet: true);
foreach (var text in new[] { "Thanks, that fixed it!", "You are an idiot and nobody wants you here." })
{
    var result = classifier.Classify(text);
    var flagged = result.AllScores.Where(s => s.Score > 0.5f).Select(s => s.Label);
    Console.WriteLine($"[{string.Join(", ", flagged)}] {text}");
}

C++ (C++23)

#include <kjarni.hpp>
#include <iostream>

int main() {
    kjarni::ClassifierOptions options;
    options.model = "toxic-bert";

    auto classifier = kjarni::Classifier::create(options);
    if (!classifier) return 1;

    for (const char* text : {"Thanks, that fixed it!", "You are an idiot and nobody wants you here."}) {
        auto labels = classifier->classify(text);
        if (!labels) return 1;
        std::cout << "[";
        for (const auto& [label, score] : *labels)
            if (score > 0.5f) std::cout << " " << label;
        std::cout << " ] " << text << "\n";
    }
}

Go

// go get github.com/kjarni-ai/kjarni-go
package main

import (
    "fmt"
    "log"

    kjarni "github.com/kjarni-ai/kjarni-go"
)

func main() {
    classifier, err := kjarni.NewClassifier("toxic-bert", kjarni.WithQuiet(true))
    if err != nil {
        log.Fatal(err)
    }
    defer classifier.Close()

    for _, text := range []string{"Thanks, that fixed it!", "You are an idiot and nobody wants you here."} {
        result, err := classifier.Classify(text)
        if err != nil {
            log.Fatal(err)
        }
        flagged := []string{}
        for _, s := range result.AllScores {
            if s.Score > 0.5 {
                flagged = append(flagged, s.Label)
            }
        }
        fmt.Println(flagged, text)
    }
}

See the Kjarni documentation for other languages and installation details.

What changed from the original

This is not a new model. The weights are unchanged: every tensor is bit-for-bit the same as in the original. Only the packaging differs:

  • The unused position_ids buffer is left out of model.safetensors.
  • A tokenizer.json is added, made from the original's vocab.txt.
  • The configuration and tokenizer files are re-saved by a newer version of transformers.

The files also load in transformers. On four test sentences, Kjarni's scores match transformers running the original model to six decimal places.

Limitations

From the original model card: comments containing swearing, insults or profanity are likely to be flagged as toxic whatever the tone or intent, including humorous or self-deprecating ones. This can bias the model against groups that are already vulnerable. The model was trained on English Wikipedia comments from the 2018 Jigsaw Toxic Comment Classification Challenge, and its scores are best treated as a signal for human review, not a final decision.

Attribution

The model is unitary/toxic-bert, the original model from Detoxify by Laura Hanu and the Unitary team. All credit for the model, its training and its evaluation belongs to them. See the original model card for benchmarks and more on its limitations.

Kjarni and this packaging are not affiliated with or endorsed by Unitary.

This repository is released under the Apache License 2.0, the same license as the original model.

Citation

@misc{Detoxify,
  title={Detoxify},
  author={Hanu, Laura and {Unitary team}},
  howpublished={Github. https://github.com/unitaryai/detoxify},
  year={2020}
}
Downloads last month
850
Safetensors
Model size
0.1B params
Tensor type
F32
Β·
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for kjarni-ai/toxic-bert

Finetuned
(11)
this model