MindGuard-AI / README.md
MohitRajput45's picture
Update README.md
061ee75 verified
---
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>
[![Python](https://img.shields.io/badge/Python-3.10+-3776AB?style=for-the-badge&logo=python&logoColor=white)](https://python.org)
[![Streamlit](https://img.shields.io/badge/Streamlit-1.35+-FF4B4B?style=for-the-badge&logo=streamlit&logoColor=white)](https://streamlit.io)
[![HuggingFace](https://img.shields.io/badge/πŸ€—_HuggingFace-XLM--RoBERTa-FFD21E?style=for-the-badge)](https://huggingface.co)
[![Groq](https://img.shields.io/badge/Groq-LLaMA_3-F55036?style=for-the-badge)](https://groq.com)
[![SHAP](https://img.shields.io/badge/SHAP-Explainable_AI-008080?style=for-the-badge)](https://shap.readthedocs.io)
[![License](https://img.shields.io/badge/License-MIT-22C55E?style=for-the-badge)](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 |
|---|---|---|
| ![Chat](docs/screenshots/chat.png) | ![Dashboard](docs/screenshots/dashboard.png) | ![SHAP](docs/screenshots/shap.png) |
<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*
[![GitHub](https://img.shields.io/badge/GitHub-MohitParmar78-181717?style=for-the-badge&logo=github)](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