AI-powered cardiovascular risk prediction using:
- Multi-modal clinical data (Framingham-based parameters)
- Cardiac imaging analysis (UNet deep learning for medical images)
React frontend with FastAPI backend supporting both assessment methods.
heart-disease-platform/
├── predict_api.py # FastAPI backend with /api/predict and /api/predict-image endpoints
├── train_model.py # PyTorch model training script (clinical data)
├── train_image_model.py # UNet training script (cardiac imaging)
├── run_all.py # Combined frontend + backend launcher
├── main.py # Alternative main entry point
├── frontend/ # React + Vite frontend
│ ├── src/
│ │ ├── main.jsx
│ │ ├── HeartDiseaseAssessment.jsx # Includes image upload tab
│ │ └── index.css
│ ├── package.json
│ ├── vite.config.js # Proxies /api to backend:8000
│ └── index.html
├── scalers.pkl # Feature scalers (generated after clinical model training)
├── heart_disease_model_final.pth # Clinical data model (generated after training)
├── best_cardiac_model.pth # Cardiac imaging model (generated after image training)
└── framingham.csv # Training dataset for clinical model
- Multi-modal neural network for structured health data
- Analyzes demographics, cardiovascular, metabolic, and lab parameters
- Provides modality-specific risk contributions
- Personalized health recommendations
- UNet-based deep learning for cardiac image segmentation
- Upload cardiac scans (X-ray, CT, MRI, ultrasound)
- Automated structural analysis and risk assessment
- Segmentation-based disease probability calculation
- Python 3.8+
- Node.js 16+ and npm
- Miniconda or Anaconda (recommended)
- Install Python dependencies:
# Create conda environment (recommended)
conda create -n heart-disease python=3.11
conda activate heart-disease
# Install PyTorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# Install other dependencies
pip install fastapi uvicorn pydantic numpy pandas scikit-learn matplotlib seaborn opencv-python pillow python-multipart- Train the models (optional):
Clinical Data Model:
python train_model.pyThis will generate:
scalers.pkl- Feature scalersheart_disease_model_final.pth- Trained model weightsbest_model.pth- Best checkpoint during trainingtraining_history.png- Training curves
Cardiac Imaging Model:
python train_image_model.pyThis will generate:
best_cardiac_model.pth- Trained UNet model for cardiac image analysisfinal_cardiac_model.pth- Final model checkpoint
- Install Node dependencies:
cd frontend
npm install
cd ..Run both frontend dev server and backend with one command:
python run_all.pyThis will:
- Start the FastAPI backend on
http://127.0.0.1:8000 - Start the Vite dev server on
http://127.0.0.1:3000 - Automatically open
http://127.0.0.1:3000in your browser - Proxy
/api/*requests to the backend
Build the frontend and serve everything from FastAPI:
# Build frontend
cd frontend
npm run build
cd ..
# Run combined server (will detect and serve built files)
python run_all.pyThe app will be available at http://127.0.0.1:8000
Run backend and frontend separately for development:
Terminal 1 - Backend:
python -m uvicorn predict_api:app --host 127.0.0.1 --port 8000 --reloadTerminal 2 - Frontend:
cd frontend
npm run devOpen http://127.0.0.1:3000 in your browser.
-
GET /- Root endpoint info -
GET /api/health- Health check{ "status": "healthy", "model_loaded": true, "image_model_loaded": true, "timestamp": "2025-12-23T..." } -
POST /api/predict- Heart disease risk prediction from clinical dataRequest body:
{ "age": 55, "sex": "male", "bmi": 27, "systolic_bp": 130, "diastolic_bp": 80, "heart_rate": 75, "prevalent_hypertension": 0, "total_cholesterol": 200, "hdl": 50, "ldl": 120, "triglycerides": 150, "fasting_glucose": 95, "diabetes": 0, "sodium": 140, "potassium": 4.2, "calcium": 9.5, "creatinine": 1.0, "egfr": 90, "smoking": 0, "physical_activity": "moderate", "family_history": 0 }Response:
{ "probability": 0.234, "risk_category": "Low", "modalities": { "cardiovascular": 0.093, "metabolic": 0.082, "labs": 0.035, "demographics": 0.023 }, "feature_importance": [ {"name": "systolic_bp", "value": 10}, {"name": "ldl", "value": 20}, ... ], "recommendations": [...] } -
POST /api/predict-image- Heart disease risk prediction from cardiac imagingRequest:
- Multipart form data
- Field name:
file - Accepted formats: PNG, JPG, JPEG, etc.
Example using curl:
curl -X POST http://127.0.0.1:8000/api/predict-image \ -F "file=@cardiac_scan.jpg"Response:
{ "probability": 0.65, "risk_probability": 0.65, "risk_category": "Medium", "predicted_class": 1, "num_classes": 2, "analysis_type": "cardiac_imaging", "recommendations": [ "Cardiac imaging shows structural analysis completed", "MODERATE RISK indicated in cardiac imaging", "Schedule follow-up cardiac imaging and consultation", "Monitor cardiac symptoms closely" ], "timestamp": "2025-01-15T...", "message": "Image analyzed using 2-class segmentation model" }
# Health check
curl http://127.0.0.1:8000/api/health
# Prediction (PowerShell)
Invoke-RestMethod -Method POST -Uri http://127.0.0.1:8000/api/predict `
-ContentType "application/json" `
-Body '{"age":55,"sex":1,"bmi":27,"systolic_bp":130,"diastolic_bp":80,"heart_rate":75,"prevalent_hypertension":0,"total_cholesterol":200,"hdl":50,"ldl":120,"triglycerides":150,"fasting_glucose":95,"diabetes":0,"sodium":140,"potassium":4.2,"calcium":9.5,"creatinine":1.0,"egfr":90,"smoking":0,"physical_activity":1,"family_history":0}'- Ensure you're running
python run_all.pyfrom theheart-disease-platformdirectory - Check if port 3000 is already in use:
netstat -ano | findstr :3000 - Try running backend and frontend separately (Option 3 above)
- If you haven't trained the model, the API will use a demo fallback (heuristic-based predictions)
- Run
python train_model.pyto generate the model files - Files will be saved in the
heart-disease-platformdirectory
- Ensure backend is running on port 8000
- Check
vite.config.jshas the proxy configured tohttp://127.0.0.1:8000 - Verify CORS middleware is enabled in
predict_api.py
To use different ports:
Backend:
python -m uvicorn predict_api:app --host 127.0.0.1 --port 8001Frontend (update vite.config.js first):
cd frontend
PORT=3001 npm run devThe React app uses:
- Vite - Build tool and dev server
- React 18 - UI framework
- Tailwind CSS (CDN) - Styling (currently loaded via CDN in index.html)
To add proper Tailwind support:
cd frontend
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -pThe FastAPI backend uses:
- FastAPI - Web framework
- PyTorch - Deep learning framework
- Scikit-learn - Data preprocessing and metrics
- Pydantic - Request/response validation
- Build the frontend:
cd frontend
npm run build- Serve with Gunicorn + Uvicorn workers:
pip install gunicorn
gunicorn predict_api:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000- Or use the built-in runner:
python run_all.py