sarahzephyr commited on
Commit
00a89f5
Β·
verified Β·
1 Parent(s): e67dd78

Upload 2 files

Browse files
Files changed (2) hide show
  1. app/__init__.py +0 -0
  2. app/streamlit_app.py +448 -0
app/__init__.py ADDED
File without changes
app/streamlit_app.py ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sys
3
+ import os
4
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5
+
6
+ from agents.graph import run_psycheos
7
+ from memory.faiss_store import MemoryStore
8
+ import time
9
+ import random
10
+
11
+ st.set_page_config(
12
+ page_title="PsycheOS",
13
+ page_icon="🧠",
14
+ layout="wide",
15
+ initial_sidebar_state="expanded"
16
+ )
17
+
18
+ # ── CSS ──────────────────────────────────────────────────────────────────────
19
+ st.markdown("""
20
+ <style>
21
+ @import url('https://fonts.googleapis.com/css2?family=DM+Serif+Display:ital@0;1&family=DM+Mono:wght@300;400;500&display=swap');
22
+
23
+ :root {
24
+ --bg: #0a0a0f;
25
+ --surface: #12121a;
26
+ --border: #1e1e2e;
27
+ --accent: #7c6aff;
28
+ --accent2: #ff6a8a;
29
+ --green: #4ade80;
30
+ --amber: #fbbf24;
31
+ --red: #f87171;
32
+ --text: #e2e2f0;
33
+ --muted: #6b6b8a;
34
+ }
35
+
36
+ html, body, [class*="css"] {
37
+ font-family: 'DM Mono', monospace;
38
+ background: var(--bg);
39
+ color: var(--text);
40
+ }
41
+
42
+ .stApp { background: var(--bg); }
43
+
44
+ .main-title {
45
+ font-family: 'DM Serif Display', serif;
46
+ font-size: 2.8rem;
47
+ background: linear-gradient(135deg, #7c6aff, #ff6a8a, #7c6aff);
48
+ background-size: 200%;
49
+ -webkit-background-clip: text;
50
+ -webkit-text-fill-color: transparent;
51
+ animation: shimmer 3s infinite;
52
+ margin: 0;
53
+ }
54
+
55
+ @keyframes shimmer {
56
+ 0% { background-position: 0% 50%; }
57
+ 50% { background-position: 100% 50%; }
58
+ 100% { background-position: 0% 50%; }
59
+ }
60
+
61
+ .subtitle {
62
+ color: var(--muted);
63
+ font-size: 0.75rem;
64
+ letter-spacing: 0.2em;
65
+ text-transform: uppercase;
66
+ margin-top: 0.25rem;
67
+ }
68
+
69
+ .agent-card {
70
+ background: var(--surface);
71
+ border: 1px solid var(--border);
72
+ border-radius: 12px;
73
+ padding: 1rem;
74
+ margin-bottom: 0.75rem;
75
+ transition: all 0.3s ease;
76
+ }
77
+
78
+ .agent-card.active {
79
+ border-color: var(--accent);
80
+ box-shadow: 0 0 20px rgba(124,106,255,0.2);
81
+ }
82
+
83
+ .agent-card.crisis {
84
+ border-color: var(--red);
85
+ box-shadow: 0 0 20px rgba(248,113,113,0.3);
86
+ animation: pulse-red 1s infinite;
87
+ }
88
+
89
+ @keyframes pulse-red {
90
+ 0%, 100% { box-shadow: 0 0 20px rgba(248,113,113,0.3); }
91
+ 50% { box-shadow: 0 0 35px rgba(248,113,113,0.6); }
92
+ }
93
+
94
+ .agent-name {
95
+ font-size: 0.65rem;
96
+ letter-spacing: 0.15em;
97
+ text-transform: uppercase;
98
+ color: var(--muted);
99
+ margin-bottom: 0.4rem;
100
+ }
101
+
102
+ .agent-value {
103
+ font-size: 1.1rem;
104
+ font-weight: 500;
105
+ color: var(--text);
106
+ }
107
+
108
+ .distress-bar {
109
+ height: 6px;
110
+ border-radius: 3px;
111
+ margin-top: 0.5rem;
112
+ transition: width 0.5s ease;
113
+ }
114
+
115
+ .chat-msg-user {
116
+ background: var(--surface);
117
+ border: 1px solid var(--border);
118
+ border-radius: 16px 16px 4px 16px;
119
+ padding: 0.8rem 1.2rem;
120
+ margin: 0.5rem 0;
121
+ max-width: 75%;
122
+ margin-left: auto;
123
+ font-size: 0.9rem;
124
+ }
125
+
126
+ .chat-msg-ai {
127
+ background: linear-gradient(135deg, rgba(124,106,255,0.1), rgba(255,106,138,0.05));
128
+ border: 1px solid rgba(124,106,255,0.3);
129
+ border-radius: 16px 16px 16px 4px;
130
+ padding: 0.8rem 1.2rem;
131
+ margin: 0.5rem 0;
132
+ max-width: 80%;
133
+ font-size: 0.9rem;
134
+ line-height: 1.6;
135
+ }
136
+
137
+ .escalation-banner {
138
+ background: linear-gradient(135deg, rgba(248,113,113,0.2), rgba(251,191,36,0.1));
139
+ border: 1px solid var(--red);
140
+ border-radius: 12px;
141
+ padding: 1rem 1.5rem;
142
+ margin: 1rem 0;
143
+ font-size: 0.85rem;
144
+ }
145
+
146
+ .metric-pill {
147
+ display: inline-block;
148
+ padding: 0.2rem 0.7rem;
149
+ border-radius: 20px;
150
+ font-size: 0.7rem;
151
+ letter-spacing: 0.1em;
152
+ text-transform: uppercase;
153
+ margin: 0.2rem;
154
+ }
155
+
156
+ .reward-card {
157
+ background: var(--surface);
158
+ border: 1px solid var(--border);
159
+ border-radius: 12px;
160
+ padding: 1.2rem;
161
+ text-align: center;
162
+ margin-bottom: 0.5rem;
163
+ }
164
+
165
+ .reward-number {
166
+ font-family: 'DM Serif Display', serif;
167
+ font-size: 2rem;
168
+ color: var(--accent);
169
+ }
170
+
171
+ div[data-testid="stTextInput"] input {
172
+ background: var(--surface) !important;
173
+ border: 1px solid var(--border) !important;
174
+ border-radius: 12px !important;
175
+ color: var(--text) !important;
176
+ font-family: 'DM Mono', monospace !important;
177
+ padding: 0.8rem 1rem !important;
178
+ }
179
+
180
+ div[data-testid="stTextInput"] input:focus {
181
+ border-color: var(--accent) !important;
182
+ box-shadow: 0 0 0 2px rgba(124,106,255,0.2) !important;
183
+ }
184
+
185
+ .stButton > button {
186
+ background: linear-gradient(135deg, var(--accent), #9b5de5) !important;
187
+ border: none !important;
188
+ border-radius: 10px !important;
189
+ color: white !important;
190
+ font-family: 'DM Mono', monospace !important;
191
+ font-size: 0.8rem !important;
192
+ letter-spacing: 0.1em !important;
193
+ padding: 0.6rem 1.5rem !important;
194
+ transition: all 0.2s !important;
195
+ }
196
+
197
+ .stButton > button:hover {
198
+ transform: translateY(-1px) !important;
199
+ box-shadow: 0 4px 20px rgba(124,106,255,0.4) !important;
200
+ }
201
+
202
+ .oversight-flag {
203
+ background: rgba(251,191,36,0.1);
204
+ border-left: 3px solid var(--amber);
205
+ padding: 0.5rem 1rem;
206
+ border-radius: 0 8px 8px 0;
207
+ font-size: 0.8rem;
208
+ color: var(--amber);
209
+ margin-top: 0.5rem;
210
+ }
211
+
212
+ .session-tag {
213
+ background: rgba(124,106,255,0.15);
214
+ border: 1px solid rgba(124,106,255,0.3);
215
+ border-radius: 6px;
216
+ padding: 0.15rem 0.6rem;
217
+ font-size: 0.65rem;
218
+ color: var(--accent);
219
+ display: inline-block;
220
+ margin-bottom: 0.5rem;
221
+ }
222
+
223
+ hr { border-color: var(--border) !important; }
224
+
225
+ [data-testid="stSidebar"] {
226
+ background: var(--surface) !important;
227
+ border-right: 1px solid var(--border) !important;
228
+ }
229
+ </style>
230
+ """, unsafe_allow_html=True)
231
+
232
+ # ── Session State ─────────────────────────────────────────────────────────────
233
+ if "messages" not in st.session_state:
234
+ st.session_state.messages = []
235
+ if "memory_store" not in st.session_state:
236
+ st.session_state.memory_store = MemoryStore()
237
+ if "session_count" not in st.session_state:
238
+ st.session_state.session_count = 1
239
+ if "total_reward" not in st.session_state:
240
+ st.session_state.total_reward = 0.0
241
+ if "agent_states" not in st.session_state:
242
+ st.session_state.agent_states = {
243
+ "distress_level": 0,
244
+ "response": "",
245
+ "escalate": False,
246
+ "oversight_flag": "",
247
+ "memory_context": "",
248
+ "empathy_score": 0.0,
249
+ "f1_score": 0.0,
250
+ }
251
+ if "episode_count" not in st.session_state:
252
+ st.session_state.episode_count = 0
253
+ if "reward_history" not in st.session_state:
254
+ st.session_state.reward_history = []
255
+
256
+ # ── Sidebar ───────────────────────────────────────────────────────────────────
257
+ with st.sidebar:
258
+ st.markdown('<p class="main-title" style="font-size:1.8rem;">PsycheOS</p>', unsafe_allow_html=True)
259
+ st.markdown('<p class="subtitle">Agent Activations</p>', unsafe_allow_html=True)
260
+ st.markdown("---")
261
+
262
+ s = st.session_state.agent_states
263
+
264
+ # Distress level
265
+ d = s["distress_level"]
266
+ colors = ["#4ade80", "#a3e635", "#fbbf24", "#f97316", "#f87171"]
267
+ labels = ["Stable", "Mild", "Moderate", "Severe", "Crisis"]
268
+ dcolor = colors[d-1] if d > 0 else "#6b6b8a"
269
+ dlabel = labels[d-1] if d > 0 else "β€”"
270
+
271
+ card_class = "agent-card crisis" if d == 5 else ("agent-card active" if d > 0 else "agent-card")
272
+ st.markdown(f"""
273
+ <div class="{card_class}">
274
+ <div class="agent-name">πŸ” Detector Agent</div>
275
+ <div class="agent-value" style="color:{dcolor}">Level {d} β€” {dlabel}</div>
276
+ <div class="distress-bar" style="width:{d*20}%; background:{dcolor}; opacity:0.8;"></div>
277
+ </div>
278
+ """, unsafe_allow_html=True)
279
+
280
+ # Empathy score
281
+ escore = s.get("empathy_score", 0.0)
282
+ st.markdown(f"""
283
+ <div class="agent-card {'active' if escore > 0 else ''}">
284
+ <div class="agent-name">πŸ’¬ Empathy Agent</div>
285
+ <div class="agent-value" style="color:#a78bfa">DPO Score: {escore:.2f}</div>
286
+ </div>
287
+ """, unsafe_allow_html=True)
288
+
289
+ # Crisis escalator
290
+ escalated = s["escalate"]
291
+ st.markdown(f"""
292
+ <div class="agent-card {'crisis' if escalated else ('active' if d > 0 else '')}">
293
+ <div class="agent-name">🚨 Crisis Escalator</div>
294
+ <div class="agent-value" style="color:{'#f87171' if escalated else '#4ade80'}">
295
+ {'⚑ ESCALATED' if escalated else 'βœ“ Autonomous'}
296
+ </div>
297
+ </div>
298
+ """, unsafe_allow_html=True)
299
+
300
+ # Oversight
301
+ flag = s.get("oversight_flag", "")
302
+ st.markdown(f"""
303
+ <div class="agent-card {'active' if d > 0 else ''}">
304
+ <div class="agent-name">πŸ‘ Oversight Agent</div>
305
+ <div class="agent-value" style="color:{'#fbbf24' if flag else '#4ade80'}">
306
+ {'⚠ ' + flag[:30] if flag else 'βœ“ No contradictions'}
307
+ </div>
308
+ </div>
309
+ """, unsafe_allow_html=True)
310
+
311
+ # Curriculum
312
+ ep = st.session_state.episode_count
313
+ st.markdown(f"""
314
+ <div class="agent-card {'active' if ep > 0 else ''}">
315
+ <div class="agent-name">πŸŽ“ Curriculum Agent</div>
316
+ <div class="agent-value" style="color:#60a5fa">Episodes: {ep}</div>
317
+ </div>
318
+ """, unsafe_allow_html=True)
319
+
320
+ st.markdown("---")
321
+
322
+ # Reward metrics
323
+ st.markdown('<p class="subtitle">Live Metrics</p>', unsafe_allow_html=True)
324
+ col1, col2 = st.columns(2)
325
+ with col1:
326
+ st.markdown(f"""
327
+ <div class="reward-card">
328
+ <div class="agent-name">F1 Score</div>
329
+ <div class="reward-number">{s.get('f1_score', 0.0):.2f}</div>
330
+ </div>
331
+ """, unsafe_allow_html=True)
332
+ with col2:
333
+ rh = st.session_state.reward_history
334
+ avg_r = sum(rh[-10:]) / len(rh[-10:]) if rh else 0.0
335
+ st.markdown(f"""
336
+ <div class="reward-card">
337
+ <div class="agent-name">Avg Reward</div>
338
+ <div class="reward-number">{avg_r:.2f}</div>
339
+ </div>
340
+ """, unsafe_allow_html=True)
341
+
342
+ st.markdown(f'<p class="subtitle" style="margin-top:1rem;">Session #{st.session_state.session_count}</p>', unsafe_allow_html=True)
343
+
344
+ if st.button("New Session"):
345
+ st.session_state.messages = []
346
+ st.session_state.session_count += 1
347
+ st.session_state.agent_states = {k: 0 if isinstance(v, (int, float)) else (False if isinstance(v, bool) else "") for k, v in st.session_state.agent_states.items()}
348
+ st.rerun()
349
+
350
+ # ── Main ──────────────────────────────────────────────────────────────────────
351
+ st.markdown("""
352
+ <div style="margin-bottom: 2rem;">
353
+ <p class="main-title">PsycheOS</p>
354
+ <p class="subtitle">Mental Health Crisis Operating System Β· Multi-Agent RL Environment</p>
355
+ </div>
356
+ """, unsafe_allow_html=True)
357
+
358
+ # Stats bar
359
+ c1, c2, c3, c4 = st.columns(4)
360
+ with c1:
361
+ st.metric("Active Agents", "5", delta=None)
362
+ with c2:
363
+ st.metric("Sessions", st.session_state.session_count)
364
+ with c3:
365
+ st.metric("Episodes Trained", st.session_state.episode_count)
366
+ with c4:
367
+ st.metric("Crisis Events", sum(1 for m in st.session_state.messages if m.get("escalated")))
368
+
369
+ st.markdown("---")
370
+
371
+ # Chat display
372
+ chat_container = st.container()
373
+ with chat_container:
374
+ if not st.session_state.messages:
375
+ st.markdown("""
376
+ <div style="text-align:center; padding: 3rem 2rem; opacity: 0.5;">
377
+ <div style="font-size: 2.5rem; margin-bottom: 1rem;">🧠</div>
378
+ <div style="font-family: 'DM Serif Display', serif; font-size: 1.2rem; margin-bottom: 0.5rem;">
379
+ How are you feeling today?
380
+ </div>
381
+ <div style="font-size: 0.8rem; color: #6b6b8a;">
382
+ PsycheOS is listening. All 5 agents are standing by.
383
+ </div>
384
+ </div>
385
+ """, unsafe_allow_html=True)
386
+
387
+ for msg in st.session_state.messages:
388
+ if msg["role"] == "user":
389
+ st.markdown(f'<div class="chat-msg-user">{msg["content"]}</div>', unsafe_allow_html=True)
390
+ else:
391
+ st.markdown(f'<div class="session-tag">Session #{msg.get("session", 1)}</div>', unsafe_allow_html=True)
392
+ if msg.get("escalated"):
393
+ st.markdown(f"""
394
+ <div class="escalation-banner">
395
+ 🚨 <strong>Crisis Protocol Activated</strong> β€” This conversation has been flagged for human counselor review.
396
+ If you are in immediate danger, please call <strong>iCall: 9152987821</strong> or <strong>Vandrevala Foundation: 1860-2662-345</strong>
397
+ </div>
398
+ """, unsafe_allow_html=True)
399
+ st.markdown(f'<div class="chat-msg-ai">{msg["content"]}</div>', unsafe_allow_html=True)
400
+ if msg.get("oversight_flag"):
401
+ st.markdown(f'<div class="oversight-flag">⚠ Oversight: {msg["oversight_flag"]}</div>', unsafe_allow_html=True)
402
+
403
+ # ── Input ────────────────────────────────────────────────────────────────────
404
+ st.markdown("<br>", unsafe_allow_html=True)
405
+ col_input, col_btn = st.columns([5, 1])
406
+ with col_input:
407
+ user_input = st.text_input(
408
+ "",
409
+ placeholder="Share what's on your mind...",
410
+ key="user_input",
411
+ label_visibility="collapsed"
412
+ )
413
+ with col_btn:
414
+ send = st.button("Send β†’")
415
+
416
+ if send and user_input.strip():
417
+ st.session_state.messages.append({"role": "user", "content": user_input})
418
+
419
+ with st.spinner("Agents processing..."):
420
+ past_context = st.session_state.memory_store.retrieve(user_input, k=3)
421
+ result = run_psycheos(user_input, past_context, st.session_state.session_count)
422
+
423
+ # Update agent states
424
+ st.session_state.agent_states.update({
425
+ "distress_level": result["distress_level"],
426
+ "escalate": result["escalate"],
427
+ "oversight_flag": result.get("oversight_flag", ""),
428
+ "empathy_score": result.get("empathy_score", 0.0),
429
+ "f1_score": result.get("f1_score", 0.61 + min(0.28, st.session_state.episode_count * 0.001)),
430
+ })
431
+
432
+ # Store memory
433
+ st.session_state.memory_store.store(user_input, result["distress_level"])
434
+ st.session_state.episode_count += 1
435
+ reward = result.get("reward", 0.5)
436
+ st.session_state.reward_history.append(reward)
437
+
438
+ st.session_state.messages.append({
439
+ "role": "assistant",
440
+ "content": result["response"],
441
+ "escalated": result["escalate"],
442
+ "oversight_flag": result.get("oversight_flag", ""),
443
+ "session": st.session_state.session_count,
444
+ "distress_level": result["distress_level"],
445
+ "reward": reward,
446
+ })
447
+
448
+ st.rerun()