Harshb11 commited on
Commit
08d98c4
Β·
verified Β·
1 Parent(s): 11fa374

Update emotion_detection.py

Browse files
Files changed (1) hide show
  1. 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: An instance of Hugging Face Tokenizer
11
- model: An instance of Hugging Face Model
12
- explainer: An instance of SequenceClassificationExplainer from Transformers interpret
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
- # Friendly emoji mapping
22
  self.emoji_map = {
23
  "joy": "😊",
24
  "anger": "😠",
@@ -26,7 +27,7 @@ class EmotionDetection:
26
  "sadness": "😒"
27
  }
28
 
29
- # Friendly explanation mapping
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
- Get html annotation for displaying emotion justification over text.
40
  Parameters:
41
- text (str): The user input string for emotion justification
42
  Returns:
43
- html (str): html string for plotting emotion prediction justification
44
  """
45
  word_attributions = self.explainer(text)
46
- html = self.explainer.visualize(return_html=True) # Changed to return HTML string
 
 
 
 
 
 
 
 
47
  return html
48
 
49
  def classify(self, text):
50
  """
51
- Recognize Emotion in text (simplified).
52
  Parameters:
53
- text (str): The user input string to perform emotion classification on
54
  Returns:
55
- result (str): User-friendly emotion label with emoji and explanation
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
- Classify and Justify Emotion in text.
76
  Parameters:
77
- text (str): The user input string to perform emotion classification on
78
  Returns:
79
- result (str): Friendly emotion classification output
80
- html (str): HTML visualization string for model justification
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)