Package ai.djl.huggingface.tokenizers
Class HuggingFaceTokenizer
- java.lang.Object
-
- ai.djl.util.NativeResource<java.lang.Long>
-
- ai.djl.huggingface.tokenizers.HuggingFaceTokenizer
-
- All Implemented Interfaces:
ai.djl.modality.nlp.preprocess.TextProcessor,ai.djl.modality.nlp.preprocess.Tokenizer,java.lang.AutoCloseable
public final class HuggingFaceTokenizer extends ai.djl.util.NativeResource<java.lang.Long> implements ai.djl.modality.nlp.preprocess.TokenizerHuggingFaceTokenizeris a Huggingface tokenizer implementation of theTokenizerinterface that converts sentences into token.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHuggingFaceTokenizer.BuilderThe builder for creating huggingface tokenizer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String[]batchDecode(long[][] batchIds)Returns the decoded Strings from the input batch ids.java.lang.String[]batchDecode(long[][] batchIds, boolean skipSpecialTokens)Returns the decoded Strings from the input batch ids.Encoding[]batchEncode(ai.djl.util.PairList<java.lang.String,java.lang.String> inputs)Returns theEncodingof the input text pair in batch.Encoding[]batchEncode(ai.djl.util.PairList<java.lang.String,java.lang.String> inputs, boolean addSpecialTokens)Returns theEncodingof the input text pair in batch.Encoding[]batchEncode(java.lang.String[] inputs)Returns theEncodingof the input sentence in batch.Encoding[]batchEncode(java.lang.String[] inputs, boolean addSpecialTokens)Returns theEncodingof the input sentence in batch.Encoding[]batchEncode(java.util.List<java.lang.String> inputs)Returns theEncodingof the input sentence in batch.Encoding[]batchEncode(java.util.List<java.lang.String> inputs, boolean addSpecialTokens)Returns theEncodingof the input sentence in batch.static HuggingFaceTokenizer.Builderbuilder()Creates a builder to build aHuggingFaceTokenizer.static HuggingFaceTokenizer.Builderbuilder(java.util.Map<java.lang.String,?> arguments)Creates a builder to build aHuggingFaceTokenizer.java.lang.StringbuildSentence(java.util.List<java.lang.String> tokens)voidclose()java.lang.Stringdecode(long[] ids)Returns the decoded String from the input ids.java.lang.Stringdecode(long[] ids, boolean skipSpecialTokens)Returns the decoded String from the input ids.voidenableBatch()Sets padding and truncation to true for batching.Encodingencode(java.lang.String text)Returns theEncodingof the input sentence.Encodingencode(java.lang.String[] inputs)Returns theEncodingof the input sentences.Encodingencode(java.lang.String[] inputs, boolean addSpecialTokens)Returns theEncodingof the input sentences.Encodingencode(java.lang.String text, boolean addSpecialTokens)Returns theEncodingof the input sentence.Encodingencode(java.lang.String text, java.lang.String textPair)Returns theEncodingof the input sentence.Encodingencode(java.lang.String text, java.lang.String textPair, boolean addSpecialTokens)Returns theEncodingof the input sentence.Encodingencode(java.util.List<java.lang.String> inputs)Returns theEncodingof the input sentences.Encodingencode(java.util.List<java.lang.String> inputs, boolean addSpecialTokens)Returns theEncodingof the input sentences.protected voidfinalize()static HuggingFaceTokenizernewInstance(java.io.InputStream is, java.util.Map<java.lang.String,java.lang.String> options)Create a pre-trainedHuggingFaceTokenizerinstance fromInputStream.static HuggingFaceTokenizernewInstance(java.lang.String name)Creates a pre-trainedHuggingFaceTokenizerinstance from huggingface hub.static HuggingFaceTokenizernewInstance(java.lang.String identifier, java.util.Map<java.lang.String,java.lang.String> options)Create a pre-trainedHuggingFaceTokenizerinstance from huggingface hub.static HuggingFaceTokenizernewInstance(java.nio.file.Path modelPath)Create a pre-trainedHuggingFaceTokenizerinstance from existing models.static HuggingFaceTokenizernewInstance(java.nio.file.Path vocab, java.nio.file.Path merges, java.util.Map<java.lang.String,java.lang.String> options)Create a pre-trained BPEHuggingFaceTokenizerinstance from existing models.static HuggingFaceTokenizernewInstance(java.nio.file.Path modelPath, java.util.Map<java.lang.String,java.lang.String> options)Create a pre-trainedHuggingFaceTokenizerinstance from existing models.java.util.List<java.lang.String>tokenize(java.lang.String sentence)
-
-
-
Method Detail
-
newInstance
public static HuggingFaceTokenizer newInstance(java.lang.String name)
Creates a pre-trainedHuggingFaceTokenizerinstance from huggingface hub.- Parameters:
name- the name of the huggingface tokenizer- Returns:
- a
HuggingFaceTokenizerinstance
-
newInstance
public static HuggingFaceTokenizer newInstance(java.lang.String identifier, java.util.Map<java.lang.String,java.lang.String> options)
Create a pre-trainedHuggingFaceTokenizerinstance from huggingface hub.- Parameters:
identifier- the identifier of the huggingface tokenizeroptions- tokenizer options- Returns:
- a
HuggingFaceTokenizerinstance
-
newInstance
public static HuggingFaceTokenizer newInstance(java.nio.file.Path modelPath) throws java.io.IOException
Create a pre-trainedHuggingFaceTokenizerinstance from existing models.- Parameters:
modelPath- the directory or file path of the model location- Returns:
- a
HuggingFaceTokenizerinstance - Throws:
java.io.IOException- when IO operation fails in loading a resource
-
newInstance
public static HuggingFaceTokenizer newInstance(java.nio.file.Path modelPath, java.util.Map<java.lang.String,java.lang.String> options) throws java.io.IOException
Create a pre-trainedHuggingFaceTokenizerinstance from existing models.- Parameters:
modelPath- the directory or file path of the model locationoptions- tokenizer options- Returns:
- a
HuggingFaceTokenizerinstance - Throws:
java.io.IOException- when IO operation fails in loading a resource
-
newInstance
public static HuggingFaceTokenizer newInstance(java.nio.file.Path vocab, java.nio.file.Path merges, java.util.Map<java.lang.String,java.lang.String> options) throws java.io.IOException
Create a pre-trained BPEHuggingFaceTokenizerinstance from existing models.- Parameters:
vocab- the BPE vocabulary filemerges- the BPE merges fileoptions- tokenizer options- Returns:
- a
HuggingFaceTokenizerinstance - Throws:
java.io.IOException- when IO operation fails in loading a resource
-
newInstance
public static HuggingFaceTokenizer newInstance(java.io.InputStream is, java.util.Map<java.lang.String,java.lang.String> options) throws java.io.IOException
Create a pre-trainedHuggingFaceTokenizerinstance fromInputStream.- Parameters:
is-InputStreamoptions- tokenizer options- Returns:
- a
HuggingFaceTokenizerinstance - Throws:
java.io.IOException- when IO operation fails in loading a resource
-
tokenize
public java.util.List<java.lang.String> tokenize(java.lang.String sentence)
- Specified by:
tokenizein interfaceai.djl.modality.nlp.preprocess.Tokenizer
-
buildSentence
public java.lang.String buildSentence(java.util.List<java.lang.String> tokens)
- Specified by:
buildSentencein interfaceai.djl.modality.nlp.preprocess.Tokenizer
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Overrides:
closein classai.djl.util.NativeResource<java.lang.Long>
-
encode
public Encoding encode(java.lang.String text, boolean addSpecialTokens)
Returns theEncodingof the input sentence.- Parameters:
text- the input sentenceaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input sentence
-
encode
public Encoding encode(java.lang.String text)
Returns theEncodingof the input sentence.- Parameters:
text- the input sentence- Returns:
- the
Encodingof the input sentence
-
encode
public Encoding encode(java.lang.String text, java.lang.String textPair, boolean addSpecialTokens)
Returns theEncodingof the input sentence.- Parameters:
text- the input sentencetextPair- the second input sentenceaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input sentence
-
encode
public Encoding encode(java.lang.String text, java.lang.String textPair)
Returns theEncodingof the input sentence.- Parameters:
text- the input sentencetextPair- the second input sentence- Returns:
- the
Encodingof the input sentence
-
encode
public Encoding encode(java.util.List<java.lang.String> inputs, boolean addSpecialTokens)
Returns theEncodingof the input sentences.- Parameters:
inputs- the input sentencesaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input sentences
-
encode
public Encoding encode(java.util.List<java.lang.String> inputs)
Returns theEncodingof the input sentences.- Parameters:
inputs- the input sentences- Returns:
- the
Encodingof the input sentences
-
encode
public Encoding encode(java.lang.String[] inputs, boolean addSpecialTokens)
Returns theEncodingof the input sentences.- Parameters:
inputs- the input sentencesaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input sentences
-
encode
public Encoding encode(java.lang.String[] inputs)
Returns theEncodingof the input sentences.- Parameters:
inputs- the input sentences- Returns:
- the
Encodingof the input sentences
-
batchEncode
public Encoding[] batchEncode(java.util.List<java.lang.String> inputs, boolean addSpecialTokens)
Returns theEncodingof the input sentence in batch.- Parameters:
inputs- the batch of input sentenceaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input sentence in batch
-
batchEncode
public Encoding[] batchEncode(java.util.List<java.lang.String> inputs)
Returns theEncodingof the input sentence in batch.- Parameters:
inputs- the batch of input sentence- Returns:
- the
Encodingof the input sentence in batch
-
batchEncode
public Encoding[] batchEncode(java.lang.String[] inputs, boolean addSpecialTokens)
Returns theEncodingof the input sentence in batch.- Parameters:
inputs- the batch of input sentenceaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input sentence in batch
-
batchEncode
public Encoding[] batchEncode(java.lang.String[] inputs)
Returns theEncodingof the input sentence in batch.- Parameters:
inputs- the batch of input sentence- Returns:
- the
Encodingof the input sentence in batch
-
batchEncode
public Encoding[] batchEncode(ai.djl.util.PairList<java.lang.String,java.lang.String> inputs, boolean addSpecialTokens)
Returns theEncodingof the input text pair in batch.- Parameters:
inputs- the batch of input text pairaddSpecialTokens- whether to encode the sequence with special tokens relative to their model- Returns:
- the
Encodingof the input text pair in batch
-
batchEncode
public Encoding[] batchEncode(ai.djl.util.PairList<java.lang.String,java.lang.String> inputs)
Returns theEncodingof the input text pair in batch.- Parameters:
inputs- the batch of input text pair- Returns:
- the
Encodingof the input text pair in batch
-
decode
public java.lang.String decode(long[] ids, boolean skipSpecialTokens)Returns the decoded String from the input ids.- Parameters:
ids- the input idsskipSpecialTokens- whether to remove special tokens in the decoding- Returns:
- the decoded String from the input ids
-
decode
public java.lang.String decode(long[] ids)
Returns the decoded String from the input ids.- Parameters:
ids- the input ids- Returns:
- the decoded String from the input ids
-
batchDecode
public java.lang.String[] batchDecode(long[][] batchIds, boolean skipSpecialTokens)Returns the decoded Strings from the input batch ids.- Parameters:
batchIds- the batch of id sequences to decodeskipSpecialTokens- whether to remove special tokens in the decoding- Returns:
- the decoded Strings from the input batch ids
-
batchDecode
public java.lang.String[] batchDecode(long[][] batchIds)
Returns the decoded Strings from the input batch ids.- Parameters:
batchIds- the batch of id sequences to decode- Returns:
- the decoded Strings from the input batch ids
-
enableBatch
public void enableBatch()
Sets padding and truncation to true for batching.
-
builder
public static HuggingFaceTokenizer.Builder builder()
Creates a builder to build aHuggingFaceTokenizer.- Returns:
- a new builder
-
builder
public static HuggingFaceTokenizer.Builder builder(java.util.Map<java.lang.String,?> arguments)
Creates a builder to build aHuggingFaceTokenizer.- Parameters:
arguments- the models' arguments- Returns:
- a new builder
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
-