Spaces:
Sleeping
Sleeping
File size: 16,083 Bytes
c2d9599 061ee75 c2d9599 173df94 8f1759f 173df94 7f99840 3564336 7f99840 3564336 7f99840 3564336 7f99840 173df94 | 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | ---
title: MindGuard AI
emoji: π‘οΈ
colorFrom: blue
colorTo: indigo
sdk: streamlit
sdk_version: 1.42.0
app_file: app.py
pinned: false
---
# MindGuard AI
(Your description starts here...)
<<<<<<< HEAD
<div align="center">
```
ββββ βββββββββββ ββββββββββ βββββββ βββ βββ ββββββ βββββββ βββββββ
βββββ βββββββββββββ βββββββββββββββββββ βββ βββββββββββββββββββββββββββ
ββββββββββββββββββββ ββββββ ββββββ βββββββ ββββββββββββββββββββββ βββ
βββββββββββββββββββββββββββ ββββββ ββββββ ββββββββββββββββββββββ βββ
βββ βββ βββββββββ βββββββββββββββββββββββββββββββββββ ββββββ βββββββββββ
βββ βββββββββ ββββββββββββ βββββββ βββββββ βββ ββββββ ββββββββββ
```
### *When the mind needs a guardian, science answers the call.*
<br>
[](https://python.org)
[](https://streamlit.io)
[](https://huggingface.co)
[](https://groq.com)
[](https://shap.readthedocs.io)
[](LICENSE)
<br>
> **MindGuard** is a production-grade, multilingual mental health AI that doesn't just respond β
> it *diagnoses*, *explains*, and *alerts*. Every word you type is analyzed by a fine-tuned
> XLM-RoBERTa neural network, explained by Game Theory mathematics (SHAP),
> and answered by a clinical-context-aware Groq LLM.
</div>
---
<br>
## β What Makes This Different
Most mental health chatbots are wrappers around GPT. **MindGuard is not.**
| Capability | Generic Chatbot | **MindGuard** |
|---|---|---|
| Response generation | β
GPT/Claude API call | β
Groq LLaMA 3 with clinical system prompt |
| Emotion detection | β Guessed from LLM output | β
Dedicated XLM-RoBERTa (35 emotions, fine-tuned) |
| Why did it predict that? | β Black box | β
SHAP word-level attribution β mathematically proven |
| Risk escalation | β None | β
High / Medium / Low triage with visual alerts |
| Multilingual | β English only | β
100+ languages via XLM-R architecture |
| Voice input | β None | β
OpenAI Whisper transcription pipeline |
| Clinical audit trail | β None | β
Full SQLite session history with timestamps |
| XAI dashboard | β None | β
Clinician-facing SHAP HTML report viewer |
<br>
---
## β System Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β USER INTERFACE LAYER β
β ββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ β
β β π¬ Chat Companion β β π Clinical Dashboard β β
β β β’ Text input β β β’ Emotion frequency chart β β
β β β’ Voice recording β β β’ Risk distribution chart β β
β β β’ Emotion badge display β β β’ Session history table β β
β β β’ Risk badge display β β β’ π¬ SHAP report viewer β β
β β β’ Inline SHAP report β β β β
β ββββββββββββββ¬ββββββββββββββ ββββββββββββββββββββββββββββββββ β
βββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β user input (text / audio)
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PROCESSING LAYER β
β β
β βββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ β
β β Whisper βββββΆβ XLM-RoBERTa βββββΆβ SHAP Engine β β
β β (audio β β β Emotion Model β β (Game Theory β β
β β text) β β 35 categories β β attribution) β β
β βββββββββββββββ ββββββββββ¬ββββββββββ ββββββββββββββββββββ β
β β emotion + risk β
β βΌ β
β ββββββββββββββββββββββββ β
β β Groq LLaMA 3 LLM β β
β β (clinical response β β
β β generation) β β
β ββββββββββββ¬ββββββββββββ β
βββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PERSISTENCE LAYER β
β SQLite DB (session history) artifacts/shap_report.html β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
<br>
---
## β The XAI Engine β Why This Matters
> *"Trust, but verify."* β Every prediction MindGuard makes can be traced back to the exact words that caused it.
MindGuard uses **SHAP (SHapley Additive exPlanations)** β a mathematically rigorous framework rooted in cooperative Game Theory β to answer:
**"Which specific words made the model predict *Anxiety* over *Depression*?"**
```
Input: "I have a massive presentation tomorrow and my chest is tight."
β β β
βΌ βΌ βΌ
[neutral] [HIGH IMPACT] [HIGH IMPACT]
SHAP β 0.01 "presentation" "chest is tight"
SHAP = +0.43 SHAP = +0.51
β β
ββββββββββββ¬ββββββββββββββ
βΌ
Predicted: ANXIETY (87.3%)
Risk Level: MEDIUM β οΈ
```
This is rendered as an **interactive HTML report** embedded directly in the chat β red highlights push the prediction *toward* the emotion, blue highlights push *against* it.
<br>
---
## β Quick Start
### Prerequisites
```bash
python >= 3.10
```
### 1 Β· Clone & Install
```bash
git clone https://github.com/MohitParmar78/MindGuard-AI-Mental-Health-System.git
cd MindGuard-AI-Mental-Health-System
pip install -r requirements.txt
```
### 2 Β· Configure Secrets
Create a `.env` file at the project root:
```env
GROQ_API_KEY=your_groq_api_key_here
```
> Get your free Groq API key at [console.groq.com](https://console.groq.com)
### 3 Β· Ensure Model Weights Exist
The XLM-RoBERTa fine-tuned weights must be present at:
```
artifacts/
βββ xlmr_weights/
βββ final_mindguard_model/
βββ config.json
βββ pytorch_model.bin
βββ tokenizer_config.json
βββ vocab.json
```
> Train your own using the notebooks in `notebooks/` or download pretrained weights.
### 4 Β· Launch
```bash
cd app
streamlit run main.py
```
Navigate to `http://localhost:8501` in your browser.
<br>
---
## β Project Structure
```
MindGuard-AI-Mental-Health-System/
β
βββ π app/ β Streamlit application
β βββ main.py β Entry point, page routing
β βββ api.py β Cached model loaders (@st.cache_resource)
β βββ components/
β βββ chat_ui.py β Chat interface + SHAP + badge rendering
β βββ dashboard_ui.py β Analytics + XAI report tab
β
βββ π src/ β Core business logic
β βββ chatbot/
β β βββ groq_bot.py β Groq LLM orchestration + DB writes
β βββ explainability/
β β βββ shap_explainer.py β XLM-R + SHAP pipeline β HTML report
β βββ database/
β β βββ db_operations.py β SQLite CRUD operations
β βββ audio/
β βββ audio_processor.py β Whisper transcription wrapper
β
βββ π artifacts/ β Model weights + generated reports
β βββ xlmr_weights/
β β βββ final_mindguard_model/ β Fine-tuned XLM-RoBERTa (35 emotions)
β βββ shap_report.html β Latest SHAP explanation (auto-generated)
β
βββ π data/
β βββ raw/ β Temp audio files from voice input
β
βββ π notebooks/ β Training + experimentation notebooks
βββ requirements.txt
βββ README.md
```
<br>
---
## β Emotion & Risk Classification
### 35-Class Emotion Taxonomy
MindGuard classifies inputs across two merged ontologies:
**Clinical Diagnoses** (7 classes)
| Label | Severity | Risk Mapping |
|---|---|---|
| π΄ Suicidal | Critical | β **HIGH** |
| π΄ Depression | Severe | β **HIGH** |
| π Anxiety | ModerateβSevere | β **MEDIUMβHIGH** |
| π Bipolar | ModerateβSevere | β **MEDIUMβHIGH** |
| π Stress | Moderate | β **MEDIUM** |
| π‘ Personality Disorder | Varies | β **MEDIUM** |
| π’ Normal | None | β **LOW** |
**GoEmotions Fine-Grained** (28 classes) β admiration, amusement, anger, annoyance, approval, caring, confusion, curiosity, desire, disappointment, disapproval, disgust, embarrassment, excitement, fear, gratitude, grief, joy, love, nervousness, neutral, optimism, pride, realization, relief, remorse, sadness, surprise
<br>
---
## β Technology Stack
| Layer | Technology | Why |
|---|---|---|
| **LLM** | Groq + LLaMA 3 | Ultra-low latency inference (<1s), free tier available |
| **Emotion Model** | XLM-RoBERTa (fine-tuned) | Multilingual, 100+ languages, state-of-art on NLP benchmarks |
| **Explainability** | SHAP `shap.Explainer` | Only mathematically rigorous word attribution framework |
| **Speech-to-Text** | OpenAI Whisper | Best-in-class accuracy, runs fully offline |
| **UI Framework** | Streamlit | Rapid ML app development, Python-native |
| **Database** | SQLite | Zero-config, portable, sufficient for session-scale data |
| **ML Utilities** | PyTorch, Transformers | Industry standard deep learning stack |
<br>
---
## β Screenshots
> *(Replace the placeholder paths below with actual screenshots from your running app)*
| Chat Companion | Clinical Dashboard | SHAP Word-Level XAI |
|---|---|---|
|  |  |  |
<br>
---
## β Roadmap
- [x] XLM-RoBERTa 35-emotion fine-tuned classifier
- [x] SHAP word-level explainability with HTML report
- [x] Groq LLM integration with clinical system prompt
- [x] Whisper voice-to-text pipeline
- [x] SQLite audit trail + clinical dashboard
- [x] Real-time risk badge system (High / Medium / Low)
- [ ] RAG integration β retrieve clinical CBT strategies from knowledge base
- [ ] Multi-session memory with persistent user profiles
- [ ] Longitudinal mood tracking with trend analysis
- [ ] Crisis escalation workflow (auto-email / SMS alert)
- [ ] Docker containerization + cloud deployment (AWS/GCP)
- [ ] REST API layer for third-party EHR integration
<br>
---
## β Ethical Disclaimer
```
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MindGuard is a research and portfolio demonstration project. β
β It is NOT a licensed medical device and is NOT a substitute β
β for professional mental health care. β
β β
β If you or someone you know is in crisis, please contact: β
β β’ iCall (India): 9152987821 β
β β’ Vandrevala Foundation: 1860-2662-345 (24x7) β
β β’ International: findahelpline.com β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
<br>
---
## β Author
<div align="center">
**Mohit Parmar**
*B.Tech CSE Β· DIT University, Dehradun*
*Data Science & ML Engineering*
[](https://github.com/MohitParmar78)
*Built with curiosity, caffeine, and an unreasonable belief that AI can make the world kinder.*
</div>
---
<div align="center">
*If this project helped you, a β on GitHub means more than you know.*
</div>
=======
---
title: MindGuard AI
emoji: π
colorFrom: red
colorTo: red
sdk: docker
app_port: 8501
tags:
- streamlit
pinned: false
short_description: Streamlit template space
license: mit
---
# Welcome to Streamlit!
Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
forums](https://discuss.streamlit.io).
>>>>>>> 356433695c22e7cf0159fbc953d720d2de65b8c2
|