Spaces:
Running
Running
Update emotion_detection.py
Browse files- emotion_detection.py +25 -16
emotion_detection.py
CHANGED
@@ -1,15 +1,16 @@
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
from transformers_interpret import SequenceClassificationExplainer
|
3 |
import torch
|
|
|
4 |
|
5 |
|
6 |
class EmotionDetection:
|
7 |
"""
|
8 |
Emotion Detection on text data.
|
9 |
Attributes:
|
10 |
-
tokenizer:
|
11 |
-
model:
|
12 |
-
explainer:
|
13 |
"""
|
14 |
|
15 |
def __init__(self):
|
@@ -18,7 +19,7 @@ class EmotionDetection:
|
|
18 |
self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)
|
19 |
self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)
|
20 |
|
21 |
-
#
|
22 |
self.emoji_map = {
|
23 |
"joy": "π",
|
24 |
"anger": "π ",
|
@@ -26,7 +27,7 @@ class EmotionDetection:
|
|
26 |
"sadness": "π’"
|
27 |
}
|
28 |
|
29 |
-
#
|
30 |
self.explanation_map = {
|
31 |
"joy": "The person is happy or excited.",
|
32 |
"anger": "The person is upset or angry.",
|
@@ -36,23 +37,31 @@ class EmotionDetection:
|
|
36 |
|
37 |
def justify(self, text):
|
38 |
"""
|
39 |
-
|
40 |
Parameters:
|
41 |
-
text (str):
|
42 |
Returns:
|
43 |
-
html (str):
|
44 |
"""
|
45 |
word_attributions = self.explainer(text)
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
return html
|
48 |
|
49 |
def classify(self, text):
|
50 |
"""
|
51 |
-
|
52 |
Parameters:
|
53 |
-
text (str):
|
54 |
Returns:
|
55 |
-
result (str):
|
56 |
"""
|
57 |
tokens = self.tokenizer.encode_plus(text, return_tensors='pt')
|
58 |
outputs = self.model(**tokens)
|
@@ -72,12 +81,12 @@ class EmotionDetection:
|
|
72 |
|
73 |
def run(self, text):
|
74 |
"""
|
75 |
-
|
76 |
Parameters:
|
77 |
-
text (str):
|
78 |
Returns:
|
79 |
-
result (str):
|
80 |
-
html (str): HTML
|
81 |
"""
|
82 |
result = self.classify(text)
|
83 |
html = self.justify(text)
|
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
from transformers_interpret import SequenceClassificationExplainer
|
3 |
import torch
|
4 |
+
import os
|
5 |
|
6 |
|
7 |
class EmotionDetection:
|
8 |
"""
|
9 |
Emotion Detection on text data.
|
10 |
Attributes:
|
11 |
+
tokenizer: Hugging Face Tokenizer instance
|
12 |
+
model: Hugging Face Sequence Classification model
|
13 |
+
explainer: SequenceClassificationExplainer instance for model interpretability
|
14 |
"""
|
15 |
|
16 |
def __init__(self):
|
|
|
19 |
self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)
|
20 |
self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)
|
21 |
|
22 |
+
# Emoji map for friendly display
|
23 |
self.emoji_map = {
|
24 |
"joy": "π",
|
25 |
"anger": "π ",
|
|
|
27 |
"sadness": "π’"
|
28 |
}
|
29 |
|
30 |
+
# Simple explanation map
|
31 |
self.explanation_map = {
|
32 |
"joy": "The person is happy or excited.",
|
33 |
"anger": "The person is upset or angry.",
|
|
|
37 |
|
38 |
def justify(self, text):
|
39 |
"""
|
40 |
+
Generate HTML visualization of word attributions for emotion.
|
41 |
Parameters:
|
42 |
+
text (str): Input text
|
43 |
Returns:
|
44 |
+
html (str): HTML string with justification visualization
|
45 |
"""
|
46 |
word_attributions = self.explainer(text)
|
47 |
+
html_path = "justification_output.html"
|
48 |
+
self.explainer.visualize(html_path)
|
49 |
+
|
50 |
+
# Read from file
|
51 |
+
with open(html_path, "r", encoding="utf-8") as f:
|
52 |
+
html = f.read()
|
53 |
+
|
54 |
+
# Clean up file
|
55 |
+
os.remove(html_path)
|
56 |
return html
|
57 |
|
58 |
def classify(self, text):
|
59 |
"""
|
60 |
+
Classify the main emotion in the input text.
|
61 |
Parameters:
|
62 |
+
text (str): Input text
|
63 |
Returns:
|
64 |
+
result (str): Friendly output with emoji and short explanation
|
65 |
"""
|
66 |
tokens = self.tokenizer.encode_plus(text, return_tensors='pt')
|
67 |
outputs = self.model(**tokens)
|
|
|
81 |
|
82 |
def run(self, text):
|
83 |
"""
|
84 |
+
Perform classification and justification.
|
85 |
Parameters:
|
86 |
+
text (str): Input text
|
87 |
Returns:
|
88 |
+
result (str): Emotion classification result
|
89 |
+
html (str): Justification HTML
|
90 |
"""
|
91 |
result = self.classify(text)
|
92 |
html = self.justify(text)
|