Giter VIP home page Giter VIP logo

ibm / max-toxic-comment-classifier Goto Github PK

View Code? Open in Web Editor NEW
51.0 21.0 30.0 968 KB

Detect 6 types of toxicity in user comments.

Home Page: https://developer.ibm.com/exchanges/models/all/max-toxic-comment-classifier/

License: Apache License 2.0

Dockerfile 4.23% Python 95.77%
docker-image machine-learning machine-learning-models ibm pytorch nlp comments natural-language-processing natural-language-understanding

max-toxic-comment-classifier's Introduction

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.

max-toxic-comment-classifier's People

Contributors

autoih avatar bdwyer2 avatar djalova avatar kmh4321 avatar nibalizer avatar ptitzler avatar splovyt avatar xuhdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

max-toxic-comment-classifier's Issues

Simplify /model/predict results data structure

Is it possible to simplify the result data structure?

For example

{
  "text": [
    "Mary had a little lamb"
  ]
}

returns

{
  "status": "ok",
  "predictions": [
    [
      {
        "toxic": 0.0014839612413197756,
        "severe_toxic": 0.00009525173663860187,
        "obscene": 0.0003549971734173596,
        "threat": 0.00007228714093798772,
        "insult": 0.00022114443709142506,
        "identity_hate": 0.00010927908442681655
      }
    ]
  ]
}

Note that the prediction returns a list of lists of dicts, whereas it should be sufficient to return a list of dicts.

{
  "status": "ok",
  "predictions": [
      {
        "toxic": 0.0014839612413197756,
        "severe_toxic": 0.00009525173663860187,
        "obscene": 0.0003549971734173596,
        "threat": 0.00007228714093798772,
        "insult": 0.00022114443709142506,
        "identity_hate": 0.00010927908442681655
      }
   ]
}

Not sure which input would return a multi-dimensional response. The simplified version can accommodate multiple inputs:

{
  "text": [
    "Mary had a little lamb",
    "Little lamb, little lamb,"
  ]
}

would return

{
  "status": "ok",
  "predictions": [
      {
        "toxic": 0.0014839619398117065,
        "severe_toxic": 0.0000952516493271105,
        "obscene": 0.00035499755176715553,
        "threat": 0.00007228707545436919,
        "insult": 0.00022114443709142506,
        "identity_hate": 0.00010927919356618077
      },
      {
        "toxic": 0.1212717592716217,
        "severe_toxic": 0.00012042735033901408,
        "obscene": 0.0017211131053045392,
        "threat": 0.00017663949984125793,
        "insult": 0.0008766597020439804,
        "identity_hate": 0.00014419591752812266
      }
  ]
}

with predictions[0] returning the eval for the first string, .... second string.

What am I missing?

/model/predict returns unexpected result

No big deal, but

{
 "text": [""]
}

it's odd that nothing can be interpreted as something

     {
        "toxic": 0.0015665121609345078,
        "severe_toxic": 0.00011611604713834822,
        "obscene": 0.0004790136299561709,
        "threat": 0.00007217624806798995,
        "insult": 0.0003883792378474027,
        "identity_hate": 0.0003294776252005249
      }

and similarly a meaningless combination of characters

{
 "text": ["khdcuc4wcc5jlcnw"]
}

and numbers

{
 "text": ["123"]
}

Note sure if there's something that can be done to eliminate noise that's a result of garbage input.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.