<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L129-L268) |

Class that performs classification on text.

```python
mp.tasks.text.TextClassifier(
    lib: serial_dispatcher.SerialDispatcher, handle: ctypes.c_void_p
)
```

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.

<br />

<br />

<br />

| ## 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 ||

<br />

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.

## Methods

### `classify`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L223-L246)

```python
classify(
    text: str
) -> `mp.tasks.components.containers.ClassificationResult`
```

Performs classification on the input `text`.

<br />

<br />

<br />

| Args ||
|---|---|
| `text` | The input text. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `TextClassifierResult` object that contains a list of text classifications. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If any of the input arguments is invalid. |
| `RuntimeError` | If text classification failed to run. |

<br />

### `close`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L248-L253)

```python
close()
```

Shuts down the MediaPipe task instance.

### `create_from_model_path`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L173-L193)

```python
@classmethod
create_from_model_path(
    model_path: str
) -> 'TextClassifier'
```

Creates an `TextClassifier` object from a TensorFlow Lite model and the default `TextClassifierOptions`.

<br />

<br />

<br />

| Args ||
|---|---|
| `model_path` | Path to the model. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `TextClassifier` object that's created from the model file and the default `TextClassifierOptions`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If failed to create `TextClassifier` object from the provided file such as invalid file path. |
| `RuntimeError` | If other types of error occurred. |

<br />

### `create_from_options`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L195-L221)

```python
@classmethod
create_from_options(
    options: `mp.tasks.text.TextClassifierOptions`
) -> 'TextClassifier'
```

Creates the `TextClassifier` object from text classifier options.

<br />

<br />

<br />

| Args ||
|---|---|
| `options` | Options for the text classifier task. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `TextClassifier` object that's created from `options`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If failed to create `TextClassifier` object from `TextClassifierOptions` such as missing the model. |
| `RuntimeError` | If other types of error occurred. |

<br />

### `__enter__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L255-L257)

```python
__enter__()
```

Returns `self` upon entering the runtime context.

### `__exit__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/text_classifier.py#L259-L265)

```python
__exit__(
    unused_exc_type, unused_exc_value, unused_traceback
)
```

Shuts down the MediaPipe task instance on exit of the context manager.

<br />

<br />

<br />

| Raises ||
|---|---|
| `RuntimeError` | If the MediaPipe TextClassifier task failed to close. |

<br />