NaimaAqeel commited on
Commit
31e8c7b
·
verified ·
1 Parent(s): 9e83f87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -11
app.py CHANGED
@@ -10,7 +10,7 @@ toxicity_pipeline = pipeline("text-classification", model="martin-ha/toxic-comme
10
 
11
  EMO_THRESHOLD = 0.05 # include even lower confidence emotions
12
 
13
- # Expanded emotion mapping
14
  EMOTION_MAPPING = {
15
  "joy": {
16
  "underlying": ["Relief", "Contentment", "Pride", "Love", "Gratitude", "Hope"],
@@ -41,9 +41,28 @@ EMOTION_MAPPING = {
41
  "underlying": ["Accomplishment", "Self-worth", "Achievement"],
42
  "recommendation": "Celebrate your success while staying humble.",
43
  "polarity": 1
 
 
 
 
 
 
 
 
 
 
44
  }
45
  }
46
 
 
 
 
 
 
 
 
 
 
47
  def create_visualization(emotions):
48
  fig, ax = plt.subplots(figsize=(8,5))
49
  colors = ['#4CAF50' if EMOTION_MAPPING.get(e, {}).get('polarity', 0) > 0
@@ -60,16 +79,31 @@ def create_visualization(emotions):
60
  plt.close()
61
  return f'<img src="data:image/png;base64,{base64.b64encode(buf.read()).decode("utf-8")}">'
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  def analyze_text(text):
64
- # Emotion detection
 
 
 
65
  emo_results = emotion_pipeline(text, top_k=None)
66
  emotions = {e["label"].lower(): e["score"] for e in emo_results if e["score"] > EMO_THRESHOLD}
67
 
68
- # Manual override for specific emotions
69
- if "disappointment" in text.lower():
70
- emotions["disappointment"] = max(emotions.get("disappointment", 0), 0.7)
71
- if "pride" in text.lower():
72
- emotions["pride"] = max(emotions.get("pride", 0), 0.8)
73
 
74
  # Generate detailed analysis
75
  details = ""
@@ -108,7 +142,10 @@ def analyze_text(text):
108
  elif negative_emotions and not positive_emotions:
109
  final_rec = "Consider reaching out for support if these feelings persist."
110
  elif positive_emotions and negative_emotions:
111
- final_rec = "You're experiencing mixed emotions. Acknowledge both sides - celebrate the positive while addressing the negative."
 
 
 
112
  else:
113
  final_rec = "The emotional tone is neutral or complex."
114
 
@@ -127,9 +164,9 @@ iface = gr.Interface(
127
  title="Advanced Emotion Analyzer",
128
  description="Detects complex emotional states including underlying emotions and provides balanced recommendations.",
129
  examples=[
130
- ["I'm proud of my work but disappointed with the recognition"],
131
- ["I feel joyful yet anxious about the future"],
132
- ["This situation makes me both hopeful and fearful"]
133
  ]
134
  )
135
 
 
10
 
11
  EMO_THRESHOLD = 0.05 # include even lower confidence emotions
12
 
13
+ # Expanded emotion mapping with special cases
14
  EMOTION_MAPPING = {
15
  "joy": {
16
  "underlying": ["Relief", "Contentment", "Pride", "Love", "Gratitude", "Hope"],
 
41
  "underlying": ["Accomplishment", "Self-worth", "Achievement"],
42
  "recommendation": "Celebrate your success while staying humble.",
43
  "polarity": 1
44
+ },
45
+ "anxiety": {
46
+ "underlying": ["Worry", "Unease", "Apprehension", "Nervousness"],
47
+ "recommendation": "Try box breathing (4-4-4-4) and focus on preparation.",
48
+ "polarity": -1
49
+ },
50
+ "excitement": {
51
+ "underlying": ["Anticipation", "Eagerness", "Enthusiasm"],
52
+ "recommendation": "Channel this energy into planning and goal-setting!",
53
+ "polarity": 1
54
  }
55
  }
56
 
57
+ # Special emotion triggers
58
+ SPECIAL_EMOTION_TRIGGERS = {
59
+ "anxious": {"emotion": "anxiety", "confidence": 0.8},
60
+ "thrilled": {"emotion": "excitement", "confidence": 0.9},
61
+ "devastated": {"emotion": "grief", "confidence": 0.85},
62
+ "overjoyed": {"emotion": "elation", "confidence": 0.95},
63
+ "apprehensive": {"emotion": "wariness", "confidence": 0.75}
64
+ }
65
+
66
  def create_visualization(emotions):
67
  fig, ax = plt.subplots(figsize=(8,5))
68
  colors = ['#4CAF50' if EMOTION_MAPPING.get(e, {}).get('polarity', 0) > 0
 
79
  plt.close()
80
  return f'<img src="data:image/png;base64,{base64.b64encode(buf.read()).decode("utf-8")}">'
81
 
82
+ def detect_special_emotions(text):
83
+ special_emotions = {}
84
+ text_lower = text.lower()
85
+
86
+ for trigger, data in SPECIAL_EMOTION_TRIGGERS.items():
87
+ if trigger in text_lower:
88
+ emotion = data["emotion"]
89
+ # Only add if not already detected or if our confidence is higher
90
+ if data["confidence"] > special_emotions.get(emotion, 0):
91
+ special_emotions[emotion] = data["confidence"]
92
+
93
+ return special_emotions
94
+
95
  def analyze_text(text):
96
+ # Detect special cases first
97
+ special_emotions = detect_special_emotions(text)
98
+
99
+ # Original emotion detection
100
  emo_results = emotion_pipeline(text, top_k=None)
101
  emotions = {e["label"].lower(): e["score"] for e in emo_results if e["score"] > EMO_THRESHOLD}
102
 
103
+ # Merge with special cases (keeping higher confidence)
104
+ for emo, score in special_emotions.items():
105
+ if score > emotions.get(emo, 0):
106
+ emotions[emo] = score
 
107
 
108
  # Generate detailed analysis
109
  details = ""
 
142
  elif negative_emotions and not positive_emotions:
143
  final_rec = "Consider reaching out for support if these feelings persist."
144
  elif positive_emotions and negative_emotions:
145
+ if "anxiety" in negative_emotions and "excitement" in positive_emotions:
146
+ final_rec = "This exciting change comes with natural nerves. Make lists of what you can control to ease anxiety."
147
+ else:
148
+ final_rec = "You're experiencing mixed emotions. Acknowledge both sides - celebrate the positive while addressing the negative."
149
  else:
150
  final_rec = "The emotional tone is neutral or complex."
151
 
 
164
  title="Advanced Emotion Analyzer",
165
  description="Detects complex emotional states including underlying emotions and provides balanced recommendations.",
166
  examples=[
167
+ ["I'm thrilled about my promotion but anxious about the increased responsibilities"],
168
+ ["I feel overjoyed yet apprehensive about the future"],
169
+ ["This situation makes me both hopeful and devastated"]
170
  ]
171
  )
172