Enabling Toxicity Detection Using OpenVINO

OpenVINO™ toolkit
OpenVINO-toolkit
Published in
4 min readOct 9, 2024

Author: Pradeep Sakhamoori

The Detoxify library empowers developers with pre-trained models to detect toxic language in online conversations. Among its models, Toxic-BERT stands out as an effective tool for identifying various types of harmful speech, including categories like “toxic,” “severe toxic,” “insult,” “obscene,” and more. Trained on large datasets of offensive and aggressive language, Toxic-BERT can help mitigate toxicity in online platforms by classifying harmful text into specific categories. You can find more details on the Toxic-BERT model page.

In this article, we’ll demonstrate how to optimize and efficiently run toxic text classification using the Toxic-BERT model with OpenVINO™ and Optimum-Intel.

Follow the steps outlined below to get started.

Step 0: Setting up the Environment

Create and enable a Python virtual environment.

# Create a Python virtual environment
python3 -m venv myenv
# Activate the Python virtual environment
source ./myenv/bin/activate
# Install Optimum-Intel with the OpenVINO backend
pip install optimum-intel[openvino]

Step 1: Setting Up the Model and Tokenizer

The Detoxify model can be loaded directly from the Hugging Face model hub using the Optimum-Intel framework. With this setup, users can load it as a typical Hugging Face model while leveraging OpenVINO™ under the hood.

from optimum.intel.openvino import OVModelForSequenceClassification
from transformers import AutoTokenizer
import numpy as np
import openvino.properties.hint as hints
# Define model and tokenizer name
model_id = "unitary/toxic-bert"
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id)
# [optional] OpenVINO configuration
ov_config = {hints.performance_mode: hints.PerformanceMode.THROUGHPUT}
# Load the OpenVINO-optimized model
ov_model = OVModelForSequenceClassification.from_pretrained(model_id, \
device="CPU", \
ov_config=ov_config, \
export=True)

For further performance optimization and configuration, refer to Hugging Face Optimum Documentation and OpenVINO Performance Guide.

Step 2: Preparing the Input Data

To classify text inputs for toxicity, the input data first needs to be tokenized. The tokenizer converts raw text into numerical input IDs and attention masks, which the model can then process efficiently.

# Example texts to classify
texts = [
"You are the terrible person I have ever met.",
"I hope something terrible happens to you.",
# (Additional texts...)
]
# Tokenize the batch of texts
inputs = tokenizer(texts, \
return_tensors="np", \
padding=True, \
truncation=True)
# Prepare input IDs and attention masks
input_ids = inputs["input_ids"]
attention_mask = inputs["attention_mask"]

Step 3: Compiling and Running the OpenVINO™ Model

Once the model and inputs are ready, we can compile the model using OpenVINO’s optimized backend. This allows for efficient execution of the model on Intel® devices (default being CPU).

# Compile the OpenVINO model
ov_model.compile()
# Run inference on the batch
output = ov_model(input_ids=input_ids, attention_mask=attention_mask)

Step 4: Processing the Output

The model’s output consists of logits — raw prediction scores that need to be converted into probabilities using a sigmoid function. Based on these probabilities, we can classify the toxic content into one of the following categories: [toxic, severe toxic, obscene, threat, insult, identity hate].

# Convert logits to probabilities using sigmoid function
logits = output['logits']
probabilities = 1 / (1 + np.exp(-logits))
# Define class labels
labels = ["toxic", "severe_toxic",
"obscene", "threat",
"insult", "identity_hate"]
# Set the threshold for classification
threshold = 0.5
# Process and print predictions for each text
for i, text in enumerate(texts):
predicted_labels = [labels[j] for j, prob in enumerate(probabilities[i]) \
if prob >= threshold]
print(f"Input text: {text}")
print(f"Predicted toxic categories: {predicted_labels}")
print()

Expected output

Input text: You are the terrible person I have ever met.
Predicted toxic categories: ['toxic', 'insult']
Input text: I hope something terrible happens to you.
Predicted toxic categories: ['toxic', 'threat']

Conclusion

Leveraging Optimum-Intel with the OpenVINO™ backend allows for seamless optimization of the Hugging Face Detoxify Toxic-BERT model and runs inference on Intel® devices. This combination simplifies the setup process and enables efficient execution of toxic language detection tasks. By following the outlined steps, developers can easily deploy the Hugging Face models and harness the capabilities of OpenVINO for LLM-based applications.

Additional References:

  1. LLMs with Hugging Face and Optimum Intel
  2. OpenVINO Large Language Model Inference Guide
  3. Toxic Bert usage limitations and guidelines
  4. OpenVINO documentation

Notices & Disclaimers

Intel technologies may require enabled hardware, software, or service activation.

Performance varies by use, configuration, and other factors. Learn more on the Performance Index site.

Performance results are based on testing as of dates shown in configurations and may not reflect all publicly available updates.

No product or component can be absolutely secure.

Your costs and results may vary.

© Intel Corporation. Intel, the Intel logo, and other Intel marks are trademarks of Intel Corporation or its subsidiaries. Other names and brands may be claimed as the property of others.

--

--

OpenVINO™ toolkit
OpenVINO-toolkit

Deploy high-performance deep learning productively from edge to cloud with the OpenVINO™ toolkit.