---
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
```
ββββ βββββββββββ ββββββββββ βββββββ βββ βββ ββββββ βββββββ βββββββ
βββββ βββββββββββββ βββββββββββββββββββ βββ βββββββββββββββββββββββββββ
ββββββββββββββββββββ ββββββ ββββββ βββββββ ββββββββββββββββββββββ βββ
βββββββββββββββββββββββββββ ββββββ ββββββ ββββββββββββββββββββββ βββ
βββ βββ βββββββββ βββββββββββββββββββββββββββββββββββ ββββββ βββββββββββ
βββ βββββββββ ββββββββββββ βββββββ βββββββ βββ ββββββ ββββββββββ
```
### *When the mind needs a guardian, science answers the call.*
[](https://python.org)
[](https://streamlit.io)
[](https://huggingface.co)
[](https://groq.com)
[](https://shap.readthedocs.io)
[](LICENSE)
> **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.
---
## β 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 |
---
## β 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 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
---
## β 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.
---
## β 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.
---
## β 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
```
---
## β 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
---
## β 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 |
---
## β Screenshots
> *(Replace the placeholder paths below with actual screenshots from your running app)*
| Chat Companion | Clinical Dashboard | SHAP Word-Level XAI |
|---|---|---|
|  |  |  |
---
## β 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
---
## β 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 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
---
## β Author
**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.*
---
*If this project helped you, a β on GitHub means more than you know.*
=======
---
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