SamanthaStorm commited on
Commit
a20729d
·
verified ·
1 Parent(s): 162c1c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -21
app.py CHANGED
@@ -87,9 +87,10 @@ ocr_reader = easyocr.Reader(["en"], gpu=False)
87
  # ——— 4) Emotional-Tone Tagging —————————————————————————————————————————————
88
  def get_emotional_tone_tag(emotion_profile, patterns, text_lower):
89
  """
90
- Assigns one of 18 nuanced tone categories based on emotion scores, patterns, and text.
 
91
  """
92
- # unpack all emotion scores before any rules
93
  sadness = emotion_profile.get("sadness", 0)
94
  joy = emotion_profile.get("joy", 0)
95
  neutral = emotion_profile.get("neutral", 0)
@@ -98,76 +99,96 @@ def get_emotional_tone_tag(emotion_profile, patterns, text_lower):
98
  fear = emotion_profile.get("fear", 0)
99
  surprise = emotion_profile.get("surprise", 0)
100
 
101
- # 0. Support override
102
- if any(k in text_lower for k in ["support", "hope", "grace"]):
 
 
 
 
 
 
 
103
  return "supportive"
104
 
105
  # 1. Performative Regret
106
- if sadness > 0.4 and any(p in patterns for p in ["blame shifting", "guilt tripping", "recovery phase"]):
 
107
  return "performative regret"
108
 
109
  # 2. Coercive Warmth
110
- if (joy > 0.3 or sadness > 0.4) and any(p in patterns for p in ["control", "gaslighting"]):
 
111
  return "coercive warmth"
112
 
113
  # 3. Cold Invalidation
114
- if (neutral + disgust) > 0.5 and any(p in patterns for p in ["dismissiveness", "projection", "obscure language"]):
 
115
  return "cold invalidation"
116
 
117
  # 4. Genuine Vulnerability
118
- if (sadness + fear) > 0.5 and all(p == "recovery phase" for p in patterns):
 
119
  return "genuine vulnerability"
120
 
121
  # 5. Emotional Threat
122
- if (anger + disgust) > 0.5 and any(p in patterns for p in ["control", "threat", "insults", "dismissiveness"]):
 
123
  return "emotional threat"
124
 
125
  # 6. Weaponized Sadness
126
- if sadness > 0.6 and any(p in patterns for p in ["guilt tripping", "projection"]):
 
127
  return "weaponized sadness"
128
 
129
  # 7. Toxic Resignation
130
- if neutral > 0.5 and any(p in patterns for p in ["dismissiveness", "obscure language"]):
131
  return "toxic resignation"
132
 
133
  # 8. Indignant Reproach
134
- if anger > 0.5 and any(p in patterns for p in ["guilt tripping", "contradictory statements"]):
 
135
  return "indignant reproach"
136
 
137
  # 9. Confrontational
138
- if anger > 0.6 and patterns:
139
  return "confrontational"
140
 
141
  # 10. Passive Aggression
142
- if neutral > 0.6 and any(p in patterns for p in ["dismissiveness", "projection"]):
143
  return "passive aggression"
144
 
145
  # 11. Sarcastic Mockery
146
- if joy > 0.3 and "insults" in patterns:
147
  return "sarcastic mockery"
148
 
149
  # 12. Menacing Threat
150
- if fear > 0.3 and "threat" in patterns:
151
  return "menacing threat"
152
 
153
  # 13. Pleading Concern
154
- if sadness > 0.3 and any(k in text_lower for k in APOLOGY_KEYWORDS) and not patterns:
 
 
155
  return "pleading concern"
156
 
157
  # 14. Fear-mongering
158
- if (fear + disgust) > 0.5 and "projection" in patterns:
 
159
  return "fear-mongering"
160
 
161
  # 15. Disbelieving Accusation
162
- if surprise > 0.3 and "blame shifting" in patterns:
 
163
  return "disbelieving accusation"
164
 
165
  # 16. Empathetic Solidarity
166
- if joy > 0.2 and sadness > 0.2 and not patterns:
 
 
167
  return "empathetic solidarity"
168
 
169
  # 17. Assertive Boundary
170
- if anger > 0.4 and "control" in patterns:
171
  return "assertive boundary"
172
 
173
  # 18. Stonewalling
 
87
  # ——— 4) Emotional-Tone Tagging —————————————————————————————————————————————
88
  def get_emotional_tone_tag(emotion_profile, patterns, text_lower):
89
  """
90
+ Assigns one of 18 nuanced tone categories based on
91
+ model scores, NRC-EmoLex counts, detected patterns, and text.
92
  """
93
+ # unpack model emotion scores
94
  sadness = emotion_profile.get("sadness", 0)
95
  joy = emotion_profile.get("joy", 0)
