Ace-119 commited on
Commit
492f6b1
·
1 Parent(s): 570a76f

Fix HF Space config: enable docker

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -0
  2. README.md +214 -49
Dockerfile CHANGED
@@ -1,5 +1,7 @@
1
  # ── Base image ──────────────────────────────────────────────────────────────
2
  # Python 3.11 slim keeps the image lean while matching the dev environment.
 
 
3
  FROM python:3.11-slim
4
 
5
  # ── System dependencies ──────────────────────────────────────────────────────
 
1
  # ── Base image ──────────────────────────────────────────────────────────────
2
  # Python 3.11 slim keeps the image lean while matching the dev environment.
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
  FROM python:3.11-slim
6
 
7
  # ── System dependencies ──────────────────────────────────────────────────────
README.md CHANGED
@@ -7,72 +7,237 @@ sdk: docker
7
  pinned: false
8
  license: mit
9
  ---
 
10
 
11
- # 🧠 StressDetect
12
 
13
- A full-stack mental health application for real-time stress detection with
14
- personalized interventions built as an end-to-end ML systems project.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  ## Architecture
17
 
 
 
 
 
 
 
 
 
 
 
 
18
  ```
19
- ┌─────────────────────────────────────────────────┐
20
- │ Streamlit UI │ ← port 7860 (public)
21
- │ Dashboard · History & Analytics · Settings │
22
- └──────────────────┬──────────────────────────────┘
23
- REST (localhost)
24
- ┌──────────────────▼──────────────────────────────┐
25
- │ FastAPI Backend │ ← port 8000 (internal)
26
- │ /analyze · /history · /feedback · /personalize │
27
- └──────────────────┬──────────────────────────────┘
28
-
29
- ┌───────────┼───────────┐
30
- ▼ ▼ ▼
31
- CNN Model SQLite DB Intervention
32
- (MC-Dropout) (encrypted) Engine
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  ```
34
 
35
- ## Model
 
 
 
 
 
 
36
 
