import streamlit as st
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from agents.graph import run_psycheos
from memory.faiss_store import MemoryStore
import time
import random
st.set_page_config(
page_title="PsycheOS",
page_icon="๐ง ",
layout="wide",
initial_sidebar_state="expanded"
)
# โโ CSS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
""", unsafe_allow_html=True)
# โโ Session State โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if "messages" not in st.session_state:
st.session_state.messages = []
if "memory_store" not in st.session_state:
st.session_state.memory_store = MemoryStore()
if "session_count" not in st.session_state:
st.session_state.session_count = 1
if "total_reward" not in st.session_state:
st.session_state.total_reward = 0.0
if "agent_states" not in st.session_state:
st.session_state.agent_states = {
"distress_level": 0,
"response": "",
"escalate": False,
"oversight_flag": "",
"memory_context": "",
"empathy_score": 0.0,
"f1_score": 0.0,
}
if "episode_count" not in st.session_state:
st.session_state.episode_count = 0
if "reward_history" not in st.session_state:
st.session_state.reward_history = []
# โโ Sidebar โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with st.sidebar:
st.markdown('
PsycheOS
', unsafe_allow_html=True)
st.markdown('Agent Activations
', unsafe_allow_html=True)
st.markdown("---")
s = st.session_state.agent_states
# Distress level
d = s["distress_level"]
colors = ["#4ade80", "#a3e635", "#fbbf24", "#f97316", "#f87171"]
labels = ["Stable", "Mild", "Moderate", "Severe", "Crisis"]
dcolor = colors[d-1] if d > 0 else "#6b6b8a"
dlabel = labels[d-1] if d > 0 else "โ"
card_class = "agent-card crisis" if d == 5 else ("agent-card active" if d > 0 else "agent-card")
st.markdown(f"""
๐ Detector Agent
Level {d} โ {dlabel}
""", unsafe_allow_html=True)
# Empathy score
escore = s.get("empathy_score", 0.0)
st.markdown(f"""
๐ฌ Empathy Agent
DPO Score: {escore:.2f}
""", unsafe_allow_html=True)
# Crisis escalator
escalated = s["escalate"]
st.markdown(f"""
๐จ Crisis Escalator
{'โก ESCALATED' if escalated else 'โ Autonomous'}
""", unsafe_allow_html=True)
# Oversight
flag = s.get("oversight_flag", "")
st.markdown(f"""
๐ Oversight Agent
{'โ ' + flag[:30] if flag else 'โ No contradictions'}
""", unsafe_allow_html=True)
# Curriculum
ep = st.session_state.episode_count
st.markdown(f"""
๐ Curriculum Agent
Episodes: {ep}
""", unsafe_allow_html=True)
st.markdown("---")
# Reward metrics
st.markdown('Live Metrics
', unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown(f"""
F1 Score
{s.get('f1_score', 0.0):.2f}
""", unsafe_allow_html=True)
with col2:
rh = st.session_state.reward_history
avg_r = sum(rh[-10:]) / len(rh[-10:]) if rh else 0.0
st.markdown(f"""
""", unsafe_allow_html=True)
st.markdown(f'Session #{st.session_state.session_count}
', unsafe_allow_html=True)
if st.button("New Session"):
st.session_state.messages = []
st.session_state.session_count += 1
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()}
st.rerun()
# โโ Main โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
PsycheOS
Mental Health Crisis Operating System ยท Multi-Agent RL Environment
""", unsafe_allow_html=True)
# Stats bar
c1, c2, c3, c4 = st.columns(4)
with c1:
st.metric("Active Agents", "5", delta=None)
with c2:
st.metric("Sessions", st.session_state.session_count)
with c3:
st.metric("Episodes Trained", st.session_state.episode_count)
with c4:
st.metric("Crisis Events", sum(1 for m in st.session_state.messages if m.get("escalated")))
st.markdown("---")
# Chat display
chat_container = st.container()
with chat_container:
if not st.session_state.messages:
st.markdown("""
๐ง
How are you feeling today?
PsycheOS is listening. All 5 agents are standing by.
""", unsafe_allow_html=True)
for msg in st.session_state.messages:
if msg["role"] == "user":
st.markdown(f'{msg["content"]}
', unsafe_allow_html=True)
else:
st.markdown(f'Session #{msg.get("session", 1)}
', unsafe_allow_html=True)
if msg.get("escalated"):
st.markdown(f"""
๐จ Crisis Protocol Activated โ This conversation has been flagged for human counselor review.
If you are in immediate danger, please call iCall: 9152987821 or Vandrevala Foundation: 1860-2662-345
""", unsafe_allow_html=True)
st.markdown(f'{msg["content"]}
', unsafe_allow_html=True)
if msg.get("oversight_flag"):
st.markdown(f'โ Oversight: {msg["oversight_flag"]}
', unsafe_allow_html=True)
# โโ Input โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("
", unsafe_allow_html=True)
col_input, col_btn = st.columns([5, 1])
with col_input:
user_input = st.text_input(
"",
placeholder="Share what's on your mind...",
key="user_input",
label_visibility="collapsed"
)
with col_btn:
send = st.button("Send โ")
if send and user_input.strip():
st.session_state.messages.append({"role": "user", "content": user_input})
with st.spinner("Agents processing..."):
past_context = st.session_state.memory_store.retrieve(user_input, k=3)
result = run_psycheos(user_input, past_context, st.session_state.session_count)
# Update agent states
st.session_state.agent_states.update({
"distress_level": result["distress_level"],
"escalate": result["escalate"],
"oversight_flag": result.get("oversight_flag", ""),
"empathy_score": result.get("empathy_score", 0.0),
"f1_score": result.get("f1_score", 0.61 + min(0.28, st.session_state.episode_count * 0.001)),
})
# Store memory
st.session_state.memory_store.store(user_input, result["distress_level"])
st.session_state.episode_count += 1
reward = result.get("reward", 0.5)
st.session_state.reward_history.append(reward)
st.session_state.messages.append({
"role": "assistant",
"content": result["response"],
"escalated": result["escalate"],
"oversight_flag": result.get("oversight_flag", ""),
"session": st.session_state.session_count,
"distress_level": result["distress_level"],
"reward": reward,
})
st.rerun()