mp.tasks.text.LanguageDetector

Class that predicts the language of an input text.

This API expects a TFLite model with TFLite Model Metadata that contains the mandatory (described below) input tensors, output tensor, and the language codes in an AssociatedFile.

(kTfLiteString)

  • 1 input tensor that is scalar or has shape [1] containing the input string.

(kTfLiteFloat32)

  • 1 output tensor of shape[1 x N] where N is the number of languages.

graph_config The mediapipe text task graph config proto.

Methods

close

View source

Shuts down the mediapipe text task instance.

Raises
RuntimeError If the mediapipe text task failed to close.

create_from_model_path

View source

Creates an LanguageDetector object from a TensorFlow Lite model and the default LanguageDetectorOptions.

Args
model_path Path to the model.

Returns
LanguageDetector object that's created from the model file and the default LanguageDetectorOptions.

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

create_from_options

View source

Creates the LanguageDetector object from language detector options.

Args
options Options for the language detector task.

Returns
LanguageDetector object that's created from options.

Raises
ValueError If failed to create LanguageDetector object from LanguageDetectorOptions such as missing the model.
RuntimeError If other types of error occurred.

detect

View source

Predicts the language of the input text.

Args
text The input text.

Returns
A LanguageDetectorResult object that contains a list of languages and scores.

Raises
ValueError If any of the input arguments is invalid.
RuntimeError If language detection failed to run.

__enter__

View source

Returns self upon entering the runtime context.

__exit__

View source

Shuts down the mediapipe text task instance on exit of the context manager.

Raises
RuntimeError If the mediapipe text task failed to close.