--- license: apache-2.0 library_name: litert pipeline_tag: text-classification language: - en tags: - mediapipe - litert - tflite - text-classification - on-device - android - ios - web - python - flutter - nlp - mobilebert - sentiment-analysis - sst-2 - dynamic-range - arxiv:2004.02984 --- # MediaPipe Text Classifier (MobileBERT) On-device English sentiment classification model (`bert_classifier.tflite`, 25.7 MB) trained on Stanford Sentiment Treebank (SST-2) movie reviews for the [MediaPipe Text Classifier (`TextClassifier`)](https://developers.google.com/edge/mediapipe/solutions/text/text_classifier) task.

Documentation: MediaPipe Text Classifier Docs

Samples: Android · iOS · Python Colab · Web · Flutter

Try live: Web Demo

MobileBERT sequence classifier fine-tuned on SST-2 (`negative`, `positive`), **dynamic-range quantized** with embedded BERT `vocab.txt` and `labels.txt` in `TFLITE_METADATA` (performing out-of-graph WordPiece tokenization into 128-token `word_ids`, `type_ids`, and `mask` inputs). ## Files | File | Precision | Size | SHA-256 | |---|---|---:|---| | [`bert_classifier.tflite`](https://huggingface.co/litert-community/MediaPipe-TextClassifier-MobileBERT/blob/main/bert_classifier.tflite) | `dynamic-range` | 25.71 MB (25,707,538 B) | `9b45012ab143d88d61e10ea501d6c8763f7202b86fa987711519d89bfa2a88b1` | ### Download ```bash pip install -U huggingface_hub hf download litert-community/MediaPipe-TextClassifier-MobileBERT bert_classifier.tflite --local-dir . ``` ## Inputs, Outputs, and Model Specifications ### `bert_classifier.tflite` - Embedded `TFLITE_METADATA`: name `MobileBert text classifier` · version `1` · author `MediaPipe` · packed associated files: `vocab.txt`, `labels.txt` | Direction | Tensor | Shape | Type | Notes | |---|---|---|---|---| | Input | `serving_default_input_word_ids:0` | `[1, 128]` | `int32` | ids — Tokenized ids of input text as concatenated query and passage. | | Input | `serving_default_input_type_ids:0` | `[1, 128]` | `int32` | segment_ids — 0 for query and 1 for passage tokens. | | Input | `serving_default_input_mask:0` | `[1, 128]` | `int32` | mask — Mask with 1 for real tokens and 0 for padding tokens. | | Output | `StatefulPartitionedCall:0` | `[1, 2]` | `float32` | probability — Probabilities of labels respectively. — Labels: `labels.txt` | ## Use with MediaPipe Tasks ### Python Quickstart ```bash pip install -U mediapipe huggingface_hub ``` ```python import mediapipe as mp from huggingface_hub import hf_hub_download model_path = hf_hub_download(repo_id="litert-community/MediaPipe-TextClassifier-MobileBERT", filename="bert_classifier.tflite") BaseOptions = mp.tasks.BaseOptions TextClassifier = mp.tasks.text.TextClassifier TextClassifierOptions = mp.tasks.text.TextClassifierOptions options = TextClassifierOptions(base_options=BaseOptions(model_asset_path=model_path)) with TextClassifier.create_from_options(options) as classifier: result = classifier.classify("An unforgettable and genuinely moving film!") for cat in result.classifications[0].categories: print(f"{cat.category_name}: {cat.score:.3f}") ``` ### Cross-Platform Guides & Samples - **Task overview & options:** [Text Classifier documentation](https://developers.google.com/edge/mediapipe/solutions/text/text_classifier) - **Platform setup guides:** [Python](https://developers.google.com/edge/mediapipe/solutions/setup_python) · [Web / JavaScript](https://developers.google.com/edge/mediapipe/solutions/setup_web) · [Android (Kotlin / Java)](https://developers.google.com/edge/mediapipe/solutions/setup_android) · [iOS (Swift)](https://developers.google.com/edge/mediapipe/solutions/setup_ios) · [Flutter (Dart)](https://developers.google.com/edge/mediapipe/solutions/setup_flutter) - **Runnable sample apps:** [Web Demo](https://google-ai-edge.github.io/mediapipe-samples-web/#/text/text_classifier) · [Web Source](https://github.com/google-ai-edge/mediapipe-samples-web/blob/main/src/tasks/text-classifier.ts) · [Android](https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_classification/android) · [iOS](https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_classification/ios) · [Python Notebook](https://colab.research.google.com/github/google-ai-edge/mediapipe-samples/blob/main/examples/text_classification/python/text_classifier.ipynb) · [Flutter](https://github.com/google/flutter-mediapipe/tree/main/samples/text/text_classifier) ## Latency Benchmarks End-to-end MediaPipe Task pipeline average latency reported by the [Text Classifier documentation](https://developers.google.com/edge/mediapipe/solutions/text/text_classifier#benchmarks): | Model Variant | Device | CPU Latency | GPU Latency | |---|---|---:|---:| | MobileBERT | Pixel 6 | 57.68 ms | — | ## Intended Use, Limitations, and Fairness - **Intended applications:** Binary English sentiment analysis (positive vs. negative) on short text snippets such as reviews and feedback. - **Out-of-scope & limitations:** - **English only:** Not suitable for non-English text. - **Domain shift:** Trained on SST-2 movie reviews (`https://nlp.stanford.edu/sentiment/index.html`); accuracy may degrade on unrelated domains, sarcasm, or multi-sentence documents. - No standalone Google fairness PDF is published for this model on the MediaPipe documentation page. ## Provenance Every model binary in this repository is byte-identical (unmodified FlatBuffer weights and embedded `TFLITE_METADATA` bytes) to the official artifact served by Google from `storage.googleapis.com/mediapipe-models`. | Repo File | Upstream URL | GCS Generation | Upstream `Last-Modified` | Pinned Bucket Version | |---|---|---|---|---| | `bert_classifier.tflite` | [`bert_classifier.tflite`](https://storage.googleapis.com/mediapipe-models/text_classifier/bert_classifier/float32/latest/bert_classifier.tflite) | `1682480022043445` | Wed, 26 Apr 2023 03:33:42 GMT | `/1/` | ## License and Citation - License: `apache-2.0` — Stated as Apache License, Version 2.0 (`http://www.apache.org/licenses/LICENSE-2.0`) in the embedded `TFLITE_METADATA` block of the `.tflite` model file(s). ### Citation ```bibtex @inproceedings{sun2020mobilebert, title = {MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices}, author = {Sun, Zhiqing and Yu, Hongkun and Song, Xiaodan and Liu, Renjie and Yang, Yiming and Zhou, Denny}, booktitle = {Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics (ACL)}, year = {2020}, url = {https://arxiv.org/abs/2004.02984} } ```