The Heart Disease Prediction Web Application helps users predict their risk of heart disease using machine learning models. The app is built using Flask, SQLAlchemy, Bcrypt, and is deployed with Docker. It offers a user-friendly interface for inputting health data, viewing prediction results, and managing profiles.
Access the live version: http://188.121.99.111:5000
-
User Registration and Authentication
- Secure registration and login functionality.
- Password hashing for added security using Bcrypt.
-
User Profile and Prediction History
- View personalized prediction history.
- Option to download past predictions as reports.
-
Real-Time Predictions
- Immediate prediction results based on user-inputted health data.
-
Responsive Design
- Optimized for both desktop and mobile users.
-
Error Handling
- Clear error messages and feedback for incorrect inputs.
- Flash messages for successful actions like registration and login.
-
Data Security and Privacy
- Secure storage of user data using an SQLite database.
- Encrypted passwords for user accounts.
The project's goal is to collaboratively develop a web application that:
- Utilizes machine learning for heart disease prediction.
- Implements user registration and authentication systems using a database.
- Containerizes the application with Docker and deploys it using Docker Swarm on a cloud-based host for scalability.
Heart Disease Prediction Web Application
β βββ assets
β β βββ dataset
β β βββ images
β β βββ heart_disease_diagnosis_model.ipynb
β β βββ heart_disease_diagnosis_model.py
β βββ instance
β β βββ Heart_Disease_Diagnosis.db
β βββ static
β β βββ css
β β β βββ style.css
β β βββ images
β β β βββ Error_401.webp
β β β βββ Error_403.webp
β β β βββ Error_404.webp
β β β βββ Error_500.webp
β β β βββ Heart_Disease.webp
β β β βββ No_Disease.webp
β β β βββ Logo.webp
β β β βββ smart_app.webp
β βββ templates/
β β βββ base_error.html
β β βββ base.html
β β βββ error_401.html
β β βββ error_403.html
β β βββ error_404.html
β β βββ error_500.html
β β βββ history.html
β β βββ home.html
β β βββ input.html
β β βββ login.html
β β βββ profile.html
β β βββ register.html
β β βββ result.html
β βββ .gitignore
β βββ app.py
β βββ form.py
β βββ heart_disease_diagnosis_model.pkl
β βββ members.txt
β βββ model.py
β βββ README.md
β βββ requirements.txt
β βββ Dockerfile
βββ docker-compose.yml
The dataset used for training the model is sourced from Kaggle's Heart Disease Dataset. It includes 303 observations with 14 features, such as:
- Age: Patient's age
- Sex: Gender (1 = Male, 0 = Female)
- Chest Pain Type:
- 0 = Typical angina
- 1 = Atypical angina
- 2 = Non-anginal pain
- 3 = Asymptomatic
- Resting Blood Pressure: Measurement in mm Hg
- Cholesterol: Serum cholesterol in mg/dl
- Fasting Blood Sugar: >120 mg/dl (1 = True, 0 = False)
- Resting ECG: Electrocardiographic results
- Max Heart Rate: Maximum heart rate achieved
- Exercise-Induced Angina: (1 = Yes, 0 = No)
- Old Peak: ST depression induced by exercise relative to rest
- ST Slope: Slope of the peak exercise ST segment
- Number of Major Vessels: (0-3) colored by fluoroscopy
- Thallium Stress Test Results: (1 = Normal, 2 = Fixed Defect, 3 = Reversible Defect)
- Target: Presence of heart disease (1 = Disease, 0 = No Disease)
- Renamed columns for better readability.
- Categorical Mapping for variables:
- Sex mapped to "Male" or "Female".
- Chest Pain categorized into descriptive labels.
- Resting ECG, Exercise-Induced Angina, and Thallium levels mapped to categorical descriptions.
- Scaling: StandardScaler was applied to normalize features.
- Univariate Analysis: Histograms and box plots for distribution analysis.
- Insights:
- Most patients were aged around 54.
- Majority had typical chest pain.
- Significant correlation found between chest pain type and heart disease presence.
We trained a K-Nearest Neighbors (KNN) classifier using scikit-learn. This simple yet effective model classifies patients based on their proximity to similar cases in the dataset.KNN is a simple, yet powerful algorithm that classifies data points based on their proximity to the nearest neighbors in the feature space.
- Data Splitting: 80% training, 20% testing.
- Hyperparameter Tuning: Used GridSearchCV to optimize the number of neighbors.
- Model Accuracy: Achieved an accuracy of ~85%.
- Confusion Matrix: Used to visualize the model's performance.
- Classification Report: Displays precision, recall, and F1-score for both classes (Heart Disease vs. No Disease).
- Model Saving: The trained model was saved using
pickle
for deployment.
-
Users Table
- Stores user information and authentication details.
- Fields:
id
: Unique identifier (Primary Key)username
: Unique usernameemail
: Unique email addresspassword_hash
: Hashed password
- Relationship: One-to-many relationship with
prediction_history
.
-
PredictionHistory Table
- Stores predictions made by each user.
- Fields:
id
: Unique identifier (Primary Key)user_id
: Foreign key referencing theusers
tableage
,sex
,chest_pain
,resting_blood_pressure
,cholesterol
, etc.: User inputs for predictionsresult
: Prediction outcome ("Positive" or "Negative")timestamp
: Time when the prediction was made
# User Model
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
predictions = db.relationship('PredictionHistory', backref='user', lazy=True)
def set_password(self, password):
self.password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
def check_password(self, password):
return bcrypt.check_password_hash(self.password_hash, password)
# PredictionHistory Model
class PredictionHistory(db.Model):
__tablename__ = 'prediction_history'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
age = db.Column(db.Integer, nullable=False)
sex = db.Column(db.String, nullable=False)
chest_pain = db.Column(db.String, nullable=False)
# Other fields here...
result = db.Column(db.String, nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
- Python 3.x
- Flask
- SQLAlchemy
- Flask-Login
- Bcrypt
- Scikit-learn
- Docker
- Docker Compose
- Python 3.x
- Docker and Docker Compose
git clone https://github.com/Ulduzpp/Smart_Web_Nexus.git
cd Smart_Web_Nexus
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py
- Open your browser and navigate to: http://127.0.0.1:5000
- Build the Docker Image
docker build -t smart_web_nexus .
- Run the Docker Container
docker run -p 5000:5000 smart_web_nexus
Role | Name |
---|---|
Frontend Developer | Fatemeh Habibimoghaddam |
Backend Developer | Ouldouz Pakpour |
Database Engineer | Sahar Abdi |
Machine Learning Engineer | Hanie Fazli |
Docker & MLOps Engineers | Touba Derakhshandeh & Hanie Fazli |
We welcome contributions to improve this project! Please fork the repository and submit a pull request.