-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbert_binary_classifier.py
More file actions
43 lines (35 loc) · 1.37 KB
/
Copy pathbert_binary_classifier.py
File metadata and controls
43 lines (35 loc) · 1.37 KB
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
34
35
36
37
38
39
40
41
42
43
import numpy as np
import torch
from transformers import BertTokenizer
class BertBinaryClassifier:
def __init__(self):
self.model = torch.load('/models/bert_binary.pt')
self.tokenizer = BertTokenizer.from_pretrained('cointegrated/rubert-tiny')
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.max_len = 512
self.out_features = self.model.bert.encoder.layer[1].output.dense.out_features
self.model.to(self.device)
def predict(self, text):
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
truncation=True,
padding='max_length',
return_attention_mask=True,
return_tensors='pt',
)
out = {
'text': text,
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten()
}
input_ids = out["input_ids"].to(self.device)
attention_mask = out["attention_mask"].to(self.device)
outputs = self.model(
input_ids=input_ids.unsqueeze(0),
attention_mask=attention_mask.unsqueeze(0)
)
prediction = torch.argmax(outputs.logits, dim=1).cpu().numpy()[0]
return prediction