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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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
---
title: BREATHE
emoji: 🫁
colorFrom: blue
colorTo: indigo
sdk: docker
app_file: app.py
pinned: false
---

# 🫁 BREATHE

> **Stress Intelligence Platform** β€” understand your stress before it controls you.

BREATHE is a full-stack web app that fuses psychometric lifestyle data and free-text journaling through an ML pipeline to give you a personalised stress score, trend charts, and actionable advice β€” all in a clean, dark-themed React dashboard.

---

## ✨ Features

### Core
- **Secure auth** β€” sign up / log in with hashed passwords and server-side sessions
- **Psychometric assessment** β€” 12 lifestyle sliders (sleep, work hours, screen time, caffeine …) + 4 categorical cues
- **Text journaling** β€” write freely; RoBERTa NLP analyses the sentiment
- **ML fusion** β€” tabular ensemble (LightGBM / CatBoost / XGBoost / Stacking) + RoBERTa β†’ weighted probability fusion β†’ 5-class stress output
- **Demo mode** β€” app runs fully with rule-based heuristics when ML model files are absent

### Dashboard
- Area timeline chart of stress scores
- Pie distribution of stress levels
- Stat cards (total assessments, latest level, average score, trend)
- Recent assessments with per-assessment detail modal
- Stress relief activity panel (accordion)

### Profile
- Upload a profile photo (JPG/PNG/GIF/WebP, max 2 MB)
- Set gender, working status, and date of birth
- Write a short bio
- **Anonymous mode** β€” toggle to hide your name in shared/exported views

### Gratitude Journal
- Write 3 prompted daily gratitude items + optional mood tag + free-text reflection
- All entries saved to your account with timestamps
- Paginated history view with expand/collapse and per-entry delete

### Daily To-Do
- Add tasks with High / Medium / Low priority
- Filter by All / Active / Done; bulk-clear completed tasks
- **Stored in browser localStorage only** β€” never sent to the server

### Breathe Hub
- Full-screen activity centre (no sidebar) β€” opens on login
- 6 activity cards: Gratitude Journal, Box Breathing, Daily To-Do, Body Scan, Sound Bath, Progressive Relaxation
- Guided exercise modal with step-through progress dots for breathing and relaxation exercises
- Quick nav buttons to Dashboard and Profile

---

## πŸ–₯️ Tech Stack

| Layer | Technology |
|-------|------------|
| Frontend | React 18, Vite 5, React Router v6, Recharts 2 |
| Backend | Python 3.10+, Flask 3, Flask-SQLAlchemy |
| Database | SQLite (dev) / PostgreSQL (prod) |
| Auth | Server-side sessions, Werkzeug password hashing |
| ML β€” Tabular | LightGBM / CatBoost / XGBoost / Stacking ensemble |
| ML β€” Text | RoBERTa-base fine-tuned on mental-health dataset |
| ML β€” Fusion | Weighted probability fusion β†’ Minimal / Mild / Moderate / Severe / Critical |

---

## πŸš€ Quick Start

> Full instructions are in [SETUP.md](SETUP.md). This is the short version.

### Prerequisites
- Python β‰₯ 3.10
- Node.js β‰₯ 18 + npm β‰₯ 9

