Spaces:
Sleeping
Sleeping
Update EmotionClassifier/EmotionPredictor.py
Browse files
EmotionClassifier/EmotionPredictor.py
CHANGED
@@ -1,40 +1,43 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
from transformers import pipeline
|
4 |
-
from huggingface_hub import PyTorchModelHubMixin
|
5 |
-
|
6 |
-
class EmotionPredictor(nn.Module,PyTorchModelHubMixin):
|
7 |
-
def __init__(self):
|
8 |
-
super(EmotionPredictor, self).__init__()
|
9 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
self.classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli",device=self.device)
|
11 |
-
self.tokenizer = self.classifier.tokenizer
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
sorted_scores
|
35 |
-
|
36 |
-
sorted_tensors
|
37 |
-
sorted_tensors.to(self.device)
|
38 |
-
weighted_scores =
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from transformers import pipeline
|
4 |
+
from huggingface_hub import PyTorchModelHubMixin
|
5 |
+
|
6 |
+
class EmotionPredictor(nn.Module,PyTorchModelHubMixin):
|
7 |
+
def __init__(self):
|
8 |
+
super(EmotionPredictor, self).__init__()
|
9 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
self.classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli",device=self.device)
|
11 |
+
self.tokenizer = self.classifier.tokenizer
|
12 |
+
self.emotions = ['anger', 'disgust', 'fear', 'inspiration', 'joy', 'love', 'neutral', 'sadness', 'suprise']
|
13 |
+
self.emotions.sort()
|
14 |
+
|
15 |
+
def forward(self, payload):
|
16 |
+
length_sentences = []
|
17 |
+
sentences = []
|
18 |
+
sorted_tensors = []
|
19 |
+
tokens = self.tokenizer.encode(payload, return_tensors="pt", return_overflowing_tokens=True, stride=10, max_length=1096, truncation=True, padding=True)
|
20 |
+
for i in range(len(tokens)):
|
21 |
+
tokens_list = self.tokenizer.convert_ids_to_tokens(tokens[i])
|
22 |
+
tokens_string = self.tokenizer.convert_tokens_to_string([token for token in tokens_list if token not in ['<s>', '</s>', self.tokenizer.pad_token]])
|
23 |
+
length_sentences.append(len(tokens_string.split()))
|
24 |
+
sentences.append(tokens_string)
|
25 |
+
|
26 |
+
length_sentences = torch.tensor(length_sentences)
|
27 |
+
weights = length_sentences/length_sentences.sum()
|
28 |
+
weights.to(self.device)
|
29 |
+
del length_sentences,tokens
|
30 |
+
predictions = self.classifier(sentences, self.emotions, multi_label=True)
|
31 |
+
for prediction in predictions:
|
32 |
+
item = dict(zip(prediction['labels'],prediction['scores']))
|
33 |
+
sorted_scores = [item[label] for label in self.emotions]
|
34 |
+
sorted_tensors.append(sorted_scores)
|
35 |
+
sorted_tensors = torch.tensor(sorted_tensors)
|
36 |
+
sorted_tensors.to(self.device)
|
37 |
+
weighted_scores = torch.mul(weights.unsqueeze(1),sorted_tensors).to(self.device)
|
38 |
+
weighted_scores = weighted_scores.sum(dim=0)
|
39 |
+
top_scores,top_indices = torch.topk(weighted_scores,3)
|
40 |
+
emotions_dict = {}
|
41 |
+
for X,Y in zip(top_scores,top_indices):
|
42 |
+
emotions_dict.update({self.emotions[Y.item()]:X.item()})
|
43 |
+
return [emotions_dict]
|