Building a Language Toxicity Classification Model | by Ekaba Bisong | Towards Data Science

Online communities are a significant part of interactions over the internet. The rise of such communities comes with the need for community moderation to ensure that participants adhere to their prescribed guidelines and avoid “clear and obvious” and objective policy violations.
Due to the presence of bad actors with malicious intent, the work of moderation poses particular challenges. One of such is the time spent on moderating online communities due to the size of bad actors. Moreso, the volume of these violations may result in some of them making it past the moderators and may be extremely dangerous in specific communities. An example is an obscene text getting past moderators in an online community for K-12 children.
Also, the work of moderation may lead to other challenges such as consistency in judgment and more importantly, psychological danger due to constant exposure to graphic and obscene materials.
Google Cloud AutoML for Natural Language provides the platform for designing and developing custom language models for language recognition use-cases. This article uses Google Cloud AutoML for Natural Language to develop an end-to-end language toxicity classification model to identify obscene text. The concept of neural architecture search and transfer learning are used under the hood to find the best network architecture and the optimal hyperparameter setting that improves the performance of the model.
The data used in this project is from the Toxic Comment Classification Challenge on Kaggle by Jigsaw and Google. The data is modified to have a sample of 16,000 toxic and 16,000 non-toxic words as inputs to build the model on AutoML NLP.
(1). Go to the cloud console: https://cloud.google.com/
(2). Open Cloud AutoML Natural Language by clicking the triple-dash at the top-left corner of the GCP dashboard. Select Natural Language under the product section for Artificial Intelligence.
(3). Select Text Classification under AutoML Natural Language.
(4). Setup Project APIs, permissions and Cloud Storage bucket to store the text files for modeling and other AutoML assets.
(5). Select your GCP billing project from the drop-down when asked. Now we are ready to create a Dataset for building the custom classification model on AutoML. We will return here after downloading the raw dataset from Kaggle to Cloud Storage and preparing the data for modeling with AutoML.
In this case, the automatically created bucket is called: gs://ekabasandbox-lcm.
(1). Activate the Cloud shell (in red circle) to launch the ephemeral VM instance to stage the dataset download from Kaggle, unzip it and upload to the storage bucket.
(2). Install the Kaggle command-line interface. This tool will allow us to download datasets from Kaggle. Run the following code:
sudo pip install kaggle
Note, however, that the Cloud Shell instance is ephemeral and does not persist system-wide changes when the session ends. Also, if a dataset is particularly large, other options exist such as spinning-up a compute VM, download the dataset, unzip it and then upload to Cloud Storage. It is possible to design other advanced data pipelines to get data into GCP for analytics/ machine learning.
(3). Download Kaggle API token key that will enable the Kaggle CLI to authenticate/ authorize against Kaggle to download the desired datasets.
Login to your Kaggle account.
Go to: https://www.kaggle.com/[KAGGLE_USER_NAME]/account
Click on: Create New API Token.
Download the token to your local machine and upload it to the cloud shell.
Move the uploaded .json key to the directory .kaggle . Use the code below:
mv kaggle.json .kaggle/kaggle.json
(4). Download dataset from Kaggle to Google Cloud Storage.
kaggle competitions download -c jigsaw-toxic-comment-classification-challenge
(5). Unzip the downloaded dataset
unzip jigsaw-toxic-comment-classification-challenge.zip
(6). Move the dataset from the ephemeral cloud shell instance to the created cloud storage bucket. Insert your bucket name here.
gsutil -m cp -r train.csv test.csv gs://ekabasandbox-lcm/toxicity/
(1). Launch Jupyter Notebooks on the Google Cloud AI Platform.
(2). Create a new Notebook Instance.
(3). Select an instance name and create.
(4). Open JupyterLab.
(5). Before building a custom language classification model with AutoML Cloud Natural Language, the dataset must be prepared in a particular format:
The training input can either be a document in (.txt) format or as a cell in a (.csv) file. Multiple texts can be grouped as a compressed (.zip) file.For this project, text files are placed in sub-folders with their output labels as the folder name. This is later used to create a CSV file containing the data file path and their labels. An example folder structure is shown below: → [files] — →[toxic] — →[clean]Next, a CSV is generated that points to the paths of the text and their corresponding label. Cloud NLP uses the CSV file to point to the location of the training documents or words and their corresponding labels. The CSV file is placed in the same GCS bucket created AutoML NLP was configured. In our case, this bucket is named gs://ekabasandbox-lcm.
The training input can either be a document in (.txt) format or as a cell in a (.csv) file. Multiple texts can be grouped as a compressed (.zip) file.
For this project, text files are placed in sub-folders with their output labels as the folder name. This is later used to create a CSV file containing the data file path and their labels. An example folder structure is shown below: → [files] — →[toxic] — →[clean]
Next, a CSV is generated that points to the paths of the text and their corresponding label. Cloud NLP uses the CSV file to point to the location of the training documents or words and their corresponding labels. The CSV file is placed in the same GCS bucket created AutoML NLP was configured. In our case, this bucket is named gs://ekabasandbox-lcm.
(6). Clone the preprocessing script from Github. Click on the icon, circled in red and enter the Github URL https://github.com/dvdbisong/automl-toxicity-classification.git to clone the repo with the preprocessing code.
(7). Run all the cells in the notebook nlp_preprocessing.ipynb to create the CSV file containing the path and labels of the images and upload this file to Cloud Storage.
(1). Click on “New Dataset” from the AutoML Natural Language Dashboard.
(2). Fill-in the dataset name and select the CSV file from the Cloud Storage bucket created by AutoML.
(3). For now, you may dismiss if you see the error message that duplicated files are located. To the best of my knowledge, this is not the case as per the file names.
(4). Click on Train as shown in red in the image above to initiate model building with Cloud AutoML.
(5). Start the model training.
(6). After model training is complete, click on Evaluate to view the performance metric of the model.
(7). Assess the performance metric (precision, recall and confusion matrix).
(1). Click on the Predict tab to test the model.
(2). We can explore this model by passing in introductory text about Nigeria from Wikipedia. We expect the model prediction to be clean. By further exploration, the model performs well on “clear and obvious” toxic examples.
The custom language classification model is also exposed as a REST or Python API for integration into software applications as a prediction service for inference.
The article provided a walkthrough to design powerful language classification models for custom use-cases by leveraging Google Cloud Platform AutoML Natural Language. Also, the model is hosted on the cloud for inference as a prediction service. This is a powerful mechanism to build and deploy language classification products. I trust this sample serves as a template for using AutoML to build Natural Language products. Finally, be sure to delete your models and datasets when it is no longer needed, this will spare cloud costs.
Excerpts of this article are taken from the book “Building Machine Learning and Deep Learning Models on Google Cloud Platform” published by Apress (Springer Nature).