Skip to content

kspeiris/Hart_prediction_ml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

❀️ Heart Attack Prediction System

Heart Attack Prediction System hero image

Flask-based clinical risk prediction web app powered by a trained logistic regression model.

Python Flask scikit-learn Bootstrap


✨ Overview

The Heart Attack Prediction System is a complete web application for estimating heart attack risk from structured patient data. It combines a polished frontend form, a Flask backend API, and pre-trained machine learning artifacts to produce a probability score and a readable risk category.

This project is designed as a decision-support tool, not a diagnostic system. It helps organize cardiovascular risk indicators into a fast digital assessment flow.

🎯 Key Features

  • Interactive patient intake form with modern UI styling
  • Real-time derived metrics such as Pulse_Pressure and Sedentary_Ratio
  • Backend validation for missing, invalid, and inconsistent input values
  • Logistic regression prediction pipeline using saved .pkl artifacts
  • Risk classification output: Low, Moderate, High, Very High
  • Health check endpoint for deployment and service readiness
  • Image asset support for branding and visual presentation

🧰 Tech Stack

Layer Technology
Frontend HTML, CSS, JavaScript, Bootstrap 5, Font Awesome
Backend Flask, Flask-CORS
ML / Data NumPy, scikit-learn, joblib
Serving Flask development server, Waitress, Gunicorn
Assets PNG illustrations, logos, favicon

πŸ—οΈ System Architecture

flowchart TB
    U["User / Patient / Clinician"]
    F["Frontend Web Interface<br/>HTML, CSS, JavaScript, Bootstrap"]
    B["Flask Backend API<br/>app.py"]
    R["Routes<br/>GET /<br/>GET /health<br/>POST /predict"]
    V["Validation and Feature Checks<br/>Required fields<br/>Numeric conversion<br/>BP and derived feature validation"]
    P["Feature Preparation<br/>Ordered 30-feature vector<br/>Pulse Pressure<br/>Sedentary Ratio"]
    S["Standard Scaler<br/>standard_scaler.pkl"]
    M["Logistic Regression Model<br/>logistic_regression_model.pkl"]
    O["Prediction Output<br/>Probability score<br/>Risk level"]
    D["Results Dashboard<br/>Risk percentage<br/>Recommendations<br/>Key factors"]
    A["Static Assets<br/>/images"]
    H["Health Check<br/>Model readiness"]

    U --> F
    F --> B
    B --> R
    R --> V
    V --> P
    P --> S
    S --> M
    M --> O
    O --> D
    B --> H
    A --> F

    classDef user fill:#ffe6ea,stroke:#d9485f,color:#5c1220,stroke-width:2px;
    classDef frontend fill:#e8f4ff,stroke:#1d70b8,color:#12344d,stroke-width:2px;
    classDef backend fill:#e8f8ef,stroke:#2b8a3e,color:#1f4f2a,stroke-width:2px;
    classDef process fill:#fff4e6,stroke:#f08c00,color:#7a3e00,stroke-width:2px;
    classDef model fill:#f3e8ff,stroke:#7b2cbf,color:#4a1772,stroke-width:2px;
    classDef output fill:#e6fcf5,stroke:#0ca678,color:#0b4f42,stroke-width:2px;
    classDef asset fill:#f1f3f5,stroke:#868e96,color:#343a40,stroke-width:2px;

    class U user;
    class F,D frontend;
    class B,R,H backend;
    class V,P,S process;
    class M model;
    class O output;
    class A asset;
Loading

πŸ” Architecture Notes

  • The browser collects patient inputs and computes some helper values before submission.
  • The Flask backend validates the JSON payload against the expected 30-feature schema.
  • The scaler standardizes the ordered feature vector before inference.
  • The model returns a probability, which the backend maps to a human-readable risk level.
  • The frontend renders the prediction, guidance text, and high-impact risk factors.

System Architecture Preview

Mermaid Diagram Preview

πŸ“ Project Structure

Hart_prediction_backup/
|-- app.py
|-- requirements.txt
|-- logistic_regression_model.pkl
|-- standard_scaler.pkl
|-- templates/
|   `-- heart_disease_prediction.html
`-- images/
    |-- Primary logo.png
    |-- Wordmark logo.png
    |-- Favicon app icon.png
    `-- ...

πŸš€ Getting Started

1. Clone the project

git clone <your-repository-url>
cd Hart_prediction_backup

2. Create a virtual environment

python -m venv .venv

3. Activate the environment

.venv\Scripts\activate

4. Install dependencies

pip install -r requirements.txt

5. Run the app

python app.py

Open your browser at:

http://127.0.0.1:5001

βš™οΈ Environment Notes

The app supports these optional environment variables:

Variable Purpose Default
PORT Application port 5001
FLASK_HOST Bind host 0.0.0.0
FLASK_DEBUG Debug mode toggle disabled
LOG_LEVEL Logging verbosity INFO
CORS_ALLOWED_ORIGINS Allowed CORS origins *

πŸ§ͺ API Endpoints

GET /

Serves the main prediction interface.

GET /health

Returns service readiness and model status.

Example response:

{
  "status": "ok",
  "model_loaded": true,
  "model_features": 30
}

POST /predict

Accepts a JSON body containing the full feature set required by the model.

Example response:

{
  "status": "success",
  "prediction": 0.42,
  "risk_level": "High",
  "features_used": 30
}

🧾 Expected Input Highlights

The backend expects 30 numeric features, including:

  • Demographics: Age, Sex, Income
  • Clinical markers: Cholesterol, Heart Rate, BMI, Triglycerides
  • Blood pressure fields: Systolic_BP, Diastolic_BP, Pulse_Pressure
  • Lifestyle indicators: smoking, alcohol use, exercise, sleep, sedentary time
  • Condition flags: Diabetes, Hypertension, Metabolic_Risk
  • Geography flags: continent one-hot values

Validation also checks:

  • Diastolic_BP < Systolic_BP
  • Pulse_Pressure = Systolic_BP - Diastolic_BP
  • Sedentary_Ratio matches activity inputs
  • Only one continent flag is active

πŸ–ΌοΈ Screenshots

Application Screenshot

πŸ”’ Model Artifacts

This repository depends on:

  • logistic_regression_model.pkl
  • standard_scaler.pkl

If either file is missing or incompatible, the service will fail to start correctly and /health will report a degraded state.

πŸ“Œ Use Cases

  • Educational machine learning demos
  • Healthcare analytics portfolio projects
  • Clinical decision-support prototypes
  • Flask + scikit-learn deployment practice

⚠️ Disclaimer

This system is for screening and educational purposes only. It does not replace physician judgment, clinical testing, diagnosis, or emergency care.


❀️ Built for cardiovascular risk insight with Flask, ML, and a clean clinical UI.

About

AI-powered heart attack risk prediction web app built with Flask, Bootstrap, and logistic regression.

Topics

Resources

Stars

Watchers

Forks

Contributors