SamanthaStorm commited on
Commit
9f4751d
·
verified ·
1 Parent(s): 6139d49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -2
app.py CHANGED
@@ -154,13 +154,42 @@ ESCALATION_QUESTIONS = [
154
  ]
155
 
156
  def analyze_single_message(text, thresholds, motif_flags):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
158
  with torch.no_grad():
159
  outputs = model(**inputs)
160
  scores = torch.sigmoid(outputs.logits.squeeze(0)).numpy()
161
- threshold_labels = [label for label, score in zip(LABELS, scores) if score > thresholds[label]]
 
162
  top_patterns = sorted([(label, score) for label, score in zip(LABELS, scores)], key=lambda x: x[1], reverse=True)[:2]
163
- return (np.mean([score for _, score in top_patterns]) * 100, threshold_labels, top_patterns, 0.0, {"label": "supportive"})
 
 
 
 
 
 
 
 
 
 
164
 
165
  # --- Composite Analysis with Escalation Quiz ---
166
  def analyze_composite(msg1, msg2, msg3, *answers_and_none):
 
154
  ]
155
 
156
  def analyze_single_message(text, thresholds, motif_flags):
157
+ motif_hits, matched_phrases = detect_motifs(text)
158
+ sentiment = {"label": "undermining"} # fallback in case sentiment fails
159
+ try:
160
+ input_ids = sentiment_tokenizer(f"emotion: {text}", return_tensors="pt").input_ids
161
+ with torch.no_grad():
162
+ outputs = sentiment_model.generate(input_ids)
163
+ emotion = sentiment_tokenizer.decode(outputs[0], skip_special_tokens=True).strip().lower()
164
+ sentiment = {
165
+ "label": EMOTION_TO_SENTIMENT.get(emotion, "undermining"),
166
+ "emotion": emotion
167
+ }
168
+ except:
169
+ sentiment["emotion"] = "unknown"
170
+
171
+ sentiment_score = 0.5 if sentiment["label"] == "undermining" else 0.0
172
+ contradiction_flag = detect_contradiction(text)
173
+ motifs = [phrase for _, phrase in matched_phrases]
174
+
175
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
176
  with torch.no_grad():
177
  outputs = model(**inputs)
178
  scores = torch.sigmoid(outputs.logits.squeeze(0)).numpy()
179
+
180
+ labels = [label for label, score in zip(LABELS, scores) if score > thresholds[label]]
181
  top_patterns = sorted([(label, score) for label, score in zip(LABELS, scores)], key=lambda x: x[1], reverse=True)[:2]
182
+ pattern_labels = [label for label, _ in top_patterns]
183
+
184
+ darvo_score = calculate_darvo_score(pattern_labels, 0.0, sentiment_score, motifs, contradiction_flag)
185
+
186
+ return (
187
+ np.mean([score for _, score in top_patterns]) * 100,
188
+ labels,
189
+ top_patterns,
190
+ darvo_score,
191
+ sentiment
192
+ )
193
 
194
  # --- Composite Analysis with Escalation Quiz ---
195
  def analyze_composite(msg1, msg2, msg3, *answers_and_none):