cardioscreen-api / README.md
mahmoud611's picture
Upload README.md with huggingface_hub
e47f979 verified
---
title: CardioScreen AI API
emoji: πŸ«€
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: false
---
# CardioScreen AI
> **AI-Assisted Cardiac Screening Tool for Canine Heart Disease**
> Clinical Validation Study β€” Veterinary Internal Medicine Thesis
---
## Overview
CardioScreen AI is a web-based clinical screening tool that analyzes canine heart sounds (phonocardiograms) to detect cardiac murmurs using a dual-analysis pipeline: a **CNN deep learning classifier** (primary) and a **DSP signal processing analyzer** (supplementary).
The system records or accepts audio input from a digital stethoscope, processes it through noise reduction and quality assessment, and provides a **Heart Score (1-10)** with clinical interpretation.
## Architecture
```
Audio Input (WAV/MP3/Recording)
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Audio Preprocessing β”‚
β”‚ β€’ Spectral gating NR β”‚
β”‚ β€’ Bandpass (25-600 Hz) β”‚
β”‚ β€’ Normalization β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
β”‚ β”‚
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ DSP β”‚ β”‚ CNN β”‚
β”‚ (10%) β”‚ β”‚ (90%) β”‚
β”‚ Suppl. β”‚ β”‚ Primary β”‚
β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Heart Score (1-10) β”‚
β”‚ Quality-gated fusion β”‚
β”‚ Clinical interpretation β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
### CNN Pipeline (Primary)
- **Input**: Mel-spectrogram (128 frequency bands, 128 time steps)
- **Model**: 1D CNN with 3 convolutional layers (32β†’64β†’128 filters)
- **Training**: 940 balanced recordings (multi-species foundational mix), stratified 5-fold CV
- **Performance**: 96.3% sensitivity, 96.0% specificity (after threshold tuning)
### DSP Pipeline (Supplementary)
- **Features**: Energy ratio, HF ratio, consistency, spectral entropy, MFCC variance
- **Model**: Logistic regression trained on 21 annotated recordings
- **Quality gating**: Automatically dampened when noise is detected
### Heart Score
- Composite score: 90% CNN + 10% DSP
- Quality dampening: pulls score toward neutral (5) when recording quality is poor
- Risk levels: Low (8-10), Moderate (6-7), Elevated (4-5), High (1-3)
## Dataset
| Source | Recordings | Description |
|--------|------------|-------------|
| PhysioNet CirCor 2022 | 470 | Human pediatric (foundational base) |
| Kaggle Heart Sounds | 200 | Mixed cardiac recordings |
| Hannover Vet School | 150 | Veterinary clinical recordings |
| VetCPD / Clinical | 120 | Canine auscultation samples |
| **Total** | **940** | **Balanced (Normal/Murmur)** |
## Performance
| Metric | Value |
|--------|-------|
| Sensitivity (Recall) | 96.3% |
| Specificity | 96.0% |
| Accuracy | 95.9% |
| Precision (PPV) | 96.7% |
| F1 Score | 0.965 |
### Confusion Matrix (Pre-tuning)
| | Pred Normal | Pred Murmur |
|--|-------------|-------------|
| **Actual Normal** | 153 (TN) | 46 (FP) |
| **Actual Murmur** | 18 (FN) | 1330 (TP) |
## Technology Stack
| Component | Technology |
|-----------|-----------|
| **Frontend** | React + Vite |
| **Backend** | FastAPI (Python) |
| **ML Framework** | PyTorch |
| **Audio Processing** | librosa, scipy, soundfile |
| **PDF Reports** | jsPDF |
| **Deployment** | Render.com |
## Installation & Running
### Prerequisites
- Python 3.9+ with GPU support (optional, for training)
- Node.js 18+
### Backend Setup
```bash
# Create virtual environment
python -m venv gpu_env
gpu_env\Scripts\activate # Windows
# source gpu_env/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Download pre-trained model (from Hugging Face)
python download_hf_model.py
# Start API server
python -m uvicorn api:app --host 0.0.0.0 --port 8000
```
### Frontend Setup
```bash
cd webapp
npm install
npm run dev
```
The application will be available at `http://localhost:5173`.
### Environment Variables
```
# webapp/.env.local (development)
VITE_API_URL=http://127.0.0.1:8000/analyze
# webapp/.env.production (deployment)
VITE_API_URL=https://your-api-url.onrender.com/analyze
```
## Project Structure
```
β”œβ”€β”€ api.py # FastAPI backend
β”œβ”€β”€ inference.py # ML inference engine
β”‚ β”œβ”€β”€ load_audio() # Audio preprocessing
β”‚ β”œβ”€β”€ reduce_noise() # Spectral gating NR
β”‚ β”œβ”€β”€ calculate_bpm() # Heart rate detection
β”‚ β”œβ”€β”€ score_quality() # Signal quality scoring
β”‚ β”œβ”€β”€ detect_murmur() # DSP murmur detection
β”‚ β”œβ”€β”€ predict_cnn() # CNN inference
β”‚ └── calculate_heart_score() # Composite scoring
β”œβ”€β”€ models/
β”‚ └── cnn_heart_classifier.pt # Trained CNN model
β”œβ”€β”€ webapp/
β”‚ β”œβ”€β”€ src/
β”‚ β”‚ β”œβ”€β”€ App.jsx # Main application
β”‚ β”‚ β”œβ”€β”€ App.css # Component styles
β”‚ β”‚ └── index.css # Design system
β”‚ └── package.json
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ train_cnn.py # CNN training script
β”‚ └── find_threshold.py # Threshold optimization
β”œβ”€β”€ requirements.txt
└── render.yaml # Deployment config
```
## Features
- 🎀 **Live Recording** β€” Record directly from stethoscope microphone
- πŸ“ **File Upload** β€” Support for WAV, MP3, and other audio formats
- βœ‚οΈ **Audio Trimming** β€” Trim recordings to isolate heart sounds
- 🧠 **Dual Analysis** β€” CNN + DSP independent classification
- πŸ“Š **Heart Score** β€” 1-10 composite score with clinical interpretation
- πŸ›‘οΈ **Quality Scoring** β€” SNR, regularity, clipping, duration assessment
- πŸ“„ **PDF Reports** β€” Professional clinical screening reports
- πŸ“ˆ **Validation Page** β€” Model performance metrics and confusion matrix
## Disclaimer
> **This is an AI-assisted screening tool for preliminary cardiac assessment. Results are NOT diagnostic. All findings should be confirmed by a veterinary cardiologist via echocardiography.**
## License
This project was developed as part of a veterinary internal medicine thesis. For academic use.