Spaces:
Sleeping
Sleeping
File size: 9,804 Bytes
a7ea8bc a6406c6 a7ea8bc 2bbcf98 a7ea8bc 2bbcf98 a7ea8bc a6406c6 a7ea8bc 2bbcf98 a6406c6 a7ea8bc a6406c6 a7ea8bc 2bbcf98 a6406c6 2bbcf98 a7ea8bc a6406c6 2bbcf98 a6406c6 2bbcf98 a6406c6 2bbcf98 a7ea8bc 2bbcf98 a7ea8bc a6406c6 a7ea8bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | """Module 1 — Saathi Chat: general mental-health Q&A + city-based doctor finder.
Flow:
1. User types a mental-health question (in any supported language).
2. Crisis regex runs first. If positive, Claude is bypassed and the helpline banner fires.
3. Otherwise Claude answers as Saathi, in the user's language.
4. User can optionally type a city to get 3-5 pre-curated resources from data/mental_health_resources.json.
5. Student Mode toggle: when ON, shows event-type selector and routes through student coaching prompt.
"""
from __future__ import annotations
from typing import Dict, List
import streamlit as st
from backend.voice_widget import render_mic_button
from backend.claude_client import chat
from backend.i18n import claude_language_name, t
from backend.resources import get_mental_health_resources, list_known_cities
from backend.safeguards import check_crisis, render_crisis_banner
from modules.cognitive_journal import get_cognitive_journal_context
MODULE_NAME = "saathi_chat"
STUDENT_MODULE_NAME = "student_corner"
HISTORY_KEY = "saathi_chat_history"
CITY_KEY = "saathi_chat_city"
MEMORY_TOGGLE_KEY = "saathi_chat_cross_module_memory_enabled"
STUDENT_MODE_KEY = "saathi_chat_student_mode"
STUDENT_EVENT_KEY = "saathi_chat_student_event"
# How many prior messages (not counting the current turn) to resend to Claude.
# 20 = 10 full user↔assistant exchanges. Caps token cost + latency on long demos.
HISTORY_WINDOW = 20
# Event choices for Student Mode (value, i18n key)
STUDENT_EVENT_CHOICES = [
("exam", "event_exam"),
("placement_interview", "event_placement"),
("viva", "event_viva"),
("presentation", "event_presentation"),
("result_day", "event_result"),
("general_burnout", "event_burnout"),
]
def _init_state() -> None:
if HISTORY_KEY not in st.session_state:
st.session_state[HISTORY_KEY] = [] # list of {role, content}
if CITY_KEY not in st.session_state:
st.session_state[CITY_KEY] = ""
if MEMORY_TOGGLE_KEY not in st.session_state:
# Default ON — cross-module awareness is Saathi's differentiator.
st.session_state[MEMORY_TOGGLE_KEY] = True
if STUDENT_MODE_KEY not in st.session_state:
st.session_state[STUDENT_MODE_KEY] = False
if STUDENT_EVENT_KEY not in st.session_state:
st.session_state[STUDENT_EVENT_KEY] = "exam"
def _build_cross_module_memory_block() -> str:
"""Return the memory digest iff the user has opted in (default on)."""
if not st.session_state.get(MEMORY_TOGGLE_KEY, True):
return ""
try:
return get_cognitive_journal_context() or ""
except Exception:
# Never let a memory-digest bug break the chat turn.
return ""
def _render_resources(resources: List[Dict], lang: str) -> None:
st.markdown(f"### {t('chat_resources_heading', lang)}")
for r in resources:
with st.container(border=True):
st.markdown(f"**{r.get('name', '')}** — *{r.get('type', '')}*")
if r.get("address"):
st.markdown(f"📍 {r['address']}")
if r.get("phone"):
st.markdown(f"📞 `{r['phone']}`")
website = r.get("website")
if website:
st.markdown(f"🌐 [{website}]({website})")
specialties = r.get("specialties") or []
if specialties:
st.caption("• " + " · ".join(specialties))
if r.get("cost"):
st.caption(f"💰 {r['cost']}")
st.caption(
"_Resources are curated from Government of India, AIIMS, NIMHANS, and "
"state mental-health websites. Saathi is not affiliated with any of "
"these institutions and does not make referrals on their behalf._"
)
def render(lang: str) -> None:
"""Top-level render function. `lang` is the language code (e.g. 'en', 'hi')."""
_init_state()
st.header(t("chat_header", lang))
st.caption(t("chat_sub", lang))
# --- Student Mode toggle + Cross-module memory (side by side) ---
col_student, col_memory = st.columns(2)
with col_student:
st.toggle(
t("chat_student_mode_label", lang),
key=STUDENT_MODE_KEY,
help=t("chat_student_mode_help", lang),
)
with col_memory:
with st.expander(t("chat_memory_heading", lang), expanded=False):
st.toggle(
t("chat_memory_toggle_label", lang),
key=MEMORY_TOGGLE_KEY,
help=t("chat_memory_toggle_help", lang),
)
digest_preview = _build_cross_module_memory_block()
if digest_preview:
st.caption(t("chat_memory_active_caption", lang))
st.code(digest_preview, language="markdown")
else:
st.caption(t("chat_memory_empty_caption", lang))
# --- Student Mode: event picker ---
student_mode_on = st.session_state.get(STUDENT_MODE_KEY, False)
if student_mode_on:
st.markdown(f"**{t('student_event_label', lang)}**")
labels = [t(label_key, lang) for _, label_key in STUDENT_EVENT_CHOICES]
event_values = [value for value, _ in STUDENT_EVENT_CHOICES]
current_index = (
event_values.index(st.session_state[STUDENT_EVENT_KEY])
if st.session_state[STUDENT_EVENT_KEY] in event_values
else 0
)
chosen_label = st.radio(
"event",
options=labels,
index=current_index,
horizontal=True,
label_visibility="collapsed",
key="saathi_chat_student_event_radio",
)
st.session_state[STUDENT_EVENT_KEY] = event_values[labels.index(chosen_label)]
# --- Conversation history ---
for msg in st.session_state[HISTORY_KEY]:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# --- Voice mic (auto-transcribes into chat input) ---
render_mic_button(lang, target_aria_label=t("chat_input_placeholder", lang))
# --- Chat input ---
user_text = st.chat_input(t("chat_input_placeholder", lang), key="saathi_chat_input")
if user_text:
# Crisis check BEFORE Claude is invoked
if check_crisis(user_text):
st.session_state[HISTORY_KEY].append({"role": "user", "content": user_text})
with st.chat_message("user"):
st.markdown(user_text)
with st.chat_message("assistant"):
render_crisis_banner(lang)
return
# Normal flow → Claude
st.session_state[HISTORY_KEY].append({"role": "user", "content": user_text})
with st.chat_message("user"):
st.markdown(user_text)
with st.chat_message("assistant"):
with st.spinner("…"):
try:
# Route through student prompt when Student Mode is ON
if student_mode_on:
augmented_text = (
f"event_type: {st.session_state[STUDENT_EVENT_KEY]}\n"
f"situation: {user_text}"
)
reply = chat(
module=STUDENT_MODULE_NAME,
user_text=augmented_text,
language_name=claude_language_name(lang),
max_tokens=2400,
extra_context={
"cross_module_memory": _build_cross_module_memory_block()
},
)
else:
prior_history = st.session_state[HISTORY_KEY][:-1][-HISTORY_WINDOW:]
reply = chat(
module=MODULE_NAME,
user_text=user_text,
language_name=claude_language_name(lang),
history=prior_history,
extra_context={
"cross_module_memory": _build_cross_module_memory_block()
},
)
except Exception as e:
reply = (
"I'm having trouble reaching my language model right now. "
"If this is urgent, please call Tele-MANAS on **14416** "
"(or 1800-89-14416) — Government of India, 24×7, free, 20+ Indian languages. "
"For an emergency, call **112**.\n\n"
f"_(Technical detail: {e})_"
)
st.markdown(reply)
st.session_state[HISTORY_KEY].append({"role": "assistant", "content": reply})
# --- City-based resource finder (always visible under the chat) ---
st.divider()
st.markdown(f"**{t('chat_city_prompt', lang)}**")
col_input, col_button = st.columns([3, 1])
with col_input:
city = st.text_input(
"city",
value=st.session_state[CITY_KEY],
placeholder=t("chat_city_placeholder", lang),
label_visibility="collapsed",
key="saathi_chat_city_input",
)
with col_button:
find_clicked = st.button(
t("chat_city_button", lang),
key="saathi_chat_city_button",
use_container_width=True,
)
if find_clicked and city:
st.session_state[CITY_KEY] = city
resources = get_mental_health_resources(city)
if resources:
_render_resources(resources, lang)
else:
st.warning(t("chat_no_resources", lang))
known = list_known_cities()
if known:
st.caption("Cities I currently know: " + ", ".join(known))
|