Text Classifier

MediaPipe Text Classifier task lets you classify text into a set of defined categories, such as positive or negative sentiment. The categories are defined during the training of the model. This task operates on text data with a machine learning (ML) model as static data and outputs a list of categories and their likelihood scores.

Quickstart

Install the SDK for your platform, initialize the task with a model, and run inference.

Web

npmnpm install @mediapipe/tasks-textWeb demo·Edit in StackBlitz·Setup guide
import { FilesetResolver, TextClassifier } from "@mediapipe/tasks-text";

const MODEL_URL = "https://storage.googleapis.com/mediapipe-models/text_classifier/bert_classifier/float32/latest/bert_classifier.tflite";

const textFiles = await FilesetResolver.forTextTasks(
  "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-text@latest/wasm"
);
const textClassifier = await TextClassifier.createFromOptions(textFiles, {
  baseOptions: { modelAssetPath: MODEL_URL },
  maxResults: 5,
});

const inputText = "An unforgettable and genuinely moving film!";
const result = textClassifier.classify(inputText);

Android (Kotlin)

Gradleimplementation("com.google.mediapipe:tasks-text:latest.release")Sample code·Setup guide
import com.google.mediapipe.tasks.core.BaseOptions
import com.google.mediapipe.tasks.text.textclassifier.TextClassifier
import com.google.mediapipe.tasks.text.textclassifier.TextClassifier.TextClassifierOptions

val baseOptions = BaseOptions.builder()
    .setModelAssetPath("bert_classifier.tflite")
    .build()
val options = TextClassifierOptions.builder()
    .setBaseOptions(baseOptions)
    .build()

val textClassifier = TextClassifier.createFromOptions(context, options)

val inputText = "An unforgettable and genuinely moving film!"
val result = textClassifier.classify(inputText)

Android (Java)

Gradleimplementation 'com.google.mediapipe:tasks-text:latest.release'Sample code·Setup guide
import com.google.mediapipe.tasks.core.BaseOptions;
import com.google.mediapipe.tasks.text.textclassifier.TextClassifier;
import com.google.mediapipe.tasks.text.textclassifier.TextClassifier.TextClassifierOptions;
import com.google.mediapipe.tasks.text.textclassifier.TextClassifierResult;

BaseOptions baseOptions = BaseOptions.builder()
    .setModelAssetPath("bert_classifier.tflite")
    .build();
TextClassifierOptions options = TextClassifierOptions.builder()
    .setBaseOptions(baseOptions)
    .build();

TextClassifier textClassifier = TextClassifier.createFromOptions(context, options);

String inputText = "An unforgettable and genuinely moving film!";
TextClassifierResult result = textClassifier.classify(inputText);

iOS

SwiftPMhttps://github.com/google-ai-edge/mediapipe → MediaPipeTasksTextPackage.swift·Sample code·Setup guide
import Foundation
import MediaPipeTasksText

guard let modelPath = Bundle.main.path(forResource: "bert_classifier", ofType: "tflite") else { return }

let options = TextClassifierOptions()
options.baseOptions.modelAssetPath = modelPath
options.scoreThreshold = 0.6

let textClassifier = try TextClassifier(options: options)

let inputText = "An unforgettable and genuinely moving film!"
let result = try textClassifier.classify(text: inputText)

Python

pippip install mediapipeSample code·Setup guide
import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
TextClassifier = mp.tasks.text.TextClassifier
TextClassifierOptions = mp.tasks.text.TextClassifierOptions

model_path = "bert_classifier.tflite"
options = TextClassifierOptions(base_options=BaseOptions(model_asset_path=model_path))

input_text = "An unforgettable and genuinely moving film!"
with TextClassifier.create_from_options(options) as classifier:
    classification_result = classifier.classify(input_text)

Task details

This section describes the capabilities, inputs, outputs, and configuration options of this task.

Features

  • Input text processing - Support out-of-graph tokenization for models without in-graph tokenization
  • Multiple classification heads - Each head can use its own category set
  • Label map locale - Set the language used for display names
  • Score threshold - Filter results based on prediction scores
  • Top-k classification results - Limit the number of classification results
  • Label allowlist and denylist - Specify the categories detected
Inputs
Text Classifier accepts the following input data type:
  • String
Outputs
The Text Classifier outputs a list of categories containing:
  • Category index: the index of the category in the model outputs
  • Score: the confidence score for this category, expressed as a probability between zero and one as floating point value.
  • Category name (optional): the name of the category as specified in the TensorFlow Lite Model Metadata, if available.
  • Category display name (optional): a display name for the category as specified in the TensorFlow Lite Model Metadata, in the language specified through display names locale options, if available.

Configuration options

This task has the following configuration options:

Web

