Stanford-TH commited on
Commit
533e9d1
·
verified ·
1 Parent(s): f709d81

Update EmotionClassifier/EmotionPredictor.py

Browse files
Files changed (1) hide show
  1. EmotionClassifier/EmotionPredictor.py +43 -40
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
- def forward(self, payload):
14
- length_sentences = []
15
- sentences = []
16
- sorted_tensors = []
17
- tokens = self.tokenizer.encode(payload, return_tensors="pt", return_overflowing_tokens=True, stride=10, max_length=1096, truncation=True, padding=True)
18
- for i in range(len(tokens)):
19
- tokens_list = self.tokenizer.convert_ids_to_tokens(tokens[i])
20
- tokens_string = self.tokenizer.convert_tokens_to_string([token for token in tokens_list if token not in ['<s>', '</s>', self.tokenizer.pad_token]])
21
- length_sentences.append(len(tokens_string.split()))
22
- sentences.append(tokens_string)
23
-
24
- length_sentences = torch.tensor(length_sentences)
25
- weights = length_sentences/length_sentences.sum()
26
- weights.to(self.device)
27
- del length_sentences,tokens
28
- emotions = ['anger', 'disgust', 'fear', 'inspiration', 'joy', 'love', 'neutral', 'sadness', 'suprise']
29
- predictions = self.classifier(sentences, emotions, multi_label=True)
30
- print(predictions)
31
- emotions.sort()
32
- for prediction in predictions:
33
- item = dict(zip(prediction['labels'],prediction['scores']))
34
- sorted_scores = [item[label] for label in emotions]
35
- sorted_tensors.append(sorted_scores)
36
- sorted_tensors = torch.tensor(sorted_tensors)
37
- sorted_tensors.to(self.device)
38
- weighted_scores = torch.mul(weights.unsqueeze(1),sorted_tensors).to(self.device)
39
- weighted_scores = weighted_scores.sum(dim=0)
40
- return weighted_scores.cpu().numpy()
 
 
 
 
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]