Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -32,9 +32,10 @@ sentiment_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/tether-sentim
|
|
32 |
emotion_pipeline = hf_pipeline(
|
33 |
"text-classification",
|
34 |
model="j-hartmann/emotion-english-distilroberta-base",
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
)
|
39 |
# DARVO model
|
40 |
darvo_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tether-darvo-regressor-v1").to(device)
|
@@ -122,11 +123,25 @@ THREAT_MOTIFS = [
|
|
122 |
]
|
123 |
|
124 |
def get_emotion_profile(text):
|
125 |
-
"""Get emotion profile from text"""
|
126 |
-
|
127 |
-
|
128 |
-
emotions
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
132 |
"""Get emotional tone tag based on emotions and patterns"""
|
@@ -141,7 +156,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
141 |
|
142 |
# 1. Performative Regret
|
143 |
if (
|
144 |
-
sadness > 0.
|
145 |
any(p in patterns for p in ["blame shifting", "guilt tripping", "recovery"]) and
|
146 |
(sentiment == "undermining" or abuse_score > 40)
|
147 |
):
|
@@ -149,7 +164,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
149 |
|
150 |
# 2. Coercive Warmth
|
151 |
if (
|
152 |
-
(joy > 0.
|
153 |
any(p in patterns for p in ["control", "gaslighting"]) and
|
154 |
sentiment == "undermining"
|
155 |
):
|
@@ -157,7 +172,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
157 |
|
158 |
# 3. Cold Invalidation
|
159 |
if (
|
160 |
-
(neutral + disgust) > 0.
|
161 |
any(p in patterns for p in ["dismissiveness", "projection", "obscure language"]) and
|
162 |
sentiment == "undermining"
|
163 |
):
|
@@ -165,7 +180,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
165 |
|
166 |
# 4. Genuine Vulnerability
|
167 |
if (
|
168 |
-
(sadness + fear) > 0.
|
169 |
sentiment == "supportive" and
|
170 |
all(p in ["recovery"] for p in patterns)
|
171 |
):
|
@@ -173,7 +188,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
173 |
|
174 |
# 5. Emotional Threat
|
175 |
if (
|
176 |
-
(anger + disgust) > 0.
|
177 |
any(p in patterns for p in ["control", "insults", "dismissiveness"]) and
|
178 |
sentiment == "undermining"
|
179 |
):
|
@@ -181,7 +196,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
181 |
|
182 |
# 6. Weaponized Sadness
|
183 |
if (
|
184 |
-
sadness > 0.
|
185 |
any(p in patterns for p in ["guilt tripping", "projection"]) and
|
186 |
sentiment == "undermining"
|
187 |
):
|
@@ -189,7 +204,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
189 |
|
190 |
# 7. Toxic Resignation
|
191 |
if (
|
192 |
-
neutral > 0.
|
193 |
any(p in patterns for p in ["dismissiveness", "obscure language"]) and
|
194 |
sentiment == "undermining"
|
195 |
):
|
@@ -197,7 +212,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
197 |
|
198 |
# 8. Aggressive Dismissal
|
199 |
if (
|
200 |
-
anger > 0.
|
201 |
any(p in patterns for p in ["insults", "control"]) and
|
202 |
sentiment == "undermining"
|
203 |
):
|
@@ -205,7 +220,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
205 |
|
206 |
# 9. Deflective Hostility
|
207 |
if (
|
208 |
-
(0.
|
209 |
any(p in patterns for p in ["projection"]) and
|
210 |
sentiment == "undermining"
|
211 |
):
|
@@ -213,7 +228,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
213 |
|
214 |
# 10. Contradictory Gaslight
|
215 |
if (
|
216 |
-
(joy + anger + sadness) > 0.
|
217 |
any(p in patterns for p in ["gaslighting", "contradictory statements"]) and
|
218 |
sentiment == "undermining"
|
219 |
):
|
@@ -221,7 +236,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
221 |
|
222 |
# 11. Forced Accountability Flip
|
223 |
if (
|
224 |
-
(anger + disgust) > 0.
|
225 |
any(p in patterns for p in ["blame shifting", "projection"]) and
|
226 |
sentiment == "undermining"
|
227 |
):
|
@@ -229,7 +244,7 @@ def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
|
229 |
|
230 |
# Emotional Instability Fallback
|
231 |
if (
|
232 |
-
(anger + sadness + disgust) > 0.
|
233 |
sentiment == "undermining"
|
234 |
):
|
235 |
return "emotional instability"
|
|
|
32 |
emotion_pipeline = hf_pipeline(
|
33 |
"text-classification",
|
34 |
model="j-hartmann/emotion-english-distilroberta-base",
|
35 |
+
return_all_scores=True, # Get all emotion scores
|
36 |
+
top_k=None, # Don't limit to top k predictions
|
37 |
+
truncation=True,
|
38 |
+
device=0 if torch.cuda.is_available() else -1
|
39 |
)
|
40 |
# DARVO model
|
41 |
darvo_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tether-darvo-regressor-v1").to(device)
|
|
|
123 |
]
|
124 |
|
125 |
def get_emotion_profile(text):
|
126 |
+
"""Get emotion profile from text with all scores"""
|
127 |
+
try:
|
128 |
+
emotions = emotion_pipeline(text)
|
129 |
+
if isinstance(emotions, list) and isinstance(emotions[0], list):
|
130 |
+
# Extract all scores from the first prediction
|
131 |
+
emotion_scores = emotions[0]
|
132 |
+
# Convert to dictionary with lowercase emotion names
|
133 |
+
return {e['label'].lower(): round(e['score'], 3) for e in emotion_scores}
|
134 |
+
return {}
|
135 |
+
except Exception as e:
|
136 |
+
logger.error(f"Error in get_emotion_profile: {e}")
|
137 |
+
return {
|
138 |
+
"sadness": 0.0,
|
139 |
+
"joy": 0.0,
|
140 |
+
"neutral": 0.0,
|
141 |
+
"disgust": 0.0,
|
142 |
+
"anger": 0.0,
|
143 |
+
"fear": 0.0
|
144 |
+
}
|
145 |
|
146 |
def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
|
147 |
"""Get emotional tone tag based on emotions and patterns"""
|
|
|
156 |
|
157 |
# 1. Performative Regret
|
158 |
if (
|
159 |
+
sadness > 0.3 and
|
160 |
any(p in patterns for p in ["blame shifting", "guilt tripping", "recovery"]) and
|
161 |
(sentiment == "undermining" or abuse_score > 40)
|
162 |
):
|
|
|
164 |
|
165 |
# 2. Coercive Warmth
|
166 |
if (
|
167 |
+
(joy > 0.2 or sadness > 0.3) and
|
168 |
any(p in patterns for p in ["control", "gaslighting"]) and
|
169 |
sentiment == "undermining"
|
170 |
):
|
|
|
172 |
|
173 |
# 3. Cold Invalidation
|
174 |
if (
|
175 |
+
(neutral + disgust) > 0.4 and
|
176 |
any(p in patterns for p in ["dismissiveness", "projection", "obscure language"]) and
|
177 |
sentiment == "undermining"
|
178 |
):
|
|
|
180 |
|
181 |
# 4. Genuine Vulnerability
|
182 |
if (
|
183 |
+
(sadness + fear) > 0.4 and
|
184 |
sentiment == "supportive" and
|
185 |
all(p in ["recovery"] for p in patterns)
|
186 |
):
|
|
|
188 |
|
189 |
# 5. Emotional Threat
|
190 |
if (
|
191 |
+
(anger + disgust) > 0.4 and
|
192 |
any(p in patterns for p in ["control", "insults", "dismissiveness"]) and
|
193 |
sentiment == "undermining"
|
194 |
):
|
|
|
196 |
|
197 |
# 6. Weaponized Sadness
|
198 |
if (
|
199 |
+
sadness > 0.5 and
|
200 |
any(p in patterns for p in ["guilt tripping", "projection"]) and
|
201 |
sentiment == "undermining"
|
202 |
):
|
|
|
204 |
|
205 |
# 7. Toxic Resignation
|
206 |
if (
|
207 |
+
neutral > 0.4 and
|
208 |
any(p in patterns for p in ["dismissiveness", "obscure language"]) and
|
209 |
sentiment == "undermining"
|
210 |
):
|
|
|
212 |
|
213 |
# 8. Aggressive Dismissal
|
214 |
if (
|
215 |
+
anger > 0.4 and
|
216 |
any(p in patterns for p in ["insults", "control"]) and
|
217 |
sentiment == "undermining"
|
218 |
):
|
|
|
220 |
|
221 |
# 9. Deflective Hostility
|
222 |
if (
|
223 |
+
(0.15 < anger < 0.6 or 0.15 < disgust < 0.6) and
|
224 |
any(p in patterns for p in ["projection"]) and
|
225 |
sentiment == "undermining"
|
226 |
):
|
|
|
228 |
|
229 |
# 10. Contradictory Gaslight
|
230 |
if (
|
231 |
+
(joy + anger + sadness) > 0.4 and
|
232 |
any(p in patterns for p in ["gaslighting", "contradictory statements"]) and
|
233 |
sentiment == "undermining"
|
234 |
):
|
|
|
236 |
|
237 |
# 11. Forced Accountability Flip
|
238 |
if (
|
239 |
+
(anger + disgust) > 0.4 and
|
240 |
any(p in patterns for p in ["blame shifting", "projection"]) and
|
241 |
sentiment == "undermining"
|
242 |
):
|
|
|
244 |
|
245 |
# Emotional Instability Fallback
|
246 |
if (
|
247 |
+
(anger + sadness + disgust) > 0.5 and
|
248 |
sentiment == "undermining"
|
249 |
):
|
250 |
return "emotional instability"
|