/MAX-Toxic-Comment-Classifier

Detect 6 types of toxicity in user comments.

Primary LanguagePythonApache License 2.0Apache-2.0

Build Status API demo

IBM Developer Model Asset Exchange: Toxic Comment Classifier

This repository contains code to instantiate and deploy a toxic comment classifier. This model is able to detect 6 types of toxicity in a text fragment. The six detectable types are toxic, severe toxic, obscene, threat, insult, and identity hate.

The model is based on the pre-trained BERT-Base, English Uncased model and was finetuned on the Toxic Comment Classification Dataset using the Huggingface BERT Pytorch repository. The model files are hosted on IBM Cloud Object Storage. The code in this repository deploys the model as a web service in a Docker container. This repository was developed as part of the IBM Developer Model Asset Exchange and the public API is powered by IBM Cloud.

A brief definition of the six different toxicity types can be found below.

Toxic: very bad, unpleasant, or harmful

Severe toxic: extremely bad and offensive

Obscene: (of the portrayal or description of sexual matters) offensive or disgusting by accepted standards of morality and decency

Threat: a statement of an intention to inflict pain, injury, damage, or other hostile action on someone in retribution for something done or not done

Insult: speak to or treat with disrespect or scornful abuse

Identity hate: hatred, hostility, or violence towards members of a race, ethnicity, nation, religion, gender, gender identity, sexual orientation or any other designated sector of society

Model Metadata

Domain Application Industry Framework Training Data Input Data
Natural Language Processing (NLP) Text Classification General PyTorch Toxic Comment Classification Dataset Text

Benchmark

This model achieves a column-wise ROC AUC score of 0.98355 (private score) in the Kaggle Toxic Comment Classification Competition. This implementation is trained with a maximum sequence length of 256 instead of 512 to have higher inference speed. For most applications outside of this Kaggle competition, a sequence length of 256 is more than sufficient.

References

Licenses

Component License Link
This repository Apache 2.0 LICENSE
Finetuned Model Weights Apache 2.0 LICENSE
Pre-trained Model Weights Apache 2.0 LICENSE
TensorFlow Model Code (3rd party) Apache 2.0 LICENSE
PyTorch Model Code (3rd party) Apache 2.0 LICENSE
Toxic Comment Classification Dataset CC0 LICENSE

Pre-requisites:

  • docker: The Docker command-line interface. Follow the installation instructions for your system.
  • The minimum recommended resources for this model is 4GB Memory and 4 CPUs.

Deployment options

Deploy from Quay

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 quay.io/codait/max-toxic-comment-classifier

This will pull a pre-built image from the Quay.io container registry (or use an existing image if already cached locally) and run it. If you'd rather checkout and build the model locally you can follow the run locally steps below.

Deploy on Red Hat OpenShift

You can deploy the model-serving microservice on Red Hat OpenShift by following the instructions for the OpenShift web console or the OpenShift Container Platform CLI in this tutorial, specifying quay.io/codait/max-toxic-comment-classifier as the image name.

Deploy on Kubernetes

You can also deploy the model on Kubernetes using the latest docker image on Quay.

On your Kubernetes cluster, run the following commands:

$ kubectl apply -f https://github.com/IBM/MAX-Toxic-Comment-Classifier/raw/master/max-toxic-comment-classifier.yaml

The model will be available internally at port 5000, but can also be accessed externally through the NodePort.

A more elaborate tutorial on how to deploy this MAX model to production on IBM Cloud can be found here.

Run Locally

  1. Build the Model
  2. Deploy the Model
  3. Use the Model
  4. Development
  5. Cleanup

1. Build the Model

Clone this repository locally. In a terminal, run the following command:

$ git clone https://github.com/IBM/MAX-Toxic-Comment-Classifier.git

Change directory into the repository base folder:

$ cd MAX-Toxic-Comment-Classifier

To build the docker image locally, run:

$ docker build -t max-toxic-comment-classifier .

All required model assets will be downloaded during the build process. Note that currently this docker image is CPU only (we will add support for GPU images later).

2. Deploy the Model

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 max-toxic-comment-classifier

3. Use the Model

The API server automatically generates an interactive Swagger documentation page. Go to http://localhost:5000 to load it. From there you can explore the API and also create test requests.

Example:
{
  "text": [
    "I would like to punch you.",
    "In hindsight, I do apologize for my previous statement."
  ]
}

Result:
{
  "status": "ok",
  "results": [
    {
      "original_text": "I would like to punch you.",
      "predictions": {
        "toxic": 0.9796434044837952,
        "severe_toxic": 0.07256634533405304,
        "obscene": 0.058431386947631836,
        "threat": 0.8635178804397583,
        "insult": 0.11121545732021332,
        "identity_hate": 0.013826466165482998
      }
    },
    {
      "original_text": "In hindsight, I do apologize for my previous statement.",
      "predictions": {
        "toxic": 0.00029103411361575127,
        "severe_toxic": 0.00012417171092238277,
        "obscene": 0.0001522742968518287,
        "threat": 8.440738747594878e-05,
        "insult": 0.00016013211279641837,
        "identity_hate": 0.00012860879360232502
      }
    }
  ]
}

Use the model/predict endpoint to submit input text in json format. The JSON structure should have one key, text, with as value a list of input strings to be analyzed. An example can be found in the image below.

Submitting proper json data triggers the model and will return a json file with a status and a predictions key. With this predictions field, a list of class labels and their corresponding probabilities will be associated. The first element in the list corresponds to the prediction for the first string in the input list.

Swagger UI Screenshot

You can also test it on the command line, for example:

$ curl -d "{ \"text\": [ \"I would like to punch you.\", \"In hindsight, I do apologize for my previous statement.\" ]}" -X POST "http://localhost:5000/model/predict" -H "Content-Type: application/json"

You should see a JSON response like that below:

{
  "status": "ok",
  "results": [
    {
      "original_text": "I would like to punch you.",
      "predictions": {
        "toxic": 0.9796434044837952,
        "severe_toxic": 0.07256634533405304,
        "obscene": 0.058431386947631836,
        "threat": 0.8635178804397583,
        "insult": 0.11121545732021332,
        "identity_hate": 0.013826466165482998
      }
    },
    {
      "original_text": "In hindsight, I do apologize for my previous statement.",
      "predictions": {
        "toxic": 0.00029103411361575127,
        "severe_toxic": 0.00012417171092238277,
        "obscene": 0.0001522742968518287,
        "threat": 8.440738747594878e-05,
        "insult": 0.00016013211279641837,
        "identity_hate": 0.00012860879360232502
      }
    }
  ]
}

4. Development

To run the Flask API app in debug mode, edit config.py to set DEBUG = True under the application settings. You will then need to rebuild the docker image (see step 1).

5. Cleanup

To stop the Docker container, type CTRL + C in your terminal.

Resources and Contributions

If you are interested in contributing to the Model Asset Exchange project or have any queries, please follow the instructions here.