Spaces:
Runtime error
Download README.md from IDKHowToCodeFr/tinyml-backend: direct link, hf CLI and curl.
- Browser
- Download file 26.7 kB
-
https://huggingface.co/spaces/IDKHowToCodeFr/tinyml-backend/resolve/main/README.md
- Command line
-
hf download hf://spaces/IDKHowToCodeFr/tinyml-backend/README.md
-
curl -L -o README.md https://huggingface.co/spaces/IDKHowToCodeFr/tinyml-backend/resolve/main/README.md
title: HeartFlow OS Backend
emoji: π«
colorFrom: gray
colorTo: red
sdk: docker
pinned: false
app_port: 8000
βββ βββββββββββ ββββββ βββββββ ββββββββββββββββββββ βββββββ βββ βββ βββββββ ββββββββ
βββ βββββββββββββββββββββββββββββββββββββββββββββββ ββββββββββββ βββ βββββββββββββββββ
ββββββββββββββ ββββββββββββββββ βββ ββββββ βββ βββ ββββββ ββ βββ βββ βββββββββββ
ββββββββββββββ ββββββββββββββββ βββ ββββββ βββ βββ βββββββββββββ βββ βββββββββββ
βββ ββββββββββββββ ββββββ βββ βββ βββ βββββββββββββββββββββββββββ βββββββββββββββββ
βββ ββββββββββββββ ββββββ βββ βββ βββ ββββββββ βββββββ ββββββββ βββββββ ββββββββ
Clinical Cardiovascular Telemetry & Distributed Edge TinyML Transpilation Engine
Executive Architectural Summary
HeartFlow OS is a distributed Edge AI clinical intelligence architecture engineered to decouple real-time physiological diagnostics from cloud computing dependency. The platform bridges clinical cloud machine learning pipelines with resource-constrained, bare-metal microcontrollers (ESP32-S3, ARM Cortex-M4, RP2040, nRF52840).
Continuous multi-channel physiological vitals are evaluated across a calibrated 5-model weighted soft-voting ensemble (Random Forest, Linear SVM, Logistic Regression, MLP Neural Network, and KNN), providing sub-20Β΅s in-browser anomaly classification, automated INT8 fixed-point quantization, 250 Hz Lead-II ECG waveform streaming, and zero-malloc C99 embedded transpilation.
Complete Technical Documentation:
- π ARCHITECTURE.md: Unified canonical architecture specification (v3.4.0), mathematical calibration proofs, embedded C99 hardware ABI, MLOps continuum, and telemetry engine specifications.
- π Interactive Docs: Hosted live in the application at
/docswith interactive in-browser API testing consoles.
Dual-Plane System Architecture
+βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+
| PHYSICAL HARDWARE LAYER |
| [ AD8232 Lead-II ECG ] ββ(250 Hz Analog)ββ> [ ESP32-S3 Microcontroller (12-bit SAR ADC) ] |
| [ MAX30102 PPG SpO2 ] ββ(I2C 400 kHz )ββ> [ firmware/src/main.cpp - PlatformIO C99 ] |
+ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ+
β
β UART Serial Frame: $HR,SPO2,SYS,DIA,TEMP*CRC\n
β Baud Rate: 115,200 bps
βΌ
+βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+
| CLINICAL BROWSER CLIENT |
| (Next.js 16 + React 19 + Tailwind v4) |
| |
| ββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββ βββββββββββββββββββββ |
| β Web Serial API Bridge β ββ> β Lead-II ECG Oscilloscope β ββ> β Telemetry Monitor β |
| β (W3C navigator.serial) β β (60 FPS Canvas GPU Render) β β (/monitor) β |
| βββββββββββββββ¬βββββββββββββ ββββββββββββββββββββββββββββββ βββββββββββββββββββββ |
| β |
| βΌ |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β PRIMARY PLANE: ZERO-CLOUD CLIENT TINYML RUNTIME β |
| β Latency: < 20 Β΅s | Egress: 0.0 KB β |
| β β |
| β Feature Preprocessor ββ> Scaler Normalization + Risk_Severity Feature Synthesis β |
| β β β |
| β ββββββββββββββββββββ΄βββββββββββββββββββ β |
| β βΌ βΌ β |
| β [ INT8 Quantized Weights ] [ 5-Model Soft-Voting ] β |
| β β’ RF (30 Trees, Depth 5) β’ P(Healthy | X) β |
| β β’ Linear SVM Hyperplane β’ P(Hypertension | X) β |
| β β’ Logistic Regression (L2) β’ P(Heart Disease | X) β |
| β β’ Small-NN MLP (16x8 ReLU) β’ P(Asthma | X) β |
| β β’ KNN (k=5 Distance Weighted) β’ P(Diabetes | X) β |
| ββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ |
+βββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββ+
β
β Fallback Tunnel / Cloud REST / Telemetry Ingest
β HTTP POST / WebSocket (/ws/feed)
βΌ
+βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+
| SECONDARY CLOUD BACKEND |
| (FastAPI + Uvicorn Async Core) |
| |
| ββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββ βββββββββββββββββββββ |
| β REST & WS Gateways β ββ> β MLOps Retrain Pipeline β ββ> β Model Registry β |
| β /predict, /ws/feed, etc. β β CSV Ingest & Validation β β 5 .pkl Estimators β |
| βββββββββββββββ¬βββββββββββββ ββββββββββββββββββββββββββββββ βββββββββββββββββββββ |
| β |
| βΌ |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β AST C99 Code Exporter (/export_tinyml) ββ> Zero-Malloc Header (model.h) for Embedded β |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
+βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+
Performance & Latency Benchmark Matrix
Measured across 10,000 synthetic patient telemetry vectors:
| Execution Target | Runtime Architecture | Latency ($p_{50}$) | Throughput | Visual Latency Comparison |
|---|---|---|---|---|
| ESP32-S3 (Bare-Metal) | PlatformIO C99 Static Header | 18.4 Β΅s | 54,340 vec/sec | β (1,902x faster than cloud REST) |
| Client In-Browser | TinyML V8 / SpiderMonkey | 120.0 Β΅s | 8,330 vec/sec | ββ (291x faster than cloud REST) |
| Cloud Container | FastAPI + Uvicorn (HF Spaces) | 35.0 ms | 28 req/sec | ββββββββββββββββββββββββββ (Baseline) |
| Legacy Cloud | Standard PyTorch Container | 160.0 ms | 6 req/sec | ββββββββββββββββββββββββββββββββββββ |
Deterministic SLA Guarantees
- Local TinyML Execution: Guaranteed
< 20 Β΅sprocessing budget per telemetry frame. - Microcontroller Jitter ($p_{99}$):
Β± 1.2 Β΅sdeterministic execution variance without heap allocation (malloc-free). - Network Egress:
0.0 KB/sclient data transmission in primary offline mode.
SRAM Footprint & INT8 Fixed-Point Quantization
Continuous 32-bit floating point weights ($W \in \mathbb{R}$) are quantized to signed 8-bit integers ($Q \in [-128, 127]$) using affine scaling:
| Estimator Architecture | FP32 Footprint | INT8 Quantized | SRAM Savings | Visual Compression | MCU Compatibility |
|---|---|---|---|---|---|
| Random Forest (30 Trees, D=5) | 16,384 bytes | 4,096 bytes | -75.0% | ββββββββββββ 75.0% |
ESP32-S3, RP2040, M4 |
| Small-NN (MLP 16x8 ReLU) | 7,168 bytes | 1,792 bytes | -75.0% | ββββββββββββ 75.0% |
ESP32-S3, RP2040, M4 |
| Linear Support Vector Machine | 3,584 bytes | 896 bytes | -75.0% | ββββββββββββ 75.0% |
ESP32-S3, RP2040, M4 |
| Logistic Regression (L2) | 3,072 bytes | 768 bytes | -75.0% | ββββββββββββ 75.0% |
ESP32-S3, RP2040, M4 |
| k-Nearest Neighbors ($k=5$) | 4,400 bytes | 600 bytes | -86.4% | ββββββββββββ 86.4% |
ESP32-S3, RP2040, M4 |
| COMPOSITE ENSEMBLE TOTAL | 34,608 bytes | 8,152 bytes | -76.4% | ββββββββββββ -76.4% |
All MCUs (> 16KB SRAM) |
Calibrated 5-Model Ensemble Arbitration
Individual estimator posterior probabilities are aggregated using weighted soft-voting to cancel estimator-specific variance:
| Model ID | Estimator Architecture | Weight ($w_i$) | Accuracy | Macro $F_1$ | Primary Architectural Advantage |
|---|---|---|---|---|---|
rf |
Random Forest (30 trees, depth 5) | 25% | 95.4% | 0.938 | Captures non-linear physiological interactions |
small_nn |
MLP Neural Net (16-8 hidden layers) | 25% | 94.9% | 0.931 | Smooth continuous decision manifold |
logreg |
Logistic Regression (L2 Regularized) | 20% | 92.2% | 0.904 | Robust linear baseline calibration |
svm |
Linear SVM (Platt Calibrated) | 15% | 93.5% | 0.912 | Maximum-margin class separation hyperplane |
knn |
k-Nearest Neighbors ($k=5$, Weighted) | 15% | 91.8% | 0.897 | Localized density anomaly cluster identification |
| ENSEMBLE | Composite Soft-Voting Classifier | 100% | 96.2% | 0.948 | -42.6% variance reduction vs single tree |
Hardware Sensor Bridge & UART Protocol
The ESP32 firmware continuously polls the AD8232 ECG analog pin and MAX30102 $I^2C$ registers, assembling packets in NMEA-style ASCII framing:
FORMAT: $HR,SPO2,SYS_BP,DIA_BP,TEMP*CHECKSUM\n
EXAMPLE: $84,97.5,132,86,37.1*4F\n
Microcontroller Pinout (ESP32-S3)
ESP32-S3 DevKit AD8232 ECG Sensor
ββββββββββββββββ ββββββββββββββββ
β 3V3 βββββββββββββββββββββββ€ 3.3V β
β GND βββββββββββββββββββββββ€ GND β
β GPIO4 βββββββββββββββββββββββ€ OUTPUT (ADC) β
β GPIO5 βββββββββββββββββββββββ€ LO+ (LeadOff)β
β GPIO6 βββββββββββββββββββββββ€ LO- (LeadOff)β
ββββββββββββββββ ββββββββββββββββ
ESP32-S3 DevKit MAX30102 PPG Sensor
ββββββββββββββββ ββββββββββββββββ
β 3V3 βββββββββββββββββββββββ€ VIN β
β GND βββββββββββββββββββββββ€ GND β
β GPIO8 βββββββββββββββββββββββ€ SDA (I2C) β
β GPIO9 βββββββββββββββββββββββ€ SCL (I2C) β
ββββββββββββββββ ββββββββββββββββ
Workspace Navigation & Interactive Clinical Routes
| Route | View Name | Clinical & Engineering Purpose |
|---|---|---|
/ |
Landing Hub | Engineering telemetry badges, core specs ticker, and architecture feature overview. |
/monitor |
Clinical Telemetry | 60 FPS Lead-II ECG canvas, vitals monitors, auto-healing WebSocket feed, and SHAP attribution. |
/simulator |
Physiological Simulator | Interactive patient vitals simulator, arrhythmia state machine triggers, and edge inference. |
/dataset |
Dataset Explorer | 60,000 observation ground-truth engine: 9Γ9 Pearson correlation heatmap, 12-bin continuous histograms, balanced disease cohorts, and 25-row paginated inspector. |
/edge |
TinyML Edge Compiler | In-browser & cloud C99 generator for ESP32/Cortex-M with INT8 quantization & zero-malloc memory guarantee. |
/mlops |
MLOps Registry & Retrain | Batch CSV ingestion, schema alias sanitization, F1-macro automated validation gates, and hot-swapping. |
/architecture |
Pipeline Topology | Native architectural schematic detailing client, API, MLOps, and persistence subsystems. |
/history |
Clinical Audit Logs | Local session telemetry history with chronological diagnostic records and risk indicators. |
/analytics |
Longitudinal Analytics | Population distributions, correlation matrices, and model weight arbitration diagnostics. |
/docs |
System & API Specs | Interactive endpoint reference, SVG topology diagrams, latency benchmarks, and live test console. |
API Specifications Contract
Interactive documentation with live in-browser testing is hosted at /docs.
| Method | Endpoint | Latency SLA | Description |
|---|---|---|---|
POST |
/predict |
< 20 Β΅s (Edge) / < 35 ms (Cloud) |
Evaluates 5 vitals against calibrated 5-model ensemble. |
WS |
/ws/feed |
< 5 ms / frame |
60 Hz bidirectional telemetry streaming with concurrent predictions. |
GET |
/export_tinyml |
< 2 ms |
Transpiles trained models to standalone zero-malloc C99 header (model.h). |
POST |
/explain |
< 15 ms |
TreeExplainer SHAP game-theoretic feature attribution. |
POST |
/retrain |
< 3.5 s |
Batch CSV upload, alias resolution, refitting, and zero-downtime hot-swap. |
GET |
/metrics |
< 10 ms |
Accuracy, F1 macro, per-model weights, and edge latency benchmarks. |
GET |
/history |
< 5 ms |
Returns isolated chronological session telemetry audit records. |
GET |
/dataset/summary |
< 5 ms |
Precomputed statistical profiling across 60,000 observations (99.8% bandwidth reduction). |
GET |
/dataset |
< 5 ms |
Dual-mode: legacy list when unparameterized; windowed 25-row records when parameterized. |
GET |
/dataset/download |
< 100 ms |
Direct streaming download of the verified 4.49 MB ground-truth CSV corpus. |
GET |
/health |
< 1 ms |
Verifies FastAPI readiness and model memory residency. |
Quickstart Guide
Option 1: Live Cloud Hugging Face Space (Zero Local Install)
The backend is deployed and running live on Hugging Face Spaces:
- Hugging Face Space: https://huggingface.co/spaces/IDKHowToCodeFr/tinyml-backend
- Direct REST API Gateway:
https://idkhowtocodefr-tinyml-backend.hf.space - Interactive Swagger OpenAPI Docs:
https://idkhowtocodefr-tinyml-backend.hf.space/docs - WebSocket Telemetry Stream:
wss://idkhowtocodefr-tinyml-backend.hf.space/ws/feed
To connect the Next.js frontend to the live cloud backend, set:
NEXT_PUBLIC_API_URL=https://idkhowtocodefr-tinyml-backend.hf.space
NEXT_PUBLIC_WS_URL=wss://idkhowtocodefr-tinyml-backend.hf.space/ws/feed
Option 2: Docker Compose (Full Stack Local)
docker compose up --build
- Web App: http://localhost:3000
- FastAPI OpenAPI Docs: http://localhost:8000/docs
Option 3: Local Development Setup
1. Backend (Python 3.10+)
cd backend
python -m venv .venv
source .venv/bin/activate # Linux / macOS
.\.venv\Scripts\activate # Windows PowerShell
pip install -r requirements.txt
python models.py # Train initial baseline ensemble
uvicorn main:app --reload --port 8000
2. Frontend (Node.js 20+)
cd frontend
npm install
npm run dev
Open http://localhost:3000 in your browser.
Option 4: Hardware Firmware Deployment (ESP32)
- Open
/edgein the application or call/export_tinyml?model_name=rf&quantize=true. - Save the emitted C header to
firmware/src/model.h. - Compile and upload via PlatformIO:
cd firmware
pio run -t upload
pio device monitor -b 115200
Test Verification Suite
The repository is validated against a 58-test automated verification suite (58/58 passing in ~5.5s) enforcing strict clinical, mathematical, and safety invariants across all layers.
Backend Unit & Integration Tests (58/58 passing)
| Test Module | Subsystem Tested | Tests | Key Edge Cases & Invariants Tested |
|---|---|---|---|
test_api.py |
REST Gateways & Routes | 21 | β’ GET /health service readinessβ’ Normal and acute vital sign evaluations ( POST /predict)β’ Game-theoretic SHAP TreeExplainer attributions ( POST /explain)β’ Parameterized C99 exporter queries across all models ( GET /export_tinyml)β’ Dual-mode dataset list & 25-row windowed inspector ( GET /dataset)β’ Statistical moments and CSV download streaming |
test_export.py |
C99 Transpiler & Zero-Malloc ABI | 7 | β’ Transpilation for all 5 models (rf, svm, logreg, small_nn, knn)β’ FP32 double and INT8 fixed-point quantization β’ Zero-Malloc guarantee: static verification that no dynamic allocation calls exist ( malloc, calloc, realloc, free)β’ Balanced braces ( { == }) and include guard integrityβ’ INT8 saturation clipping $[-128, 127]$ and error handling for null/invalid models |
test_dataset_analytics.py |
Dataset Profiler & Explorer | 8 | β’ 9Γ9 Pearson correlation matrix calculation & 12-bin continuous histograms β’ In-memory caching by file modification time ( mtime)β’ Boundary clamping ( page <= 0 clamped to 1, limit > 100 clamped to 100)β’ Out-of-bounds page handling ( rows: []) and case-insensitive cohort filtering |
test_mlops.py |
Schema Ingestion & Validation | 6 | β’ Case-insensitive alias mapping across 40+ header variations (HR, hr, sys_bp, temp)β’ Corrupt character encoding auto-repair ( \ufffd $\to$ Β°C)β’ Missing mandatory signal detection ( ValueError) and all-NaN row droppingβ’ Auto-population of optional canonical metadata |
test_simulator.py |
Telemetry Physics Simulator | 5 | β’ Physiological clamping boundaries under extreme vitals (HR $\in [50, 180]$, $SpO_2 \in [80, 100]$, Temp $\in [36.0, 39.0]$) β’ State machine transitions across all 5 conditions ( asthma, hypertension, heart_disease, diabetes, normal)β’ Physics drift convergence and 1-decimal place precision rounding |
test_preprocessing.py |
Feature Engineering & Scaler | 4 | β’ StandardScaler affine transform and artifact serialization β’ Target leakage prevention and Risk Severity synthesis heuristic |
test_database.py |
SQLite Audit Logger | 3 | β’ Asynchronous schema creation and chronological prediction history logging |
test_backend.py |
Root & Schema Validation | 2 | β’ Docs redirect and HTTP 422 Unprocessable Entity type validation |
test_ensemble.py |
5-Model Soft-Voting Ensemble | 2 | β’ Model loading and probability arbitration variance reduction |
| TOTAL | Full Verification Suite | 58 | 58/58 Passing (100% Green in ~5.5s) |
# Execute full backend test verification suite
pytest tests -v
Frontend Typechecking & Static Build
cd frontend
npx tsc --noEmit
npm run build
License
Distributed under the MIT License. See LICENSE for full details.