This API expects a TFLite model with (optional) TFLite Model Metadata that
contains the mandatory (described below) input tensors, output tensor,
and the optional (but recommended) category labels as AssociatedFiles with
type
TENSOR_AXIS_LABELS per output classification tensor. Metadata is required for
models with int32 input tensors because it contains the input process unit
for the model's Tokenizer. No metadata is required for models with string
input tensors.
Input tensors
(kTfLiteInt32)
3 input tensors of size [batch_size x bert_max_seq_len] representing
the input ids, segment ids, and mask ids
or 1 input tensor of size [batch_size x max_seq_len] representing the
input ids
or (kTfLiteString)
1 input tensor that is shapeless or has shape [1] containing the input
string
At least one output tensor with:
(kTfLiteFloat32/kBool)
[1 x N] array with N represents the number of categories.
optional (but recommended) category labels as AssociatedFiles with type
TENSOR_AXIS_LABELS, containing one label per line. The first such
AssociatedFile (if any) is used to fill the category_name field of the
results. The display_name field is filled from the AssociatedFile (if
any) whose locale matches the display_names_locale field of the
TextClassifierOptions used at creation time ("en" by default, i.e.
English). If none of these are available, only the index field of the
results will be filled.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-05-01 UTC."],[],[],null,["# mp.tasks.text.TextClassifier\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L87-L187) |\n\nClass that performs classification on text. \n\n mp.tasks.text.TextClassifier(\n graph_config: ../../../mp/calculators/core/constant_side_packet_calculator_pb2/mediapipe_dot_framework_dot_calculator__pb2/CalculatorGraphConfig\n ) -\u003e None\n\nThis API expects a TFLite model with (optional) TFLite Model Metadata that\ncontains the mandatory (described below) input tensors, output tensor,\nand the optional (but recommended) category labels as AssociatedFiles with\ntype\nTENSOR_AXIS_LABELS per output classification tensor. Metadata is required for\nmodels with int32 input tensors because it contains the input process unit\nfor the model's Tokenizer. No metadata is required for models with string\ninput tensors.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Input tensors ------------- ||\n|---|---|\n| (kTfLiteInt32) \u003cbr /\u003e - 3 input tensors of size `[batch_size x bert_max_seq_len]` representing the input ids, segment ids, and mask ids - or 1 input tensor of size `[batch_size x max_seq_len]` representing the input ids or (kTfLiteString) - 1 input tensor that is shapeless or has shape \\[1\\] containing the input string ||\n\n\u003cbr /\u003e\n\nAt least one output tensor with:\n(kTfLiteFloat32/kBool)\n\n- `[1 x N]` array with `N` represents the number of categories.\n- optional (but recommended) category labels as AssociatedFiles with type TENSOR_AXIS_LABELS, containing one label per line. The first such AssociatedFile (if any) is used to fill the `category_name` field of the results. The `display_name` field is filled from the AssociatedFile (if any) whose locale matches the `display_names_locale` field of the `TextClassifierOptions` used at creation time (\"en\" by default, i.e. English). If none of these are available, only the `index` field of the results will be filled.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|---------------------------------------------|\n| `graph_config` | The mediapipe text task graph config proto. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `classify`\n\n[View source](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L166-L187) \n\n classify(\n text: str\n ) -\u003e ../../../mp/tasks/audio/AudioClassifierResult\n\nPerforms classification on the input [`text`](https://www.tensorflow.org/text/api_docs/python/text).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------------------------------------------------------------|-----------------|\n| [`text`](https://www.tensorflow.org/text/api_docs/python/text) | The input text. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A `TextClassifierResult` object that contains a list of text classifications. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|----------------|-------------------------------------------|\n| `ValueError` | If any of the input arguments is invalid. |\n| `RuntimeError` | If text classification failed to run. |\n\n\u003cbr /\u003e\n\n### `close`\n\n[View source](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/core/base_text_task_api.py#L36-L42) \n\n close() -\u003e None\n\nShuts down the mediapipe text task instance.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|----------------|---------------------------------------------|\n| `RuntimeError` | If the mediapipe text task failed to close. |\n\n\u003cbr /\u003e\n\n### `create_from_model_path`\n\n[View source](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L121-L139) \n\n @classmethod\n create_from_model_path(\n model_path: str\n ) -\u003e 'TextClassifier'\n\nCreates an `TextClassifier` object from a TensorFlow Lite model and the default `TextClassifierOptions`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------------|--------------------|\n| `model_path` | Path to the model. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| `TextClassifier` object that's created from the model file and the default `TextClassifierOptions`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|----------------|-----------------------------------------------------------------------------------------------|\n| `ValueError` | If failed to create `TextClassifier` object from the provided file such as invalid file path. |\n| `RuntimeError` | If other types of error occurred. |\n\n\u003cbr /\u003e\n\n### `create_from_options`\n\n[View source](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L141-L164) \n\n @classmethod\n create_from_options(\n options: ../../../mp/tasks/text/TextClassifierOptions\n ) -\u003e 'TextClassifier'\n\nCreates the `TextClassifier` object from text classifier options.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|-----------|---------------------------------------|\n| `options` | Options for the text classifier task. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| `TextClassifier` object that's created from `options`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|----------------|-----------------------------------------------------------------------------------------------------|\n| `ValueError` | If failed to create `TextClassifier` object from `TextClassifierOptions` such as missing the model. |\n| `RuntimeError` | If other types of error occurred. |\n\n\u003cbr /\u003e\n\n### `__enter__`\n\n[View source](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/core/base_text_task_api.py#L44-L46) \n\n __enter__()\n\nReturns `self` upon entering the runtime context.\n\n### `__exit__`\n\n[View source](https://github.com/google/mediapipe/blob/master/mediapipe/tasks/python/text/core/base_text_task_api.py#L48-L54) \n\n __exit__(\n unused_exc_type, unused_exc_value, unused_traceback\n )\n\nShuts down the mediapipe text task instance on exit of the context manager.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|----------------|---------------------------------------------|\n| `RuntimeError` | If the mediapipe text task failed to close. |\n\n\u003cbr /\u003e"]]