Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- environment/__init__.py +0 -0
- environment/psycheos_env.py +240 -0
environment/__init__.py
ADDED
|
File without changes
|
environment/psycheos_env.py
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PsycheOS — OpenEnv RL Environment
|
| 3 |
+
Mental Health Crisis POMDP
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import random
|
| 7 |
+
import numpy as np
|
| 8 |
+
from dataclasses import dataclass, field
|
| 9 |
+
from typing import Optional
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
# ── Try importing openenv, fallback to gymnasium ─────────────────────────────
|
| 13 |
+
try:
|
| 14 |
+
import openenv as gym
|
| 15 |
+
BACKEND = "openenv"
|
| 16 |
+
except ImportError:
|
| 17 |
+
try:
|
| 18 |
+
import gymnasium as gym
|
| 19 |
+
BACKEND = "gymnasium"
|
| 20 |
+
except ImportError:
|
| 21 |
+
import gym
|
| 22 |
+
BACKEND = "gym"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@dataclass
|
| 26 |
+
class PatientState:
|
| 27 |
+
distress_level: int = 1 # 1–5
|
| 28 |
+
session_history: list = field(default_factory=list)
|
| 29 |
+
crisis_indicators: dict = field(default_factory=dict)
|
| 30 |
+
context: str = ""
|
| 31 |
+
turn: int = 0
|
| 32 |
+
session: int = 1
|
| 33 |
+
stressors: list = field(default_factory=list)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class PsycheOSEnv:
|
| 37 |
+
"""
|
| 38 |
+
OpenEnv-compliant environment for mental health crisis RL training.
|
| 39 |
+
State: PatientState (POMDP)
|
| 40 |
+
Actions: distress_level (1-5), response (str), escalate (bool)
|
| 41 |
+
Rewards: composite of detection F1, empathy DPO, escalation precision
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
metadata = {"render_modes": ["text"], "name": "PsycheOS-v1"}
|
| 45 |
+
|
| 46 |
+
DISTRESS_TEMPLATES = {
|
| 47 |
+
1: [
|
| 48 |
+
"I've been feeling okay lately, just a bit tired from work.",
|
| 49 |
+
"Things are fine, just managing day to day stuff.",
|
| 50 |
+
"Not much to share, had a decent week overall.",
|
| 51 |
+
],
|
| 52 |
+
2: [
|
| 53 |
+
"I've been a bit down lately, nothing serious but persistent.",
|
| 54 |
+
"Finding it hard to stay motivated. Sleep has been off.",
|
| 55 |
+
"Feeling kind of empty. Can't pinpoint why exactly.",
|
| 56 |
+
],
|
| 57 |
+
3: [
|
| 58 |
+
"I cry almost every day and I don't know why. Everything feels heavy.",
|
| 59 |
+
"I haven't left the house in weeks. I can't make myself care about anything.",
|
| 60 |
+
"My anxiety is out of control. I can't focus on anything. My heart races constantly.",
|
| 61 |
+
],
|
| 62 |
+
4: [
|
| 63 |
+
"I keep thinking about how much better things would be if I just wasn't here.",
|
| 64 |
+
"I've been hurting myself to cope. It's the only thing that helps.",
|
| 65 |
+
"Everyone would be better off without me. I've thought about this a lot.",
|
| 66 |
+
],
|
| 67 |
+
5: [
|
| 68 |
+
"I have a plan. I've thought about it every day this week. I don't see another way.",
|
| 69 |
+
"I already took some pills. I don't know if I want help.",
|
| 70 |
+
"I'm saying goodbye. I just needed to tell someone.",
|
| 71 |
+
]
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
STRESSORS = [
|
| 75 |
+
"job loss", "relationship breakdown", "grief", "academic failure",
|
| 76 |
+
"family conflict", "financial crisis", "chronic illness", "isolation",
|
| 77 |
+
"trauma", "substance use", "housing insecurity"
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
def __init__(self, difficulty: int = 1):
|
| 81 |
+
self.difficulty = difficulty
|
| 82 |
+
self.state: Optional[PatientState] = None
|
| 83 |
+
self.max_turns = 8 + difficulty * 2
|
| 84 |
+
self._episode_reward = 0.0
|
| 85 |
+
self._true_distress = 0
|
| 86 |
+
self._ground_truth_escalate = False
|
| 87 |
+
|
| 88 |
+
def reset(self, seed=None, episode=None):
|
| 89 |
+
if episode:
|
| 90 |
+
self.state = episode
|
| 91 |
+
else:
|
| 92 |
+
self.state = self._generate_episode()
|
| 93 |
+
self._episode_reward = 0.0
|
| 94 |
+
obs = self._observe()
|
| 95 |
+
return obs, {}
|
| 96 |
+
|
| 97 |
+
def step(self, action: dict):
|
| 98 |
+
assert self.state is not None, "Call reset() first"
|
| 99 |
+
|
| 100 |
+
reward = self._compute_reward(action)
|
| 101 |
+
self._episode_reward += reward
|
| 102 |
+
|
| 103 |
+
done = self._check_done(action)
|
| 104 |
+
self.state = self._transition(action)
|
| 105 |
+
|
| 106 |
+
obs = self._observe()
|
| 107 |
+
info = {
|
| 108 |
+
"true_distress": self._true_distress,
|
| 109 |
+
"ground_truth_escalate": self._ground_truth_escalate,
|
| 110 |
+
"episode_reward": self._episode_reward,
|
| 111 |
+
"turn": self.state.turn,
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
return obs, reward, done, False, info
|
| 115 |
+
|
| 116 |
+
def _generate_episode(self) -> PatientState:
|
| 117 |
+
"""Generate a synthetic patient episode based on difficulty."""
|
| 118 |
+
if self.difficulty <= 2:
|
| 119 |
+
level = random.randint(1, 3)
|
| 120 |
+
elif self.difficulty <= 4:
|
| 121 |
+
level = random.randint(2, 4)
|
| 122 |
+
else:
|
| 123 |
+
level = random.randint(3, 5)
|
| 124 |
+
|
| 125 |
+
self._true_distress = level
|
| 126 |
+
self._ground_truth_escalate = level >= 4
|
| 127 |
+
|
| 128 |
+
stressors = random.sample(self.STRESSORS, k=min(self.difficulty, 3))
|
| 129 |
+
history = []
|
| 130 |
+
if self.difficulty >= 3:
|
| 131 |
+
prev_level = max(1, level - random.randint(0, 2))
|
| 132 |
+
prev_msg = random.choice(self.DISTRESS_TEMPLATES[prev_level])
|
| 133 |
+
history.append({"turn": 0, "message": prev_msg, "distress": prev_level})
|
| 134 |
+
|
| 135 |
+
opening = random.choice(self.DISTRESS_TEMPLATES[level])
|
| 136 |
+
context = f"[Stressors: {', '.join(stressors)}] {opening}"
|
| 137 |
+
|
| 138 |
+
return PatientState(
|
| 139 |
+
distress_level=level,
|
| 140 |
+
session_history=history,
|
| 141 |
+
crisis_indicators={
|
| 142 |
+
"suicidal_ideation": level >= 4,
|
| 143 |
+
"self_harm": level >= 4,
|
| 144 |
+
"hopelessness": level >= 3,
|
| 145 |
+
},
|
| 146 |
+
context=context,
|
| 147 |
+
stressors=stressors,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
def _observe(self) -> dict:
|
| 151 |
+
return {
|
| 152 |
+
"message": self.state.context,
|
| 153 |
+
"session_history": self.state.session_history[-3:],
|
| 154 |
+
"turn": self.state.turn,
|
| 155 |
+
"session": self.state.session,
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
def _compute_reward(self, action: dict) -> float:
|
| 159 |
+
pred_distress = action.get("distress_level", 1)
|
| 160 |
+
response = action.get("response", "")
|
| 161 |
+
escalate = action.get("escalate", False)
|
| 162 |
+
|
| 163 |
+
# Detection reward (F1 proxy)
|
| 164 |
+
detection_err = abs(pred_distress - self._true_distress)
|
| 165 |
+
r_detect = max(0.0, 1.0 - detection_err * 0.3)
|
| 166 |
+
|
| 167 |
+
# Empathy reward (length + keyword heuristic as DPO proxy)
|
| 168 |
+
keywords = ["hear", "understand", "safe", "here", "feel", "support", "care"]
|
| 169 |
+
keyword_score = sum(1 for k in keywords if k in response.lower()) / len(keywords)
|
| 170 |
+
length_score = min(1.0, len(response.split()) / 40)
|
| 171 |
+
r_empathy = 0.6 * keyword_score + 0.4 * length_score
|
| 172 |
+
|
| 173 |
+
# Escalation reward (10x penalty on false negatives)
|
| 174 |
+
if self._ground_truth_escalate and not escalate:
|
| 175 |
+
r_escalate = -10.0
|
| 176 |
+
elif not self._ground_truth_escalate and escalate:
|
| 177 |
+
r_escalate = -1.0
|
| 178 |
+
elif self._ground_truth_escalate and escalate:
|
| 179 |
+
r_escalate = 3.0
|
| 180 |
+
else:
|
| 181 |
+
r_escalate = 1.0
|
| 182 |
+
|
| 183 |
+
return 0.4 * r_detect + 0.4 * r_empathy + 0.2 * r_escalate
|
| 184 |
+
|
| 185 |
+
def _check_done(self, action: dict) -> bool:
|
| 186 |
+
if action.get("escalate", False):
|
| 187 |
+
return True
|
| 188 |
+
if self.state.turn >= self.max_turns:
|
| 189 |
+
return True
|
| 190 |
+
return False
|
| 191 |
+
|
| 192 |
+
def _transition(self, action: dict) -> PatientState:
|
| 193 |
+
self.state.session_history.append({
|
| 194 |
+
"turn": self.state.turn,
|
| 195 |
+
"message": self.state.context,
|
| 196 |
+
"distress": self._true_distress,
|
| 197 |
+
"response": action.get("response", ""),
|
| 198 |
+
})
|
| 199 |
+
self.state.turn += 1
|
| 200 |
+
|
| 201 |
+
# Slight distress drift based on empathy quality
|
| 202 |
+
response = action.get("response", "")
|
| 203 |
+
keywords = ["hear", "understand", "safe", "here"]
|
| 204 |
+
good_response = any(k in response.lower() for k in keywords)
|
| 205 |
+
if good_response and self._true_distress > 1:
|
| 206 |
+
self._true_distress = max(1, self._true_distress - 1)
|
| 207 |
+
|
| 208 |
+
return self.state
|
| 209 |
+
|
| 210 |
+
def generate_episodes(self, n: int = 100) -> list:
|
| 211 |
+
"""Generate n synthetic episodes for training."""
|
| 212 |
+
episodes = []
|
| 213 |
+
for i in range(n):
|
| 214 |
+
diff = min(5, 1 + i // (n // 5))
|
| 215 |
+
old_diff = self.difficulty
|
| 216 |
+
self.difficulty = diff
|
| 217 |
+
obs, _ = self.reset()
|
| 218 |
+
episodes.append({
|
| 219 |
+
"observation": obs,
|
| 220 |
+
"true_distress": self._true_distress,
|
| 221 |
+
"ground_truth_escalate": self._ground_truth_escalate,
|
| 222 |
+
"difficulty": diff,
|
| 223 |
+
})
|
| 224 |
+
self.difficulty = old_diff
|
| 225 |
+
return episodes
|
| 226 |
+
|
| 227 |
+
def render(self, mode="text"):
|
| 228 |
+
if self.state:
|
| 229 |
+
print(f"Turn {self.state.turn} | Distress: {self._true_distress}/5")
|
| 230 |
+
print(f"Message: {self.state.context[:100]}...")
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
# Register with gym if available
|
| 234 |
+
try:
|
| 235 |
+
gym.register(
|
| 236 |
+
id="PsycheOS-v1",
|
| 237 |
+
entry_point="environment.psycheos_env:PsycheOSEnv",
|
| 238 |
+
)
|
| 239 |
+
except Exception:
|
| 240 |
+
pass
|