-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
33 lines (28 loc) · 1.09 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
from model import ColaModel
from data import DataModule
class ColaPredictor:
def __init__(self, model_path):
self.model_path = model_path
self.model = ColaModel.load_from_checkpoint(model_path)
self.model.eval()
self.model.freeze()
self.processor = DataModule()
self.softmax = torch.nn.Softmax(dim=0)
self.lables = ["unacceptable", "acceptable"]
def predict(self, text):
inference_sample = {"sentence": text}
processed = self.processor.tokenize_data(inference_sample)
logits = self.model(
torch.tensor([processed["input_ids"]]),
torch.tensor([processed["attention_mask"]]),
)
scores = self.softmax(logits[0]).tolist()[0]
predictions = []
for score, label in zip(scores, self.lables):
predictions.append({"label": label, "score": score})
return predictions
if __name__ == "__main__":
sentence = "The boy is sitting on a bench"
predictor = ColaPredictor("./models/best-checkpoint.ckpt")
print(predictor.predict(sentence))