A production-grade, end-to-end hybrid physiological monitoring system. This system processes raw bio-signals from wearable sensors to classify psychological and physiological stress using high-accuracy Machine Learning.
The system operates as follows:
- Model Cache (Startup / Cold Start): The FastAPI inference service starts, checks for a cached model, and if missing, automatically retrieves the trained pipeline (
stress_pipeline.joblib) from your public Hugging Face Hub repository. - Data Ingestion: A client app or hardware hub (ESP32) samples raw Blood Volume Pulse (BVP @ 100Hz), Galvanic Skin Response (GSR @ 10Hz), and Temperature (TEMP @ 1Hz).
- Transmission: The raw sensor windows (minimum 10-second segments) are streamed to the Main Application Backend.
- Stress Inference: The Main Backend forwards these windows to the ML Microservice via
POST /predict(secured withX-API-Key). - Feature Extraction & Math: The microservice filters the signals (Butterworth bandpass/lowpass), extracts 15 HRV and GSR features, and runs them through the ML pipeline to return a stress probability score.
stress-management-system/
├── ml/ # Phase 1: Training & Evaluation Pipeline
│ ├── src/ # Feature extraction logic & windowing algorithms
│ │ ├── features/ # HRV, EDA, and Temperature feature math
│ │ └── preprocessing.py # Butterworth filters (0.5-4Hz BVP, 1Hz EDA)
│ ├── models/ # Exported models & huggingface model cards
│ ├── upload_model.py # Python script to upload artifacts to HuggingFace
│ ├── upload_model.sh # Bash wrapper for HuggingFace model uploading
│ └── run.sh # Automation script (train, test, evaluate)
│
├── ml-service/ # Phase 2: FastAPI Inference Microservice
│ ├── app/ # FastAPI application
│ │ ├── core/ # Configuration, authentication, & model loading
│ │ ├── routes/ # Prediction router (/predict, /health)
│ │ └── schemas/ # Pydantic schemas validating window lengths
│ ├── tests/ # 31 Pytest unit & integration tests
│ ├── requirements.txt # Pinned Python package dependencies for production
│ └── run.sh # Dev server runner and test automation script
│
├── app/ # Phase 3: Next.js Full-Stack Application
│ ├── src/ # Application source code
│ │ ├── app/ # App Router pages and API routes (/api/ingest)
│ │ ├── components/ # shadcn/ui components & live dashboard UI
│ │ ├── hooks/ # Data fetching and polling hooks
│ │ └── lib/ # Baseline Psychological scoring, Prisma, and ML Client logic
│ ├── prisma/ # Database schema (PostgreSQL)
│ └── package.json # Node dependencies (Next.js 16, Tailwind, Clerk)
│
├── firmware/ # Phase 4: ESP32 PlatformIO Firmware
│ ├── include/ # Config (pins, rates) and header declarations
│ ├── src/ # Sensor drivers, sampler, HTTP client, provisioning
│ └── platformio.ini # Build config and library dependencies
│
├── render.yaml # Render Infrastructure-as-Code Blueprint
├── requirements.md # Physiological signal processing requirements (Phase 1)
└── microservices_requirements.md # Microservice engineering requirements (Phase 2)
- Setup & Train:
This will set up a virtual environment, install dependencies, run LOSO (Leave-One-Subject-Out) cross-validation, and export
cd ml bash run.shstress_pipeline.joblibinsidemodels/. - Push to HuggingFace:
Make sure you have a write token from your HuggingFace settings.
bash upload_model.sh
- Start the API server:
On first run, this generates a local
cd ../ml-service bash run.sh.envfile with a secure, randomAPI_KEYand automatically fetches the pipeline model from HuggingFace. - Verify status:
- Healthcheck:
http://localhost:8000/health - API Docs:
http://localhost:8000/docs(Swagger UI)
- Healthcheck:
This repository is optimized for one-click deployments on Render.
- Commit your changes and push them to your GitHub repository.
- Connect your GitHub account to Render.
- Render will automatically read the root-level
render.yamland configure the service. - Set the following environment variables in your Render Dashboard:
HF_REPO_ID: Your Hugging Face repository identifier (e.g.RishiAP/stress-detection-pipeline).API_KEY: A shared cryptographic key to secure the/predictendpoint.ALLOWED_ORIGINS: Comma-separated list of allowed origins (e.g.,https://my-app.com).