Option Name Description Value Range Default Value
displayNamesLocale Locale code for display names provided in the model metadata, if available. You can add localized labels to custom models with the LiteRT Metadata Writer API. Locale code "en"
maxResults Maximum number of top-scored classification results to return (-1 returns all results). Integer -1
scoreThreshold Prediction score threshold overriding the model metadata default. Classifications lower than this score are rejected. Float [0.0, 1.0] Not set
categoryAllowlist Optional list of allowed category names. Mutually exclusive with categoryDenylist. List of strings Not set
categoryDenylist Optional list of excluded category names. Mutually exclusive with categoryAllowlist. List of strings Not set

Android

Option Name Description Value Range Default Value
displayNamesLocale Locale code for display names provided in the model metadata, if available. You can add localized labels to custom models with the LiteRT Metadata Writer API. Locale code "en"
maxResults Maximum number of top-scored classification results to return (-1 returns all results). Integer -1
scoreThreshold Prediction score threshold overriding the model metadata default. Classifications lower than this score are rejected. Float [0.0, 1.0] Not set
categoryAllowlist Optional list of allowed category names. Mutually exclusive with categoryDenylist. List of strings Not set
categoryDenylist Optional list of excluded category names. Mutually exclusive with categoryAllowlist. List of strings Not set

iOS

Option Name Description Value Range Default Value
displayNamesLocale Locale code for display names provided in the model metadata, if available. You can add localized labels to custom models with the LiteRT Metadata Writer API. Locale code "en"
maxResults Maximum number of top-scored classification results to return (-1 returns all results). Integer -1
scoreThreshold Prediction score threshold overriding the model metadata default. Classifications lower than this score are rejected. Float [0.0, 1.0] Not set
categoryAllowlist Optional list of allowed category names. Mutually exclusive with categoryDenylist. List of strings Not set
categoryDenylist Optional list of excluded category names. Mutually exclusive with categoryAllowlist. List of strings Not set

Python

Option Name Description Value Range Default Value
display_names_locale Locale code for display names provided in the model metadata, if available. You can add localized labels to custom models with the LiteRT Metadata Writer API. Locale code "en"
max_results Maximum number of top-scored classification results to return (-1 returns all results). Integer -1
score_threshold Prediction score threshold overriding the model metadata default. Classifications lower than this score are rejected. Float [0.0, 1.0] Not set
category_allowlist Optional list of allowed category names. Mutually exclusive with category_denylist. List of strings Not set
category_denylist Optional list of excluded category names. Mutually exclusive with category_allowlist. List of strings Not set

Task output

Each TextClassifierResult contains one Classifications entry per classification head:

Field Type / Range Description
classifications Classifications[] Per-head classification results (headIndex, headName, categories).
categories Category[] (score in [0.0, 1.0]) Ranked sentiment or topic categories (categoryName, index, score).

The following example shows the sentiment classification output for the input text "an imperfect but overall entertaining mystery":

{
  classifications: [
    {
      headIndex: 0,
      categories: [
        { index: 0, score: 0.8904, categoryName: "positive" },
        { index: 1, score: 0.1096, categoryName: "negative" }
      ]
    }
  ]
}

Models

Text Classifier pipeline overview

Text Classifier can be used with more than one ML model. Start with the default, recommended model for your target platform when you start developing with this task. The other available models typically make trade-offs between performance, accuracy, resolution, and resource requirements, and in some cases, include additional features.

The pretrained models are trained for sentiment analysis, and predict whether the input text's sentiment is positive or negative. The models were trained on the SST-2 (Stanford Sentiment Treebank) dataset, which consists of movie reviews labeled as either positive or negative. Note that the models only support English. Since they were trained on a dataset of movie reviews, you may see reduced quality for text covering other topic areas.

This model uses a BERT-based architecture (specifically, the MobileBERT model) and is recommended because of its high accuracy. It contains metadata that allows the task to perform out-of-graph BERT tokenization.

Model name Input shape Quantization type Versions
BERT-classifier [1x128],[1x128],[1x128] dynamic range Latest

Average word embedding model

This model uses an average word-embedding architecture. This model offers a smaller model size and lower latency at the cost of a lower prediction accuracy compared to the BERT-classifier. Customizing this model through additional training is also faster than doing training of the BERT-based classifier. This model contains metadata that allows the task to perform out-of-graph regex tokenization.

Model name Input shape Quantization type Versions
Average word embedding 1 x 256 None (float32) Latest

Task benchmarks

Here are the task benchmarks for the whole pipeline based on the preceding pre-trained models. The latency result is the average latency on Pixel 6 using CPU / GPU.

Model name Device CPU latency GPU latency
Average word embedding Pixel 6 0.14ms -
BERT-classifier Pixel 6 57.68ms -