Upload handler.py
Browse files- handler.py +20 -24
handler.py
CHANGED
@@ -1,37 +1,33 @@
|
|
1 |
import torch
|
2 |
from transformers import BertTokenizer, BertForSequenceClassification
|
|
|
3 |
|
4 |
class SentimentModel:
|
5 |
def __init__(self, model_path):
|
6 |
self.tokenizer = BertTokenizer.from_pretrained(model_path)
|
7 |
self.model = BertForSequenceClassification.from_pretrained(model_path)
|
8 |
self.model.eval()
|
|
|
|
|
9 |
|
10 |
-
def predict(self, texts):
|
11 |
-
max_length =
|
12 |
-
batch_size = 64
|
13 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
-
self.model.to(device)
|
15 |
-
|
16 |
-
probabilities = []
|
17 |
encodings = self.tokenizer(texts, truncation=True, padding=True, max_length=max_length, return_tensors="pt")
|
18 |
-
input_ids = encodings['input_ids']
|
19 |
-
attention_masks = encodings['attention_mask']
|
20 |
|
21 |
-
dataset = torch.utils.data.TensorDataset(input_ids, attention_masks)
|
22 |
-
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size)
|
23 |
-
|
24 |
with torch.no_grad():
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
strong_probs = probs[:, 1].tolist()
|
35 |
-
probabilities.extend(strong_probs)
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
from transformers import BertTokenizer, BertForSequenceClassification
|
3 |
+
from typing import List
|
4 |
|
5 |
class SentimentModel:
|
6 |
def __init__(self, model_path):
|
7 |
self.tokenizer = BertTokenizer.from_pretrained(model_path)
|
8 |
self.model = BertForSequenceClassification.from_pretrained(model_path)
|
9 |
self.model.eval()
|
10 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
self.model.to(self.device)
|
12 |
|
13 |
+
def predict(self, texts: List[str]) -> List[float]:
|
14 |
+
max_length = 128
|
|
|
|
|
|
|
|
|
|
|
15 |
encodings = self.tokenizer(texts, truncation=True, padding=True, max_length=max_length, return_tensors="pt")
|
16 |
+
input_ids = encodings['input_ids'].to(self.device)
|
17 |
+
attention_masks = encodings['attention_mask'].to(self.device)
|
18 |
|
|
|
|
|
|
|
19 |
with torch.no_grad():
|
20 |
+
outputs = self.model(input_ids, attention_mask=attention_masks)
|
21 |
+
logits = outputs.logits
|
22 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
23 |
+
strong_probs = probs[:, 1].tolist() # Probability of the "strong" class (class 1)
|
24 |
+
|
25 |
+
return strong_probs
|
26 |
+
|
27 |
+
# Initialize the model
|
28 |
+
model = SentimentModel(".")
|
|
|
|
|
29 |
|
30 |
+
def infer(inputs):
|
31 |
+
texts = [inputs[i]['data'] for i in range(len(inputs))]
|
32 |
+
probabilities = model.predict(texts)
|
33 |
+
return [{"sentiment intensity": prob} for prob in probabilities]
|