### 1 β€” Install Python deps
```bash
cd breathe-app
python -m venv venv
source venv/bin/activate          # Windows: venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

### 2 β€” Configure environment
```bash
cp .env.example .env
# edit .env β€” set SECRET_KEY at minimum
```

### 3 β€” Install frontend deps
```bash
cd frontend && npm install && cd ..
```

### 3.1 β€” Docker deployment support
This repo includes a root-level `Dockerfile` so you can build the app as a single container for Hugging Face Spaces or any Docker host.

### 4 β€” Run (two terminals)

**Terminal 1 β€” Flask API (port 5000)**
```bash
source venv/bin/activate
python app.py
```

**Terminal 2 β€” React dev server (port 5173)**
```bash
cd frontend
npm run dev
```

Open **http://localhost:5173** β€” after login you land on the Breathe hub πŸŽ‰

---

## πŸ“ Project Structure

```
breathe-app/
β”œβ”€β”€ app.py                    # WSGI entry-point
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env.example
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ __init__.py           # Flask app factory
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.py           # User ORM (incl. profile fields)
β”‚   β”‚   β”œβ”€β”€ assessment.py     # Assessment ORM
β”‚   β”‚   └── gratitude.py      # GratitudeEntry ORM
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ auth.py           # /api/auth/*
β”‚   β”‚   β”œβ”€β”€ assessments.py    # /api/assessments/*
β”‚   β”‚   β”œβ”€β”€ profile.py        # /api/profile/*
β”‚   β”‚   └── gratitude.py      # /api/gratitude/*
β”‚   └── ml/ml_engine.py       # Inference wrapper + demo fallback
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ vite.config.js        # Proxies /api β†’ Flask :5000
β”‚   └── src/
β”‚       β”œβ”€β”€ pages/
β”‚       β”‚   β”œβ”€β”€ LandingPage.jsx
β”‚       β”‚   β”œβ”€β”€ AuthPage.jsx
β”‚       β”‚   β”œβ”€β”€ BreathePage.jsx    # Activity hub (post-login home)
β”‚       β”‚   β”œβ”€β”€ DashboardPage.jsx
β”‚       β”‚   β”œβ”€β”€ AssessPage.jsx
β”‚       β”‚   β”œβ”€β”€ HistoryPage.jsx
β”‚       β”‚   β”œβ”€β”€ ProfilePage.jsx
β”‚       β”‚   β”œβ”€β”€ GratitudePage.jsx
β”‚       β”‚   └── TodoPage.jsx
β”‚       β”œβ”€β”€ components/Layout.jsx  # Sidebar shell
β”‚       β”œβ”€β”€ context/AuthContext.jsx
β”‚       └── api/client.js          # Fetch wrapper (get/post/put/del)
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ psychometric/         # .pkl files from notebook
β”‚   └── text/                 # roberta-model.pt
└── instance/
    └── breathe.db            # SQLite DB (auto-created)
```

---

## 🧠 ML Pipeline

```
Lifestyle cues (12 numeric + 4 categorical)
        β”‚
        β–Ό
  Feature engineering (PolynomialFeatures, scaling, label encoding)
        β”‚
        β–Ό
  Stacking ensemble  ──────────────────────────────┐
  (LightGBM + CatBoost + XGBoost + RF + MLP)       β”‚
        β”‚                                           β”‚
        β–Ό                                           β–Ό
  psycho_score (0–1)              text_score (0–1) ← RoBERTa fine-tuned
        β”‚                                           β”‚
        └──────────── fusion (50/50) β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
              fused_score β†’ stress label
          [Minimal | Mild | Moderate | Severe | Critical]
```

The engine falls back to **Demo mode** (rule-based heuristics) if model files are missing.

---

## πŸ”Œ API Reference

### Auth
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/auth/signup` | Register (`username`, `email`, `password`) |
| POST | `/api/auth/login` | Login (`email` or `username`, `password`) |
| POST | `/api/auth/logout` | Logout |
| GET | `/api/auth/me` | Current user |

### Assessments
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/assessments` | Submit assessment |
| GET | `/api/assessments` | List (paginated `?page=1&per_page=20`) |
| GET | `/api/assessments/<id>` | Single assessment |
| GET | `/api/assessments/summary` | Dashboard data + timeline |

### Profile
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/api/profile` | Get current profile |
| PUT | `/api/profile` | Update bio, gender, working status, DOB, anonymous flag |
| POST | `/api/profile/avatar` | Upload / remove avatar (base64 data-url) |

### Gratitude Journal
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/gratitude` | Save entry (`items[]`, `content`, `mood`) |
| GET | `/api/gratitude` | List entries (paginated `?page=1`) |
| DELETE | `/api/gratitude/<id>` | Delete entry |

---

## πŸ—ΊοΈ Page Routes

| URL | Page | Auth |
|-----|------|------|
| `/` | Landing page | Public |
| `/auth` | Login / Signup | Public |
| `/app/breathe` | Breathe hub (post-login home) | Protected |
| `/app/dashboard` | Stress dashboard | Protected |
| `/app/assess` | New assessment | Protected |
| `/app/history` | Assessment history | Protected |
| `/app/profile` | User profile | Protected |
| `/app/gratitude` | Gratitude journal | Protected |
| `/app/todo` | Daily to-do list | Protected |

---

## πŸ“¦ Production Build

```bash
# Build React bundle (output β†’ frontend/dist/)
cd frontend && npm run build && cd ..