37
- - **Multichannel 1D CNN** with multi-head self-attention (4 heads)
38
- - **MC-Dropout ensemble** (3 passes) for uncertainty estimation
39
- - **FPR-constrained threshold calibration** (max FPR 20%)
40
- - **Focal loss** + cosine LR warmup + early stopping
41
- - Checkpoint: [`Ace-119/stress-detection-cnn`](https://huggingface.co/Ace-119/stress-detection-cnn)
42
 
43
- ## Safety
 
 
44
 
45
- - **4-layer intervention engine** with 988 crisis circuit breaker
46
- - Crisis keywords (suicide/self-harm) → immediate 988 lifeline, pipeline halts
47
- - 8 trigger categories: sleep, work, exam, money, relationship, health, grief, loneliness
48
- - Escalation tracker: 3+ consecutive high-stress sessions → professional referral
49
 
50
- ## Features
 
 
51
 
52
- | Feature | Detail |
53
- |---|---|
54
- | Stress scoring | CNN probability + MC-Dropout uncertainty |
55
- | Temporal profiling | Adaptive threshold, velocity, volatility |
56
- | Interventions | Progressive step-by-step guided flow |
57
- | RL feedback loop | User + LLM-as-judge reward signal |
58
- | Personalization | Per-user score bias from feedback history |
59
- | Analytics | Timeline, calendar heatmap, polar chart, trigger frequency |
60
- | Security | JWT auth, bcrypt passwords, AES-256 history encryption |
61
 
62
- ## Quick Start (local)
63
 
64
  ```bash
65
- git clone https://github.com/Ace-119/StressDetection
66
- cd StressDetection
67
- pip install -r requirements-train.txt # full deps including training
68
- python scripts/download_model.py # pulls model.pt from HF Hub
69
- make dev # FastAPI :8000 + Streamlit :8501
70
  ```
71
 
72
- ## Crisis Resources
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- This app always surfaces crisis resources when needed.
75
 
76
- **988 Suicide & Crisis Lifeline** Call or text **988** (US)
77
- **Crisis Text Line** — Text HOME to **741741**
78
- **SAMHSA Helpline** — 1-800-662-4357 (free, confidential, 24/7)
 
7
  pinned: false
8
  license: mit
9
  ---
10
+ # StressDetect - MindView
11
 
12
+ An end-to-end stress detection platform with:
13
 
14
+ - A secure FastAPI backend for analysis, history, and personalization
15
+ - A Streamlit dashboard for check-ins, trends, and interventions
16
+ - A deep learning pipeline for training and retraining text classifiers
17
+ - A safety-first intervention engine with crisis escalation pathways
18
+
19
+ This repository is designed as both a usable application and a complete ML systems project.
20
+
21
+ ## What This Project Does
22
+
23
+ Given short or long free-text input, the system:
24
+
25
+ 1. Cleans and processes text
26
+ 2. Predicts stress probability with uncertainty-aware inference
27
+ 3. Assigns a human-readable stress level
28
+ 4. Applies temporal modeling from user history
29
+ 5. Returns personalized intervention guidance
30
+ 6. Stores encrypted session history and feedback
31
+
32
+ ## Key Features
33
+
34
+ - Model inference with MC-Dropout uncertainty estimation
35
+ - Adaptive thresholding and temporal stress profiling
36
+ - Rule-based intervention engine with trigger categories
37
+ - Crisis keyword circuit breaker with immediate safety messaging
38
+ - JWT authentication and bcrypt password hashing
39
+ - Encrypted stress history at rest
40
+ - RL-style feedback loop from user and LLM reward signals
41
 
42
  ## Architecture
43
 
44
+ ```text
45
+ Streamlit UI (port 7860)
46
+ |
47
+ | REST
48
+ v
49
+ FastAPI Backend (port 8000)
50
+ |
51
+ +-- Model Inference (CNN/Transformer)
52
+ +-- Temporal Model
53
+ +-- Intervention Engine
54
+ +-- SQLite Database (users, sessions, feedback)
55
  ```
56
+
57
+ Main runtime entry points:
58
+
59
+ - `app.py`: boots FastAPI in-process and starts Streamlit (useful for Spaces)
60
+ - `api/main.py`: API routes, auth, inference, persistence
61
+ - `ui/app.py`: dashboard UI
62
+
63
+ ## Project Layout
64
+
65
+ ```text
66
+ StressDetection/
67
+ |- api/ FastAPI app and request handling
68
+ |- checkpoints/ Trained model checkpoint (model.pt)
69
+ |- data/ Dataset and evaluation files
70
+ |- database/ SQLite access and schema logic
71
+ |- intervention/ Recommendation + temporal modeling
72
+ |- models/ Neural network architectures
73
+ |- scripts/ Data prep and model download utilities
74
+ |- security/ JWT, password hashing, encryption
75
+ |- tests/ Unit and integration tests
76
+ |- training/ Train and retrain scripts
77
+ |- ui/ Streamlit frontend
78
+ |- utils/ Shared helpers
79
+ |- Dockerfile Container build
80
+ |- supervisord.conf Process supervision
81
+ |- requirements.txt Runtime dependencies
82
+ |- requirements-train.txt Training and testing dependencies
83
+ ```
84
+
85
+ ## Quick Start (Local)
86
+
87
+ ### 1. Create Environment and Install
88
+
89
+ Windows PowerShell:
90
+
91
+ ```powershell
92
+ python -m venv .venv
93
+ .\.venv\Scripts\Activate.ps1
94
+ pip install -r requirements.txt
95
  ```
96
 
97
+ Linux/macOS:
98
+
99
+ ```bash
100
+ python -m venv .venv
101
+ source .venv/bin/activate
102
+ pip install -r requirements.txt
103
+ ```
104
 
105
+ ### 2. Download Model Checkpoint
 
 
 
 
106
 
107
+ ```bash
108
+ python scripts/download_model.py
109
+ ```
110
 
111
+ ### 3. Run Backend API
 
 
 
112
 
113
+ ```bash
114
+ uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload
115
+ ```
116
 
117
+ ### 4. Run Dashboard
 
 
 
 
 
 
 
 
118
 
119
+ In a second terminal:
120
 
121
  ```bash
122
+ streamlit run ui/app.py
 
 
 
 
123
  ```
124
 
125
+ Open:
126
+
127
+ - API docs: http://localhost:8000/docs
128
+ - Dashboard: http://localhost:8501
129
+
130
+ ## Makefile Commands
131
+
132
+ Common workflows:
133
+
134
+ - `make install` install runtime deps
135
+ - `make download` download checkpoint
136
+ - `make run` run FastAPI
137
+ - `make dashboard` run Streamlit
138
+ - `make preprocess` prepare dataset
139
+ - `make train` train model
140
+ - `make retrain` retrain with feedback
141
+ - `make test` run tests
142
+ - `make lint` run Ruff lint fixes
143
+
144
+ Note: the file is named `MakeFile` in this repo.
145
+
146
+ ## API Endpoints
147
+
148
+ Public:
149
+
150
+ - `GET /health` service heartbeat
151
+ - `GET /model/info` model metadata
152
+ - `POST /register` create account and return token
153
+ - `POST /login` authenticate and return token
154
+
155
+ Authenticated (Bearer JWT):
156
+
157
+ - `POST /analyze` run stress analysis + interventions
158
+ - `GET /history` fetch previous sessions
159
+ - `POST /feedback` submit user feedback for replay/reward
160
+ - `GET /feedback/stats` feedback summary
161
+ - `GET /personalization` personalization status
162
+
163
+ ## Training Workflow
164
+
165
+ ### 1. Prepare Data
166
+
167
+ ```bash
168
+ python scripts/data_preprocessing.py
169
+ ```
170
+
171
+ Expected core training file:
172
+
173
+ - `data/processed/unified_stress.csv`
174
+
175
+ ### 2. Train Model
176
+
177
+ ```bash
178
+ python training/train.py \
179
+ --model cnn \
180
+ --epochs 15 \
181
+ --batch-size 64 \
182
+ --lr 1e-3 \
183
+ --data data/processed/unified_stress.csv \
184
+ --eval-set data/eval/happy_neutral_eval.csv \
185
+ --output checkpoints/model.pt
186
+ ```
187
+
188
+ Supported model options include `cnn`, `deberta`, and `minilm`.
189
+
190
+ ### 3. Retrain from Feedback (Optional)
191
+
192
+ ```bash
193
+ python training/retrain.py
194
+ ```
195
+
196
+ ## Testing
197
+
198
+ Run the full test suite:
199
+
200
+ ```bash
201
+ pytest tests/ -v
202
+ ```
203
+
204
+ Or with make:
205
+
206
+ ```bash
207
+ make test
208
+ ```
209
+
210
+ ## Security and Safety
211
+
212
+ Security:
213
+
214
+ - Passwords hashed with bcrypt
215
+ - JWT-based authenticated access
216
+ - Encrypted session history in SQLite-backed storage
217
+
218
+ Safety behavior:
219
+
220
+ - Crisis phrase detection triggers immediate escalation messaging
221
+ - Multi-level intervention outputs aligned to stress severity
222
+ - Temporal escalation tracking across sessions
223
+
224
+ ## Docker and Deployment
225
+
226
+ Containerized deployment is available through `Dockerfile`.
227
+
228
+ For single-process hosting environments, `app.py` starts FastAPI and Streamlit together.
229
+
230
+ ## Troubleshooting
231
+
232
+ - API cannot connect from UI:
233
+ - Ensure backend is running on port 8000
234
+ - Set `API_URL` if backend is remote
235
+ - Missing checkpoint:
236
+ - Run `python scripts/download_model.py`
237
+ - Import or dependency errors:
238
+ - Reinstall with `pip install -r requirements.txt`
239
+ - For training extras use `requirements-train.txt`
240
 
241
+ ## License and Use
242
 
243
+ This project is intended for research and educational use. It is not a medical device and does not replace professional diagnosis or emergency care.