-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
22 lines (20 loc) · 714 Bytes
/
Copy pathpredict.py
File metadata and controls
22 lines (20 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch
from model import Model, TextPreprocessing, id2label
class Predictor:
def __init__(
self,
model_checkpoint,
vocab_path):
self.model = Model.load_from_checkpoint(model_checkpoint)
self.model.eval()
self.text_preprocessing = TextPreprocessing(
self.model.config.max_seq_len,
vocab_save_file=vocab_path
)
self.id2label = id2label
def predict(self, text: str):
outputs = self.text_preprocessing.text2tensor(text)
outputs = self.model(outputs)
max_prob, label_id = torch.max(outputs, dim=-1)
label = self.id2label[label_id.item()]
return label, max_prob.item()