96
  neutral = emotion_profile.get("neutral", 0)
 
99
  fear = emotion_profile.get("fear", 0)
100
  surprise = emotion_profile.get("surprise", 0)
101
 
102
+ # count lexicon hits for the big five
103
+ words = text_lower.split()
104
+ lex_counts = {
105
+ emo: sum(EMOLEX.get(w, {}).get(emo, 0) for w in words)
106
+ for emo in ["anger","joy","sadness","fear","disgust"]
107
+ }
108
+
109
+ # 0. Support override: explicit keywords or detected joy in lexicon
110
+ if any(k in text_lower for k in ["support", "hope", "grace"]) or lex_counts["joy"] > 0:
111
  return "supportive"
112
 
113
  # 1. Performative Regret
114
+ if (sadness > 0.4 or lex_counts["sadness"] > 1) \
115
+ and any(p in patterns for p in ["blame shifting","guilt tripping","recovery phase"]):
116
  return "performative regret"
117
 
118
  # 2. Coercive Warmth
119
+ if ((joy > 0.3 or lex_counts["joy"] > 1) or (sadness > 0.4 or lex_counts["sadness"] > 1)) \
120
+ and any(p in patterns for p in ["control","gaslighting"]):
121
  return "coercive warmth"
122
 
123
  # 3. Cold Invalidation
124
+ if ((neutral + disgust) > 0.5 or lex_counts["disgust"] > 1) \
125
+ and any(p in patterns for p in ["dismissiveness","projection","obscure language"]):
126
  return "cold invalidation"
127
 
128
  # 4. Genuine Vulnerability
129
+ if ((sadness + fear) > 0.5 or (lex_counts["sadness"] + lex_counts["fear"]) > 1) \
130
+ and all(p == "recovery phase" for p in patterns):
131
  return "genuine vulnerability"
132
 
133
  # 5. Emotional Threat
134
+ if ((anger + disgust) > 0.5 or (lex_counts["anger"] + lex_counts["disgust"]) > 1) \
135
+ and any(p in patterns for p in ["control","threat","insults","dismissiveness"]):
136
  return "emotional threat"
137
 
138
  # 6. Weaponized Sadness
139
+ if (sadness > 0.6 or lex_counts["sadness"] > 2) \
140
+ and any(p in patterns for p in ["guilt tripping","projection"]):
141
  return "weaponized sadness"
142
 
143
  # 7. Toxic Resignation
144
+ if (neutral > 0.5) and any(p in patterns for p in ["dismissiveness","obscure language"]):
145
  return "toxic resignation"
146
 
147
  # 8. Indignant Reproach
148
+ if (anger > 0.5 or lex_counts["anger"] > 1) \
149
+ and any(p in patterns for p in ["guilt tripping","contradictory statements"]):
150
  return "indignant reproach"
151
 
152
  # 9. Confrontational
153
+ if (anger > 0.6 or lex_counts["anger"] > 1) and patterns:
154
  return "confrontational"
155
 
156
  # 10. Passive Aggression
157
+ if (neutral > 0.6) and any(p in patterns for p in ["dismissiveness","projection"]):
158
  return "passive aggression"
159
 
160
  # 11. Sarcastic Mockery
161
+ if (joy > 0.3 or lex_counts["joy"] > 1) and "insults" in patterns:
162
  return "sarcastic mockery"
163
 
164
  # 12. Menacing Threat
165
+ if (fear > 0.3 or lex_counts["fear"] > 0) and "threat" in patterns:
166
  return "menacing threat"
167
 
168
  # 13. Pleading Concern
169
+ if (sadness > 0.3 or lex_counts["sadness"] > 0) \
170
+ and any(k in text_lower for k in APOLOGY_KEYWORDS) \
171
+ and not patterns:
172
  return "pleading concern"
173
 
174
  # 14. Fear-mongering
175
+ if ((fear + disgust) > 0.5 or (lex_counts["fear"] + lex_counts["disgust"]) > 1) \
176
+ and "projection" in patterns:
177
  return "fear-mongering"
178
 
179
  # 15. Disbelieving Accusation
180
+ if (surprise > 0.3 or lex_counts.get("surprise",0) > 0) \
181
+ and "blame shifting" in patterns:
182
  return "disbelieving accusation"
183
 
184
  # 16. Empathetic Solidarity
185
+ if (joy > 0.2 and sadness > 0.2) \
186
+ and (lex_counts["joy"] > 0 and lex_counts["sadness"] > 0) \
187
+ and not patterns:
188
  return "empathetic solidarity"
189
 
190
  # 17. Assertive Boundary
191
+ if (anger > 0.4 or lex_counts["anger"] > 0) and "control" in patterns:
192
  return "assertive boundary"
193
 
194
  # 18. Stonewalling