Akis Giannoukos commited on
Commit
d3feaf4
·
1 Parent(s): eb07602

Added code to avoid early termination

Browse files
Files changed (1) hide show
  1. app.py +40 -2
app.py CHANGED
@@ -141,6 +141,28 @@ def compute_audio_features(audio_path: str) -> Dict[str, float]:
141
  return {}
142
 
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  def synthesize_tts(text: Optional[str]) -> Optional[str]:
145
  if not text:
146
  return None
@@ -288,7 +310,15 @@ def scoring_agent_infer(chat_history: List[Tuple[str, str]], features: Dict[str,
288
  total = int(sum(scores.values()))
289
  severity = parsed.get("Severity") or severity_from_total(total)
290
  overall_conf = float(parsed.get("Confidence", min(confidences)))
291
- high_risk = bool(parsed.get("High_Risk", False)) or (scores.get("suicidal_thoughts", 0) >= 1)
 
 
 
 
 
 
 
 
292
 
293
  return {
294
  "PHQ9_Scores": scores,
@@ -409,7 +439,15 @@ def process_turn(
409
  total = scoring.get("Total_Score", 0)
410
  severity = scoring.get("Severity", severity_from_total(total))
411
  overall_conf = float(scoring.get("Confidence", min(confidences) if confidences else 0.0))
412
- high_risk = bool(scoring.get("High_Risk", False))
 
 
 
 
 
 
 
 
413
 
414
  meta = {"Severity": severity, "Total_Score": total, "Confidence": overall_conf}
415
 
 
141
  return {}
142
 
143
 
144
+ def detect_explicit_suicidality(text: Optional[str]) -> bool:
145
+ if not text:
146
+ return False
147
+ t = text.lower()
148
+ patterns = [
149
+ r"\bkill myself\b",
150
+ r"\bend my life\b",
151
+ r"\bend it all\b",
152
+ r"\bcommit suicide\b",
153
+ r"\bsuicide\b",
154
+ r"\bself[-\s]?harm\b",
155
+ r"\bhurt myself\b",
156
+ r"\bno reason to live\b",
157
+ r"\bwant to die\b",
158
+ r"\bending it\b",
159
+ ]
160
+ for pat in patterns:
161
+ if re.search(pat, t):
162
+ return True
163
+ return False
164
+
165
+
166
  def synthesize_tts(text: Optional[str]) -> Optional[str]:
167
  if not text:
168
  return None
 
310
  total = int(sum(scores.values()))
311
  severity = parsed.get("Severity") or severity_from_total(total)
312
  overall_conf = float(parsed.get("Confidence", min(confidences)))
313
+ # Conservative high-risk detection: require explicit language or high suicidal_thoughts score
314
+ # Extract last patient message
315
+ last_patient = ""
316
+ for user_text, assistant_text in reversed(chat_history):
317
+ if user_text:
318
+ last_patient = user_text
319
+ break
320
+ explicit_flag = detect_explicit_suicidality(last_patient) or detect_explicit_suicidality(transcript)
321
+ high_risk = bool(explicit_flag or (scores.get("suicidal_thoughts", 0) >= 2))
322
 
323
  return {
324
  "PHQ9_Scores": scores,
 
439
  total = scoring.get("Total_Score", 0)
440
  severity = scoring.get("Severity", severity_from_total(total))
441
  overall_conf = float(scoring.get("Confidence", min(confidences) if confidences else 0.0))
442
+ # Override high-risk to reduce false positives: rely on explicit text or high item score
443
+ # Extract last patient message
444
+ last_patient = ""
445
+ for user_text, assistant_text in reversed(chat_history):
446
+ if user_text:
447
+ last_patient = user_text
448
+ break
449
+ explicit_flag = detect_explicit_suicidality(last_patient)
450
+ high_risk = bool(explicit_flag or (scores.get("suicidal_thoughts", 0) >= 2))
451
 
452
  meta = {"Severity": severity, "Total_Score": total, "Confidence": overall_conf}
453