INNER CODE UNIT · Python
predict
graviraja/100-Days-of-NLP · applications/classification/toxic_comment_classification/app/app.py:47
def predict(sentence, bpe_model, model):
model.eval()
if isinstance(sentence, str):
sentence = preprocess(sentence)
tokens = bpe_tokenizer(sentence)
else:
tokens = [int(token) for token in sentence]
src_indexes = tokens
# convert to tensor format
# since the inference is done on single sentence, batch size is 1
src_tensor = torch.LongTensor(src_indexes).unsqueeze(1).to(device)
# src_tensor => [seq_len, 1]
src_length = torch.LongTensor([len(src_indexes)])
# src_length => [1]