# Flask auto-serves frontend/dist/ β€” no Node process needed
gunicorn "app:app" --workers 2 --bind 0.0.0.0:5000 --timeout 120
```

---

## πŸ“„ License

MIT β€” see `LICENSE` for details.


---

## πŸ–₯️ Tech Stack

| Layer | Technology |
|-------|-----------|
| Frontend | React 18, Vite 5, React Router v6, Recharts 2 |
| Backend | Python 3.10+, Flask 3, Flask-SQLAlchemy |
| Database | SQLite (dev) / PostgreSQL (prod) |
| Auth | Server-side sessions, Werkzeug password hashing |
| ML β€” Tabular | LightGBM / CatBoost / XGBoost / Stacking ensemble |
| ML β€” Text | RoBERTa-base fine-tuned on mental-health dataset |
| ML β€” Fusion | Weighted probability fusion β†’ Minimal / Mild / Moderate / Severe / Critical |

---

## πŸš€ Quick Start

> Full instructions are in [SETUP.md](SETUP.md). This is the short version.

### Prerequisites
- Python β‰₯ 3.10
- Node.js β‰₯ 18 + npm β‰₯ 9

### 1 β€” Install Python deps
```bash
python -m venv venv
source venv/bin/activate          # Windows: venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

### 2 β€” Configure environment
```bash
cp .env.example .env
# edit .env β€” set SECRET_KEY at minimum
```

### 3 β€” Install frontend deps
```bash
cd frontend && npm install && cd ..
```

### 3.1 β€” Docker deployment support
This repo includes a root-level `Dockerfile` so you can build the app as a single container for Hugging Face Spaces or any Docker host.

### 4 β€” Run (two terminals)

**Terminal 1 β€” Flask API**
```bash
source venv/bin/activate
python app.py
```

**Terminal 2 β€” React dev server**
```bash
cd frontend
npm run dev
```

Open **http://localhost:5173** πŸŽ‰

---

## πŸ“ Project Structure

```
breathe-app/
β”œβ”€β”€ app.py                  # WSGI entry-point
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env.example
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ __init__.py         # Flask app factory
β”‚   β”œβ”€β”€ models/             # SQLAlchemy ORM models
β”‚   β”œβ”€β”€ routes/             # Auth + Assessment API blueprints
β”‚   └── ml/ml_engine.py     # Inference wrapper + demo fallback
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ vite.config.js      # Proxies /api β†’ Flask :5000
β”‚   └── src/
β”‚       β”œβ”€β”€ pages/          # AuthPage, DashboardPage, AssessPage, HistoryPage
β”‚       β”œβ”€β”€ components/     # Layout (sidebar shell)
β”‚       β”œβ”€β”€ context/        # AuthContext
β”‚       └── api/client.js   # Fetch wrapper
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ psychometric/       # .pkl files from notebook
β”‚   └── text/               # roberta-model.pt
└── notebooks/              # Original Jupyter notebooks
```

---

## 🧠 ML Pipeline

```
Lifestyle cues (12 numeric + 4 categorical)
        β”‚
        β–Ό
  Feature engineering (PolynomialFeatures, scaling, label encoding)
        β”‚
        β–Ό
  Stacking ensemble  ──────────────────────────────┐
  (LightGBM + CatBoost + XGBoost + RF + MLP)       β”‚
        β”‚                                           β”‚
        β–Ό                                           β–Ό
  psycho_score (0–1)              text_score (0–1) ← RoBERTa fine-tuned
        β”‚                                           β”‚
        └──────────── fusion (50/50) β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
              fused_score β†’ stress label
          [Minimal | Mild | Moderate | Severe | Critical]
```

The engine falls back to **Demo mode** (rule-based heuristics) if model files are missing β€” the full UI still works.

---

## πŸ”Œ API Reference

| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/auth/signup` | Register (`username`, `email`, `password`) |
| POST | `/api/auth/login` | Login (`email` or `username`, `password`) |
| POST | `/api/auth/logout` | Logout |
| GET | `/api/auth/me` | Current user |
| POST | `/api/assessments` | Submit assessment |
| GET | `/api/assessments` | List (paginated) |
| GET | `/api/assessments/<id>` | Single assessment |
| GET | `/api/assessments/summary` | Dashboard data |

---

## πŸ“¦ Production Build

```bash
# Build React bundle (output β†’ frontend/dist/)
cd frontend && npm run build && cd ..

# Flask auto-serves frontend/dist/ β€” no Node process needed
gunicorn "app:app" --workers 2 --bind 0.0.0.0:5000 --timeout 120
```

---

## 🀝 Contributing

1. Fork the repo
2. Create a feature branch: `git checkout -b feat/my-feature`
3. Commit your changes: `git commit -m "feat: add my feature"`
4. Push and open a Pull Request

---

## πŸ“„ License

MIT β€” see `LICENSE` for details.