-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
64 lines (52 loc) · 2.17 KB
/
model.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
from torch import nn
import torch.nn.functional as F
import torch
from transformers import BertTokenizer, BertModel
with open("config.json") as json_file:
config = json.load(json_file)
class SentimentClassifier(nn.Module):
def __init__(self, n_classes):
super(SentimentClassifier, self).__init__()
self.bert = BertModel.from_pretrained(config["BERT_MODEL"])
self.drop = nn.Dropout(p=0.3)
self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
def forward(self, input_ids, attention_mask):
_, pooled_output = self.bert(input_ids=input_ids, attention_mask=attention_mask)
output = self.drop(pooled_output)
return self.out(output)
class Model:
def __init__(self):
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.tokenizer = BertTokenizer.from_pretrained(config["BERT_MODEL"])
classifier = SentimentClassifier(len(config["CLASS_NAMES"]))
classifier.load_state_dict(
torch.load(config["PRE_TRAINED_MODEL"], map_location=self.device)
)
classifier = classifier.eval()
self.classifier = classifier.to(self.device)
def predict(self, text):
encoded_text = self.tokenizer.encode_plus(
text,
max_length=config["MAX_SEQUENCE_LEN"],
add_special_tokens=True,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors="pt",
)
input_ids = encoded_text["input_ids"].to(self.device)
attention_mask = encoded_text["attention_mask"].to(self.device)
with torch.no_grad():
probabilities = F.softmax(self.classifier(input_ids, attention_mask), dim=1)
confidence, predicted_class = torch.max(probabilities, dim=1)
predicted_class = predicted_class.cpu().item()
probabilities = probabilities.flatten().cpu().numpy().tolist()
return (
config["CLASS_NAMES"][predicted_class],
confidence,
dict(zip(config["CLASS_NAMES"], probabilities)),
)
model = Model()
def get_model():
return model