Classifier API for NLClassification tasks with Bert models, categorizes string into different classes. The API expects a Bert based TFLite model with metadata populated.
The metadata should contain the following information:
- 1 input_process_unit for Wordpiece/Sentencepiece Tokenizer.
- 3 input tensors with names "ids", "mask" and "segment_ids".
- 1 output tensor of type float32[1, 2], with a optionally attached label file. If a label file is attached, the file should be a plain text file with one label per line, the number of labels should match the number of categories the model outputs.
Nested Classes
class | BertNLClassifier.BertNLClassifierOptions | Options to configure BertNLClassifier. |
Public Methods
Inherited Methods
Public Methods
public List<Category> classify (String text)
Performs classification on a string input, returns classified Category
s.
Parameters
text | input text to the model. |
---|
Returns
- A list of Category results.
public static BertNLClassifier createFromBuffer (ByteBuffer modelBuffer)
Creates BertNLClassifier
with a model buffer and default BertNLClassifier.BertNLClassifierOptions
.
Parameters
modelBuffer | a direct ByteBuffer or a MappedByteBuffer of the model |
---|
Returns
- a
BertNLClassifier
instance
Throws
IllegalArgumentException | if the model buffer is not a direct ByteBuffer or a
MappedByteBuffer |
---|---|
IllegalStateException | if there is an internal error |
RuntimeException | if there is an otherwise unspecified error |
public static BertNLClassifier createFromBufferAndOptions (ByteBuffer modelBuffer, BertNLClassifier.BertNLClassifierOptions options)
Creates BertNLClassifier
with a model buffer and BertNLClassifier.BertNLClassifierOptions
.
Parameters
modelBuffer | a direct ByteBuffer or a MappedByteBuffer of the model |
---|---|
options | to configure the classifier |
Returns
- a
BertNLClassifier
instance
Throws
IllegalArgumentException | if the model buffer is not a direct ByteBuffer or a
MappedByteBuffer |
---|---|
IllegalStateException | if there is an internal error |
RuntimeException | if there is an otherwise unspecified error |
public static BertNLClassifier createFromFile (Context context, String modelPath)
Creates BertNLClassifier
from a model file with metadata and default BertNLClassifier.BertNLClassifierOptions
.
Parameters
context | Android context |
---|---|
modelPath | Path to the classification model |
Returns
- a
BertNLClassifier
instance
Throws
IOException | If model file fails to load |
---|---|
IllegalArgumentException | if an argument is invalid |
IllegalStateException | if there is an internal error |
RuntimeException | if there is an otherwise unspecified error |
public static BertNLClassifier createFromFile (File modelFile)
Creates BertNLClassifier
from a File
object with metadata and default BertNLClassifier.BertNLClassifierOptions
.
Parameters
modelFile | The classification model File instance |
---|
Returns
- a
BertNLClassifier
instance
Throws
IOException | If model file fails to load |
---|---|
IllegalArgumentException | if an argument is invalid |
IllegalStateException | if there is an internal error |
RuntimeException | if there is an otherwise unspecified error |
public static BertNLClassifier createFromFileAndOptions (File modelFile, BertNLClassifier.BertNLClassifierOptions options)
Creates BertNLClassifier
from a File
object with metadata and BertNLClassifier.BertNLClassifierOptions
.
Parameters
modelFile | The classification model File instance |
---|---|
options | to configure the classifier |
Returns
- a
BertNLClassifier
instance
Throws
IOException | If model file fails to load |
---|---|
IllegalArgumentException | if an argument is invalid |
IllegalStateException | if there is an internal error |
RuntimeException | if there is an otherwise unspecified error |
public static BertNLClassifier createFromFileAndOptions (Context context, String modelPath, BertNLClassifier.BertNLClassifierOptions options)
Creates BertNLClassifier
from a model file with metadata and BertNLClassifier.BertNLClassifierOptions
.
Parameters
context | Android context. |
---|---|
modelPath | Path to the classification model |
options | to configure the classifier |
Returns
- a
BertNLClassifier
instance
Throws
IOException | If model file fails to load |
---|---|
IllegalArgumentException | if an argument is invalid |
IllegalStateException | if there is an internal error |
RuntimeException | if there is an otherwise unspecified error |