SafeSpace / app.py
AliSakr9997's picture
Upload folder using huggingface_hub
d47c7f6 verified
"""
app.py
======
Mental Health AI โ€” Full Pipeline
Files needed:
mental_xlmr_final/ โ† XLM-R model folder
mental_model.h5 โ† Survey Keras model
scaler.pkl โ† Survey scaler
recommendations.py โ† same directory
Install:
pip install streamlit transformers torch tensorflow scikit-learn deep-translator
"""
import sys, os
sys.path.append(os.path.dirname(__file__))
import re, pickle, warnings
import numpy as np
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from deep_translator import GoogleTranslator
sys.path.insert(0, os.path.dirname(__file__))
from recommendations import get_recommendations
warnings.filterwarnings("ignore")
# โ”€โ”€ PAGE CONFIG โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
st.set_page_config(page_title="Mental Health AI", page_icon="๐Ÿง ", layout="wide")
st.markdown("""
<style>
.stApp { background: linear-gradient(135deg, #0d1117, #161b22, #0d1117); color: #e6edf3; }
h1 { text-align: center; color: #58a6ff; font-size: 36px; margin-bottom: 4px; }
h2, h3 { color: #c9d1d9; }
.section-card {
background: rgba(22,27,34,0.9);
border: 1px solid #30363d;
border-radius: 14px;
padding: 22px 26px;
margin-bottom: 18px;
}
.result-card {
background: #161b22;
border: 1px solid #30363d;
border-radius: 12px;
padding: 20px;
text-align: center;
margin-bottom: 8px;
}
.result-card.primary { border: 2px solid #58a6ff; }
.result-label { font-size: 15px; color: #8b949e; margin-bottom: 6px; }
.result-value { font-size: 44px; font-weight: 700; }
.severity-badge {
display: inline-block;
padding: 3px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-top: 6px;
}
.rec-block {
background: #161b22;
border: 1px solid #30363d;
border-radius: 12px;
padding: 18px 22px;
margin-bottom: 14px;
}
.rec-title { font-size: 15px; font-weight: 700; margin-bottom: 10px; }
.rec-item { font-size: 14px; color: #c9d1d9; padding: 4px 0; border-bottom: 1px solid #21262d; }
.rec-item:last-child { border-bottom: none; }
.ar-text { font-size: 13px; color: #8b949e; margin-top: 3px; direction: rtl; }
.referral-box {
background: rgba(248,81,73,0.1);
border: 1px solid rgba(248,81,73,0.4);
border-radius: 10px;
padding: 14px 18px;
margin-top: 12px;
}
.crisis-box {
background: rgba(248,81,73,0.2);
border: 2px solid #f85149;
border-radius: 12px;
padding: 20px 24px;
margin: 16px 0;
}
div.stButton > button {
background: linear-gradient(90deg, #1f6feb, #58a6ff);
color: white; font-size: 17px; font-weight: 700;
border-radius: 10px; height: 52px; width: 100%; border: none;
}
div.stSlider > label { color: #c9d1d9 !important; font-size: 13px; }
.stTextArea textarea {
background: #0d1117 !important;
color: #e6edf3 !important;
border: 1px solid #30363d !important;
border-radius: 8px !important;
}
</style>
""", unsafe_allow_html=True)
# โ”€โ”€ CONSTANTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
CLASSES = ["anxiety", "depression", "stress"]
ARABIC_LABELS = {"anxiety": "ุงู„ู‚ู„ู‚", "depression": "ุงู„ุงูƒุชุฆุงุจ", "stress": "ุงู„ุถุบุท ุงู„ู†ูุณูŠ"}
COLORS = {"anxiety": "#ffa657", "depression": "#79c0ff", "stress": "#56d364"}
SEVERITY_AR = {
"normal": "ุทุจูŠุนูŠ", "mild": "ุฎููŠู", "moderate": "ู…ุชูˆุณุท",
"severe": "ุดุฏูŠุฏ", "extremely_severe": "ุดุฏูŠุฏ ุฌุฏุงู‹", "crisis": "ุฃุฒู…ุฉ",
}
SEVERITY_COLORS = {
"normal": "#56d364", "mild": "#e3b341", "moderate": "#ffa657",
"severe": "#f85149", "extremely_severe": "#ff0000", "crisis": "#ff0000",
}
CAUSE_AR = {
"work": "ุถุบุท ุงู„ุนู…ู„", "relationships": "ุงู„ุนู„ุงู‚ุงุช", "financial": "ุงู„ุถุบุท ุงู„ู…ุงู„ูŠ",
"academic": "ุงู„ุถุบุท ุงู„ุฃูƒุงุฏูŠู…ูŠ", "health": "ุงู„ู…ุฎุงูˆู ุงู„ุตุญูŠุฉ", "social": "ุงู„ู‚ู„ู‚ ุงู„ุงุฌุชู…ุงุนูŠ",
"self_worth": "ุงู„ุซู‚ุฉ ุจุงู„ู†ูุณ", "trauma": "ุงู„ุตุฏู…ุฉ ุงู„ู†ูุณูŠุฉ", "general": "ุนุงู…",
}
# โ”€โ”€ LOAD MODELS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@st.cache_resource
def load_xlmr():
token = st.secrets["HF_TOKEN"]
xlmr_tokenizer = AutoTokenizer.from_pretrained(
"tasneem33355/mental-xlmr", token=token
)
model = AutoModelForSequenceClassification.from_pretrained(
"tasneem33355/mental-xlmr", token=token
)
model.eval()
# Hardcoded โ€” LabelEncoder ุนู„ู‰ ['anxiety','depression','stress'] ุฏุงูŠู…ุงู‹ ุจุชุฑุชู‘ุจ alphabetically
# 0=anxiety, 1=depression, 2=stress
classes = ["anxiety", "depression", "stress"]
return xlmr_tokenizer, model, classes
@st.cache_resource
def load_survey():
scaler = pickle.load(open(os.path.join(os.path.dirname(__file__), "scaler.pkl"), "rb"))
weights = pickle.load(open(os.path.join(os.path.dirname(__file__), "model_weights.pkl"), "rb"))
def predict(x):
for w in weights:
if len(w) == 2:
x = np.dot(x, w[0]) + w[1]
x = np.maximum(0, x) # ReLU
x = np.exp(x) / np.sum(np.exp(x)) # Softmax
return x
return scaler, predict
xlmr_tokenizer, xlmr_model, le = load_xlmr()
scaler, survey_predict = load_survey()
# โ”€โ”€ HELPERS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def clean_text(text):
text = re.sub(r'(.)\1{2,}', r'\1\1', text)
text = re.sub(r'[^\w\s\u0600-\u06FF\[\]]', ' ', text)
return re.sub(r'\s+', ' ', text).strip()
def translate_to_en(text):
try:
return GoogleTranslator(source="auto", target="en").translate(text)
except Exception:
return ""
# โ”€โ”€ KEYWORD OVERRIDE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
DEPRESSION_KEYWORDS = [
# ุนุฑุจูŠ ูุตูŠุญ
"ุงูƒุชุฆุงุจ", "ู…ูƒุชุฆุจ", "ู…ูƒุชุฆุจุฉ", "ุญุฒู†", "ุญุฒูŠู†", "ุญุฒูŠู†ุฉ", "ูŠุฃุณ", "ูŠุงุฆุณ", "ูŠุงุฆุณุฉ",
"ูุฑุงุบ", "ุฅุญุณุงุณ ุจุงู„ูุฑุงุบ", "ุจู„ุง ู…ุนู†ู‰", "ู„ุง ู…ุนู†ู‰", "ู…ุงู„ู‡ุงุด ู…ุนู†ู‰", "ุจู„ุง ู‡ุฏู",
"ู„ุง ุฃู…ู„", "ู…ููŠุด ุฃู…ู„", "ุชุนุจุช ู…ู† ุงู„ุญูŠุงุฉ", "ุฒู‡ู‚ุช ู…ู† ุงู„ุญูŠุงุฉ",
"ู…ุด ู„ุงู‚ูŠ ู…ุนู†ู‰", "ู…ุด ู„ุงู‚ูŠุฉ ู…ุนู†ู‰", "ุญุงุณุณ ุจุงู„ูุฑุงุบ", "ุญุงุณุฉ ุจุงู„ูุฑุงุบ",
"ู…ููŠุด ุทุงู‚ุฉ", "ู…ููŠุด ุฑุบุจุฉ", "ุจูƒุงุก", "ุนุงูŠุฒ ุฃุจูƒูŠ", "ุนุงูŠุฒุฉ ุฃุจูƒูŠ",
"ูˆุญูŠุฏ", "ูˆุญูŠุฏุฉ", "ุนุฒู„ุฉ", "ู…ู†ุนุฒู„", "ู…ู†ุนุฒู„ุฉ",
"ุฅุฑู‡ุงู‚ ู†ูุณูŠ", "ุฅุฑู‡ุงู‚ ุนุงุทููŠ", "ู…ุด ุญุงุณุณ ุจุญุงุฌุฉ", "ู…ุด ุญุงุณุฉ ุจุญุงุฌุฉ",
# ุนุงู…ูŠุฉ ู…ุตุฑูŠุฉ ูˆุดุงู…ูŠุฉ
"ุฒู‡ู‚ุช", "ุชุนุจุช", "ู…ุด ุทุงูŠู‚", "ู…ุด ุทุงูŠู‚ุฉ", "ู†ูุณูŠุชูŠ ูˆุญุดุฉ", "ู†ูุณูŠุชูŠ ููŠ ุงู„ุฃุฑุถ",
"ู…ุด ู‚ุงุฏุฑ ุฃูƒู…ู„", "ู…ุด ู‚ุงุฏุฑุฉ ุฃูƒู…ู„", "ู…ุด ุนุงูŠุด", "ู…ุด ู‚ุงุฏุฑ ุฃุนูŠุด",
"ู…ุด ุนุงูŠุฒ ุฃุตุญู‰", "ู…ุด ุนุงูŠุฒุฉ ุฃุตุญู‰", "ุฏู…ูˆุน", "ุจุฏู…ุน", "ู‚ู„ุจูŠ ุชู‚ูŠู„",
"ู…ุด ุญุงุณุณ ุจู†ูุณูŠ", "ู…ุด ุญุงุณุฉ ุจู†ูุณูŠ", "ู…ุง ุจุญุณ ุจุดูŠ", "ู…ุง ููŠ ูุงูŠุฏุฉ",
"ู…ุงููŠ ุงู…ู„", "ู…ุง ููŠ ุงู…ู„", "ุญูŠุงุชูŠ ุฎุฑุจุช", "ุฎุณุฑุช ูƒู„ ุญุงุฌุฉ",
# ุฅู†ุฌู„ูŠุฒูŠ
"depressed", "depression", "hopeless", "hopelessness", "empty", "emptiness",
"worthless", "meaningless", "no meaning", "no purpose", "cannot go on",
"cant go on", "no energy", "no motivation", "crying", "feel nothing",
"numb", "isolated", "lonely", "loneliness", "sad", "sadness",
"despair", "grief", "miserable", "broken", "lost all hope",
]
ANXIETY_KEYWORDS = [
# ุนุฑุจูŠ
"ู‚ู„ู‚", "ู‚ู„ู‚ุงู†", "ู‚ู„ู‚ุงู†ุฉ", "ุฎูˆู", "ุฎุงูŠู", "ุฎุงูŠูุฉ", "ุชูˆุชุฑ", "ู…ุชูˆุชุฑ", "ู…ุชูˆุชุฑุฉ",
"ู‡ู„ุน", "ู…ุด ู…ุฑุชุงุญ", "ู…ุด ู…ุฑุชุงุญุฉ", "ุฐุนุฑ", "ุฑู‡ุงุจ", "ูˆุณูˆุงุณ",
# ุฅู†ุฌู„ูŠุฒูŠ
"panic", "anxious", "anxiety", "worried", "worry", "fear",
"scared", "nervous", "restless", "tense", "phobia", "ocd",
]
STRESS_KEYWORDS = [
# ุนุฑุจูŠ
"ุถุบุท", "ุถุบูˆุท", "ู…ุถุบูˆุท", "ู…ุถุบูˆุทุฉ", "ุฅุฌู‡ุงุฏ", "ู…ุฌู‡ุฏ", "ู…ุฌู‡ุฏุฉ",
# ุฅู†ุฌู„ูŠุฒูŠ
"overwhelmed", "stressed", "stress", "burnout", "exhausted", "overloaded",
]
def keyword_boost(text: str, scores: dict) -> dict:
"""
ูŠุนูˆู‘ุถ ุงู„ู€ stress bias ููŠ ุงู„ู…ูˆุฏูŠู„ ุนู† ุทุฑูŠู‚ override ู‚ูˆูŠ
ู„ู…ุง ุชูƒูˆู† ูƒู„ู…ุงุช depression ุฃูˆ anxiety ุฃูˆ stress ูˆุงุถุญุฉ ููŠ ุงู„ู†ุต.
"""
text_lower = text.lower()
dep_hits = sum(1 for kw in DEPRESSION_KEYWORDS if kw.lower() in text_lower)
anx_hits = sum(1 for kw in ANXIETY_KEYWORDS if kw.lower() in text_lower)
str_hits = sum(1 for kw in STRESS_KEYWORDS if kw.lower() in text_lower)
if dep_hits == 0 and anx_hits == 0 and str_hits == 0:
return scores
s = dict(scores)
if dep_hits > 0 and dep_hits >= anx_hits and dep_hits >= str_hits:
# depression ูƒู„ู…ุงุช ูˆุงุถุญุฉ โ€” override ู‚ูˆูŠ
boost = min(0.55 + dep_hits * 0.10, 0.85)
s["depression"] = boost
remaining = 1.0 - boost
total_rest = s["anxiety"] + s["stress"]
if total_rest > 0:
s["anxiety"] = round(remaining * s["anxiety"] / total_rest, 4)
s["stress"] = round(remaining * s["stress"] / total_rest, 4)
s["depression"] = round(boost, 4)
elif anx_hits > 0 and anx_hits >= dep_hits and anx_hits >= str_hits:
# anxiety ูƒู„ู…ุงุช ูˆุงุถุญุฉ โ€” override ู‚ูˆูŠ
boost = min(0.55 + anx_hits * 0.10, 0.85)
s["anxiety"] = boost
remaining = 1.0 - boost
total_rest = s["depression"] + s["stress"]
if total_rest > 0:
s["depression"] = round(remaining * s["depression"] / total_rest, 4)
s["stress"] = round(remaining * s["stress"] / total_rest, 4)
s["anxiety"] = round(boost, 4)
elif str_hits > 0 and str_hits >= dep_hits and str_hits >= anx_hits:
# stress ูƒู„ู…ุงุช ูˆุงุถุญุฉ โ€” override ู‚ูˆูŠ
boost = min(0.55 + str_hits * 0.10, 0.85)
s["stress"] = boost
remaining = 1.0 - boost
total_rest = s["depression"] + s["anxiety"]
if total_rest > 0:
s["depression"] = round(remaining * s["depression"] / total_rest, 4)
s["anxiety"] = round(remaining * s["anxiety"] / total_rest, 4)
s["stress"] = round(boost, 4)
# normalize
total = sum(s.values())
if total > 0:
s = {k: round(v / total, 4) for k, v in s.items()}
return s
def predict_text(text: str) -> dict:
cleaned = clean_text(text)
text_en = translate_to_en(cleaned)
combined = (text_en + " [SEP] " + cleaned) if text_en else cleaned
inputs = xlmr_tokenizer(combined, return_tensors="pt",
truncation=True, max_length=192, padding=True)
with torch.no_grad():
probs = torch.softmax(xlmr_model(**inputs).logits, dim=-1).squeeze().numpy()
raw_scores = {c: round(float(p), 4) for c, p in zip(le, probs)}
# ุทุจู‘ู‚ ุงู„ู€ keyword boost ุนู„ู‰ ุงู„ู†ุต ุงู„ุฃุตู„ูŠ + ุงู„ุชุฑุฌู…ุฉ
boosted = keyword_boost(text + " " + text_en, raw_scores)
return boosted
def predict_survey(answers: list) -> dict:
data = scaler.transform(np.array(answers).reshape(1, -1))
pred = survey_predict(data)[0]
return {
"depression": round(float(pred[0]), 4),
"anxiety": round(float(pred[1]), 4),
"stress": round(float(pred[2]), 4),
}
def fuse_scores(text_s, survey_s, w_text=0.4, w_survey=0.6):
return {c: round(w_text * text_s[c] + w_survey * survey_s[c], 4) for c in CLASSES}
# โ”€โ”€ SURVEY QUESTIONS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
SURVEY_Q = [
("I found it hard to wind down", "ูˆุฌุฏุช ุตุนูˆุจุฉ ููŠ ุงู„ุงุณุชุฑุฎุงุก"),
("I was aware of dryness of my mouth", "ู„ุงุญุธุช ุฌูุงูุงู‹ ููŠ ูู…ูŠ"),
("I couldn't seem to experience any positive feeling at all", "ู„ู… ุฃุณุชุทุน ุงู„ุดุนูˆุฑ ุจุฃูŠ ู…ุดุงุนุฑ ุฅูŠุฌุงุจูŠุฉ"),
("I experienced breathing difficulty", "ุฃุญุณุณุช ุจุตุนูˆุจุฉ ููŠ ุงู„ุชู†ูุณ"),
("I found it difficult to work up the initiative to do things", "ูˆุฌุฏุช ุตุนูˆุจุฉ ููŠ ุงุชุฎุงุฐ ุงู„ู…ุจุงุฏุฑุฉ ู„ู„ู‚ูŠุงู… ุจุงู„ุฃุดูŠุงุก"),
("I tended to over-react to situations", "ูƒู†ุช ุฃุจุงู„ุบ ููŠ ุฑุฏูˆุฏ ุฃูุนุงู„ูŠ ุชุฌุงู‡ ุงู„ู…ูˆุงู‚ู"),
("I experienced trembling", "ุดุนุฑุช ุจุงู„ุฑุนุดุฉ"),
("I felt that I was using a lot of nervous energy", "ุดุนุฑุช ุฃู†ู†ูŠ ุฃุณุชู‡ู„ูƒ ุงู„ูƒุซูŠุฑ ู…ู† ุงู„ุทุงู‚ุฉ ุงู„ุนุตุจูŠุฉ"),
("I was worried about situations in which I might panic", "ูƒู†ุช ู‚ู„ู‚ุงู‹ ู…ู† ู…ูˆุงู‚ู ู‚ุฏ ุฃุตุงุจ ููŠู‡ุง ุจุงู„ุฐุนุฑ"),
("I felt that I had nothing to look forward to", "ุดุนุฑุช ุฃู†ู‡ ู„ุง ูŠูˆุฌุฏ ุดูŠุก ุฃุชุทู„ุน ุฅู„ูŠู‡"),
("I found myself getting agitated", "ูˆุฌุฏุช ู†ูุณูŠ ุฃุดุนุฑ ุจุงู„ุงู†ูุนุงู„"),
("I found it difficult to relax", "ูˆุฌุฏุช ุตุนูˆุจุฉ ููŠ ุงู„ุงุณุชุฑุฎุงุก"),
("I felt down-hearted and blue", "ุดุนุฑุช ุจุงู„ุฅุญุจุงุท ูˆุงู„ูƒุขุจุฉ"),
("I was intolerant of anything that kept me from getting on", "ูƒู†ุช ุบูŠุฑ ู…ุชุณุงู…ุญ ู…ุน ุฃูŠ ุดูŠุก ูŠุนูŠู‚ู†ูŠ"),
("I felt I was close to panic", "ุดุนุฑุช ุฃู†ู†ูŠ ุนู„ู‰ ูˆุดูƒ ุงู„ุฐุนุฑ"),
("I was unable to become enthusiastic", "ู„ู… ุฃุณุชุทุน ุฃู† ุฃุชุญู…ุณ ู„ุฃูŠ ุดูŠุก"),
("I felt I wasn't worth much as a person", "ุดุนุฑุช ุฃู†ู†ูŠ ู„ุณุช ุดุฎุตุงู‹ ุฐุง ู‚ูŠู…ุฉ"),
("I felt that I was rather touchy", "ุดุนุฑุช ุฃู†ู†ูŠ ู…ุชู‚ู„ุจ ุงู„ู…ุฒุงุฌ"),
("I was aware of the action of my heart", "ูƒู†ุช ูˆุงุนูŠุงู‹ ู„ู†ุจุถุงุช ู‚ู„ุจูŠ"),
("I felt scared without any good reason", "ุดุนุฑุช ุจุงู„ุฎูˆู ุฏูˆู† ุณุจุจ ูˆุงุถุญ"),
("I felt that life was meaningless", "ุดุนุฑุช ุฃู† ุงู„ุญูŠุงุฉ ุจู„ุง ู…ุนู†ู‰"),
("I found it hard to calm down", "ูˆุฌุฏุช ุตุนูˆุจุฉ ููŠ ุงู„ุชู‡ุฏุฆุฉ"),
("I felt nervous", "ุดุนุฑุช ุจุงู„ุชูˆุชุฑ"),
("I felt sad and depressed", "ุดุนุฑุช ุจุงู„ุญุฒู† ูˆุงู„ุงูƒุชุฆุงุจ"),
("I found myself getting impatient", "ูˆุฌุฏุช ู†ูุณูŠ ุฃุดุนุฑ ุจู†ูุงุฏ ุงู„ุตุจุฑ"),
("I felt that I was rather emotional", "ุดุนุฑุช ุฃู†ู†ูŠ ุนุงุทููŠ ุจุดูƒู„ ู…ูุฑุท"),
("I felt restless", "ุดุนุฑุช ุจุนุฏู… ุงู„ู‡ุฏูˆุก"),
("I had difficulty concentrating", "ูˆุฌุฏุช ุตุนูˆุจุฉ ููŠ ุงู„ุชุฑูƒูŠุฒ"),
("I felt lonely", "ุดุนุฑุช ุจุงู„ูˆุญุฏุฉ"),
("I found it difficult to relax", "ูˆุฌุฏุช ุตุนูˆุจุฉ ููŠ ุงู„ุงุณุชุฑุฎุงุก"),
("I felt hopeless", "ุดุนุฑุช ุจุงู„ูŠุฃุณ"),
("I felt worried about many things", "ูƒู†ุช ู‚ู„ู‚ุงู‹ ุจุดุฃู† ุฃุดูŠุงุก ูƒุซูŠุฑุฉ"),
("I felt that I had no energy", "ุดุนุฑุช ุจุนุฏู… ูˆุฌูˆุฏ ุทุงู‚ุฉ"),
("I felt tense", "ุดุนุฑุช ุจุงู„ุชูˆุชุฑ ูˆุงู„ุถูŠู‚"),
("I felt tired for no reason", "ุดุนุฑุช ุจุงู„ุชุนุจ ุฏูˆู† ุณุจุจ"),
("I felt uneasy", "ุดุนุฑุช ุจุนุฏู… ุงู„ุงุฑุชูŠุงุญ"),
("I felt worthless", "ุดุนุฑุช ุจุฃู†ู†ูŠ ู„ุง ู‚ูŠู…ุฉ ู„ูŠ"),
("I felt anxious", "ุดุนุฑุช ุจุงู„ู‚ู„ู‚"),
("I felt discouraged", "ุดุนุฑุช ุจุงู„ุฅุญุจุงุท"),
("I felt stressed", "ุดุนุฑุช ุจุงู„ุถุบุท"),
("I felt overwhelmed", "ุดุนุฑุช ุจุงู„ุฅุฑู‡ุงู‚"),
("I felt emotionally exhausted", "ุดุนุฑุช ุจุงู„ุฅู†ู‡ุงูƒ ุงู„ุนุงุทููŠ"),
]
# โ”€โ”€ UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
st.title("๐Ÿง  Mental Health AI")
st.markdown(
"<p style='text-align:center;color:#8b949e;font-size:15px;'>"
"Write how you feel and answer the survey for a complete assessment"
"<br><span dir='rtl'>ุงูƒุชุจ ู…ุง ุชุดุนุฑ ุจู‡ ูˆุฃุฌุจ ุนู„ู‰ ุงู„ุฃุณุฆู„ุฉ ู„ู„ุญุตูˆู„ ุนู„ู‰ ุชู‚ูŠูŠู… ุดุงู…ู„</span></p>",
unsafe_allow_html=True,
)
st.markdown("---")
# โ”€โ”€ PART 1: TEXT โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
st.markdown("<div class='section-card'>", unsafe_allow_html=True)
st.markdown("### ๐Ÿ’ฌ How are you feeling? / ูƒูŠู ุชุดุนุฑุŸ")
st.markdown(
"<p style='color:#8b949e;font-size:13px;'>"
"Write in any language โ€” Arabic (any dialect), English, or both<br>"
"<span dir='rtl'>ุงูƒุชุจ ุจุฃูŠ ู„ุบุฉ โ€” ุนุฑุจูŠ (ุฃูŠ ู„ู‡ุฌุฉ)ุŒ ุฅู†ุฌู„ูŠุฒูŠุŒ ุฃูˆ ุงู„ุงุชู†ูŠู†</span></p>",
unsafe_allow_html=True,
)
user_text = st.text_area(
label="",
placeholder="e.g. I've been feeling very overwhelmed at work and can't sleep...\nู…ุซุงู„: ุฃู†ุง ุชุนุจุงู† ุฌุฏุงู‹ ู…ู† ุงู„ุดุบู„ ูˆู…ุด ู‚ุงุฏุฑ ุฃู†ุงู…...",
height=120,
label_visibility="collapsed",
)
st.markdown("</div>", unsafe_allow_html=True)
# โ”€โ”€ PART 2: SURVEY โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
st.markdown("<div class='section-card'>", unsafe_allow_html=True)
st.markdown("### ๐Ÿ“‹ DASS-42 Survey / ุงุณุชุจูŠุงู† DASS-42")
st.markdown(
"<p style='color:#8b949e;font-size:13px;'>"
"0 = Never &nbsp;|&nbsp; 1 = Sometimes &nbsp;|&nbsp; 2 = Often &nbsp;|&nbsp; "
"3 = Most of the time &nbsp;|&nbsp; 4 = Always<br>"
"<span dir='rtl'>0 = ู„ู… ูŠุญุฏุซ ุฃุจุฏุงู‹ | 1 = ุฃุญูŠุงู†ุงู‹ | 2 = ูƒุซูŠุฑุงู‹ | 3 = ู…ุนุธู… ุงู„ูˆู‚ุช | 4 = ุฏุงุฆู…ุงู‹</span></p>",
unsafe_allow_html=True,
)
survey_answers = []
for i in range(0, len(SURVEY_Q), 2):
cols = st.columns(2)
for j, (en, ar) in enumerate(SURVEY_Q[i:i+2]):
with cols[j]:
val = st.slider(
f"{i+j+1}. {en}\n{ar}",
min_value=0, max_value=3, value=0,
key=f"q_{i+j}",
)
survey_answers.append(val)
st.markdown("</div>", unsafe_allow_html=True)
# โ”€โ”€ PREDICT BUTTON โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_, col_btn, _ = st.columns([1, 2, 1])
with col_btn:
predict_btn = st.button("๐Ÿ” Analyze / ุชุญู„ูŠู„")
# โ”€โ”€ RESULTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if predict_btn:
if not user_text.strip():
st.warning("Please write how you feel first. / ู…ู† ูุถู„ูƒ ุงูƒุชุจ ู…ุง ุชุดุนุฑ ุจู‡ ุฃูˆู„ุงู‹.")
st.stop()
with st.spinner("Analyzing... / ุฌุงุฑูŠ ุงู„ุชุญู„ูŠู„..."):
text_scores = predict_text(user_text)
survey_scores = predict_survey(survey_answers)
final_scores = fuse_scores(text_scores, survey_scores)
primary = max(final_scores, key=final_scores.get)
rec = get_recommendations(primary, final_scores[primary], user_text)
st.markdown("---")
st.markdown("## ๐Ÿ“Š Results / ุงู„ู†ุชุงุฆุฌ")
# โ”€โ”€ SCORE CARDS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
cols = st.columns(3)
for col, cls in zip(cols, CLASSES):
pct = int(final_scores[cls] * 100)
is_primary = cls == primary
card_class = "result-card primary" if is_primary else "result-card"
sev = rec["severity"] if is_primary else ""
badge = ""
if is_primary and sev:
sev_color = SEVERITY_COLORS.get(sev, "#8b949e")
badge = (f"<div class='severity-badge' style='background:{sev_color}20;"
f"color:{sev_color};border:1px solid {sev_color};'>"
f"{sev.replace('_',' ').title()} / {SEVERITY_AR.get(sev,'')}</div>")
col.markdown(f"""
<div class='{card_class}'>
<div class='result-label'>{cls.title()} / {ARABIC_LABELS[cls]}</div>
<div class='result-value' style='color:{COLORS[cls]}'>{pct}%</div>
{badge}
</div>""", unsafe_allow_html=True)
# โ”€โ”€ PRIMARY LABEL โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if not rec["suicidal_flag"]:
cause_label = CAUSE_AR.get(rec["cause"], rec["cause"])
st.markdown(
f"<p style='text-align:center;margin-top:10px;font-size:17px;color:#8b949e;'>"
f"Primary: <strong style='color:{COLORS[primary]}'>{primary.title()} / {ARABIC_LABELS[primary]}</strong>"
f" &nbsp;|&nbsp; Cause detected / ุงู„ุณุจุจ ุงู„ู…ูƒุชุดู: "
f"<strong style='color:#e3b341'>{rec['cause'].replace('_',' ').title()} / {cause_label}</strong></p>",
unsafe_allow_html=True,
)
# โ”€โ”€ CRISIS BOX โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if rec["suicidal_flag"]:
st.markdown("""
<div class='crisis-box'>
<h3 style='color:#f85149;margin-top:0;'>๐Ÿšจ Crisis Support Needed / ู…ุทู„ูˆุจ ุฏุนู… ุฃุฒู…ุฉ</h3>
</div>""", unsafe_allow_html=True)
# โ”€โ”€ SCORE DETAILS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with st.expander("Show score breakdown / ุนุฑุถ ุชูุงุตูŠู„ ุงู„ู†ุชุงุฆุฌ"):
c1, c2 = st.columns(2)
c1.markdown("**Text model / ู…ูˆุฏูŠู„ ุงู„ู†ุต:**")
for cls in CLASSES:
c1.markdown(f"- {cls} / {ARABIC_LABELS[cls]}: **{int(text_scores[cls]*100)}%**")
c2.markdown("**Survey model / ู…ูˆุฏูŠู„ ุงู„ุณูŠุฑูุงูŠ:**")
for cls in CLASSES:
c2.markdown(f"- {cls} / {ARABIC_LABELS[cls]}: **{int(survey_scores[cls]*100)}%**")
# โ”€โ”€ RECOMMENDATIONS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
st.markdown("---")
st.markdown("## ๐Ÿ’ก Recommendations / ุงู„ุชูˆุตูŠุงุช")
col_tips, col_res = st.columns(2)
with col_tips:
st.markdown("<div class='rec-block'>", unsafe_allow_html=True)
st.markdown("<div class='rec-title'>โœ… Practical Tips / ู†ุตุงุฆุญ ุนู…ู„ูŠุฉ</div>",
unsafe_allow_html=True)
tips_en = rec.get("tips_en", [])
tips_ar = rec.get("tips_ar", [])
for en, ar in zip(tips_en, tips_ar):
st.markdown(
f"<div class='rec-item'>{en}"
f"<div class='ar-text' dir='rtl'>โ€ข {ar}</div></div>",
unsafe_allow_html=True,
)
st.markdown("</div>", unsafe_allow_html=True)
with col_res:
st.markdown("<div class='rec-block'>", unsafe_allow_html=True)
st.markdown("<div class='rec-title'>๐Ÿ“š Resources / ู…ูˆุงุฑุฏ ู…ููŠุฏุฉ</div>",
unsafe_allow_html=True)
res_en = rec.get("resources_en", [])
res_ar = rec.get("resources_ar", [])
for en, ar in zip(res_en, res_ar):
st.markdown(
f"<div class='rec-item'>{en}"
f"<div class='ar-text' dir='rtl'>โ€ข {ar}</div></div>",
unsafe_allow_html=True,
)
st.markdown("</div>", unsafe_allow_html=True)
# โ”€โ”€ REFERRAL โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ref_en = rec.get("referral_en", "")
ref_ar = rec.get("referral_ar", "")
if ref_en:
box_class = "crisis-box" if rec["suicidal_flag"] else "referral-box"
st.markdown(
f"<div class='{box_class}'>"
f"<strong>๐Ÿฅ When to seek help / ู…ุชู‰ ุชุทู„ุจ ุงู„ู…ุณุงุนุฏุฉ:</strong><br>"
f"{ref_en}<br>"
f"<span dir='rtl' style='color:#f0a0a0;font-size:13px;'>{ref_ar}</span>"
f"</div>",
unsafe_allow_html=True,
)
st.markdown(
"<p style='text-align:center;color:#484f58;font-size:12px;margin-top:20px;'>"
"โš ๏ธ This system is for awareness only and is not a substitute for professional medical diagnosis.<br>"
"ู‡ุฐุง ุงู„ู†ุธุงู… ู„ู„ุชูˆุนูŠุฉ ูู‚ุท ูˆู„ูŠุณ ุจุฏูŠู„ุงู‹ ุนู† ุงู„ุชุดุฎูŠุต ุงู„ุทุจูŠ ุงู„ู…ุชุฎุตุต.</p>",
unsafe_allow_html=True,
)