
ai.djl.huggingface.translator.TokenClassificationBatchTranslator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tokenizers Show documentation
Show all versions of tokenizers Show documentation
Deep Java Library (DJL) NLP utilities for Huggingface tokenizers
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.huggingface.translator;
import ai.djl.huggingface.tokenizers.Encoding;
import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
import ai.djl.modality.nlp.translator.NamedEntity;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.translate.Batchifier;
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;
import ai.djl.util.JsonUtils;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
/** The translator for Huggingface token classification model. */
public class TokenClassificationBatchTranslator
implements NoBatchifyTranslator {
private HuggingFaceTokenizer tokenizer;
private Batchifier batchifier;
private PretrainedConfig config;
TokenClassificationBatchTranslator(HuggingFaceTokenizer tokenizer, Batchifier batchifier) {
this.tokenizer = tokenizer;
this.batchifier = batchifier;
}
/** {@inheritDoc} */
@Override
public void prepare(TranslatorContext ctx) throws IOException {
Path path = ctx.getModel().getModelPath();
Path file = path.resolve("config.json");
try (Reader reader = Files.newBufferedReader(file)) {
config = JsonUtils.GSON.fromJson(reader, PretrainedConfig.class);
}
}
/** {@inheritDoc} */
@Override
public NDList processInput(TranslatorContext ctx, String[] inputs) {
NDManager manager = ctx.getNDManager();
Encoding[] encodings = tokenizer.batchEncode(inputs);
ctx.setAttachment("encodings", encodings);
NDList[] batch = new NDList[encodings.length];
for (int i = 0; i < encodings.length; ++i) {
batch[i] = encodings[i].toNDList(manager, false);
}
return batchifier.batchify(batch);
}
/** {@inheritDoc} */
@Override
public NamedEntity[][] processOutput(TranslatorContext ctx, NDList list) {
NDList[] batch = batchifier.unbatchify(list);
Encoding[] encodings = (Encoding[]) ctx.getAttachment("encodings");
NamedEntity[][] ret = new NamedEntity[encodings.length][];
for (int i = 0; i < encodings.length; ++i) {
ret[i] = TokenClassificationTranslator.toNamedEntities(encodings[i], batch[i], config);
}
return ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy