Skip to content

Danchi-1/hms.ai

Repository files navigation

HMS-AI β€” Health Monitoring System

HMS-AI is an AI-powered health analytics platform that collects, visualizes, and analyzes data from Bluetooth wearable devices in real time. It combines a modern dark-themed web interface with machine learning insights to give users a personalized picture of their health.


πŸš€ Features

Feature Status Notes
User Authentication βœ… Login/Signup with SHA256 hashing, Flask session management, profile chip in header
Real-time Dashboard βœ… Heart Rate, Steps, Sleep, Calories via Chart.js β€” only populated from real device data
Web Bluetooth Integration βœ… Browser-native BLE via the Web Bluetooth API β€” no server required
AI Health Score βœ… 0–100 score aggregated from vitals with confidence levels and personalized insights
ML Recommendations βœ… Scikit-learn RandomForest classifier in api/predict.py generates context-aware advice
Vital Signs Monitoring βœ… Live heart rate from GATT notifications; BP / SpOβ‚‚ / temp shown only with real sensor data
Sleep Analysis βœ… 7-day sleep chart (hours + efficiency) rendered via Chart.js
Data Export βœ… Full CSV export of health metrics via /api/wearable/data/export
Responsive UI βœ… Glassmorphism dark theme, mobile-first layout, split-panel auth pages
Hybrid Deployment βœ… Vercel (frontend CDN) + Render (Flask/Gunicorn backend)

πŸ”΅ Bluetooth Architecture

HMS-AI uses two complementary Bluetooth integrations:

Web Bluetooth API (Primary β€” browser-side)

The main way users connect wearables. Runs entirely in the browser β€” no server Bluetooth adapter needed.

User's Browser (Chrome/Edge)
  └── navigator.bluetooth.requestDevice()
        └── Native OS device picker shown to user
              └── User selects their wearable
                    β”œβ”€β”€ Read: battery level, manufacturer name
                    └── Subscribe: heart rate notifications (live, every ~1 s)
                          β”œβ”€β”€ Updates dashboard metrics in real time
                          └── POST /api/wearable/ble-reading β†’ persisted to DB

Requirements:

  • HTTPS (or localhost) β€” Web Bluetooth is blocked on plain HTTP
  • Chrome 56+ / Edge 79+ / Opera 43+ β€” Firefox and Safari are not supported
  • A user gesture (button click) to trigger the device picker

Supported devices (anything advertising standard GATT services): Polar Β· Garmin Β· Fitbit Β· Samsung Galaxy Watch Β· Mi Band Β· Amazfit Β· WHOOP Β· Oura Ring …plus any device advertising Heart Rate (0x180D), Blood Pressure (0x1810), Fitness Machine (0x1826), or Glucose (0x1808) services.

Python bleak Library (Secondary β€” server-side)

Located in ble/ble.py. Useful when the Flask server is running locally on the same machine as the wearable device (e.g. a Raspberry Pi, a development laptop).

⚠️ The bleak backend is not called by the web dashboard on cloud deployments (Render/Vercel) because the server has no physical Bluetooth adapter that can reach the user's device.


🎨 Frontend Design System

The UI was redesigned in March 2026 with a new medical-tech aesthetic.

Token Value
Background #060c18 / #0a0f1c (deep charcoal)
Accent Primary #0d9488 (medical teal)
Accent Secondary #10b981 (emerald green)
Font Inter (variable weight)
Cards Glassmorphism β€” backdrop-filter: blur(20px) + subtle teal border glow
Buttons Pill-shaped, gradient fill, shimmer-on-hover

Page layouts:

  • Landing page β€” Split-panel hero (copy left, live health card mock right), stats bar, feature cards, timeline steps, device logos
  • Login / Sign Up β€” Split-panel (decorative left with stats + form right)
  • Dashboard β€” 3-column responsive card grid with glassmorphism cards

πŸ›  Tech Stack

Frontend

  • HTML5 / CSS3 / Vanilla JS (ES6+)
  • Web Bluetooth API β€” browser-native BLE device connection
  • Chart.js β€” sleep trend charts
  • Font Awesome 6 β€” icons
  • Google Fonts β€” Inter

Backend

  • Flask (Python 3.12+)
  • Pandas / NumPy β€” data processing
  • Scikit-learn β€” RandomForest health risk prediction
  • SQLite (dev) / ephemeral cloud DB (prod)
  • Flask-CORS β€” cross-origin headers for Vercel ↔ Render

Infrastructure

  • Vercel β€” frontend (global CDN, rewrite rules)
  • Render β€” backend (Gunicorn WSGI)
  • Docker β€” containerized production build
  • Git β€” CI/CD via push-triggered deployments

πŸ“‚ Project Structure

hms.ai/
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ auth.py          # Login, Register, Profile (/api/auth/*)
β”‚   β”œβ”€β”€ dashboard.py     # Data aggregation (/api/dashboard/<user_id>)
β”‚   β”œβ”€β”€ predict.py       # ML inference (/api/predict/*)
β”‚   β”œβ”€β”€ wearable.py      # Data export, BLE reading ingestion
β”‚   └── ai_advice.py     # Gemini AI health advice (/api/ai/*)
β”œβ”€β”€ ble/
β”‚   └── ble.py           # Python BLE scanner (bleak) β€” local/Pi use
β”œβ”€β”€ database/
β”‚   └── models.py        # User & HealthData SQLite models
β”œβ”€β”€ model_training/
β”‚   β”œβ”€β”€ preprocess.py    # Dataset preparation
β”‚   └── train.py         # Train RandomForest health risk model
β”œβ”€β”€ services/
β”‚   └── background_manager.py  # Background data collection services
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   β”œβ”€β”€ main.css     # Design tokens, navbar, buttons, footer
β”‚   β”‚   β”œβ”€β”€ home.css     # Landing page styles (hero, features, steps)
β”‚   β”‚   β”œβ”€β”€ auth.css     # Login/signup split-panel styles
β”‚   β”‚   └── dashboard.css # Dashboard card grid and metric styles
β”‚   └── js/
β”‚       β”œβ”€β”€ main.js      # Auth forms, mobile nav, loading spinner
β”‚       └── dashboard.js # Dashboard data loading, Web Bluetooth, charts
β”œβ”€β”€ templates/
β”‚   β”œβ”€β”€ index.html       # Landing page
β”‚   β”œβ”€β”€ login.html       # Login page
β”‚   β”œβ”€β”€ signup.html      # Sign up page
β”‚   └── dashboard.html   # Main dashboard
β”œβ”€β”€ app.py               # Flask entry point, route definitions
β”œβ”€β”€ Dockerfile           # Production container
β”œβ”€β”€ requirements.txt     # Python dependencies
└── vercel.json          # Vercel routing/rewrite rules

πŸ”§ Setup & Installation

Prerequisites

  • Python 3.12+
  • pip
  • A Chromium-based browser (Chrome / Edge) for Bluetooth features

Local Development

# 1. Clone
git clone https://github.com/Danchi-1/hms.ai.git
cd hms.ai

# 2. Create virtual environment
python3 -m venv venv
source venv/bin/activate       # Windows: venv\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Configure environment
cp .env.example .env
# Edit .env β€” set SECRET_KEY and GEMINI_API_KEY

# 5. (Optional) Train the ML model
python model_training/preprocess.py
python model_training/train.py

# 6. Run
python app.py

Visit http://localhost:5000 β€” Bluetooth features work on localhost without HTTPS.

Deployment

Platform Config
Render (backend) Build: pip install -r requirements.txt Β· Start: gunicorn app:app
Vercel (frontend) Framework: Other Β· Ensure vercel.json rewrites are present
Docker docker build -t hms-ai . && docker run -p 5000:5000 hms-ai

⚠️ On cloud deployments, the app must be served over HTTPS for Web Bluetooth to work. Render and Vercel both provide HTTPS by default.


πŸ§ͺ ML & AI Integration

Component Details
Model RandomForestClassifier (Scikit-learn)
Inputs Steps, sleep duration, sleep efficiency, heart rate
Output Risk level (Low / Medium / High) + confidence score
Inference Runs on dashboard load via /api/predict/
AI Advice Gemini API (/api/ai/health-advice) generates natural-language recommendations
Training python model_training/train.py β€” saves model.pkl

πŸ“Š Dashboard Data Policy

No data is ever simulated or fabricated. All metric cards (-- by default) update only from two real sources:

  1. Backend API β€” historical data from /api/dashboard/<user_id> (Fitbit sync data stored in DB)
  2. Web Bluetooth β€” live readings from a physically connected BLE device (heart rate notifications via GATT)

Vitals such as blood pressure, SpOβ‚‚, and temperature remain at -- until a compatible sensor provides them. Trend indicators are only shown with real historical comparison data.


πŸ” Security

  • Passwords: SHA256 with salt via hashlib
  • Sessions: Flask session encrypted with SECRET_KEY
  • Input validation: Regex sanitization on username/email fields
  • CORS: Strict origin whitelist (Vercel + Render + localhost only)
  • Bluetooth: Browser enforces HTTPS-only access and explicit user permission per device

🌐 Browser Compatibility

Feature Chrome Edge Firefox Safari
General UI βœ… βœ… βœ… βœ…
Web Bluetooth βœ… βœ… ❌ ❌
Chart.js βœ… βœ… βœ… βœ…

For full functionality (Bluetooth device connection), use Chrome or Edge.


πŸ“„ License

MIT License β€” see LICENSE for details.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors