Commit ยท
4e763b8
1
Parent(s): 201edb7
add all file
Browse files- Final Model.pkl +3 -0
- app.py +189 -0
- columns.pkl +3 -0
- requirements.txt +8 -0
- scaler.pkl +3 -0
Final Model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:730ddac99ed50df8bb7a95c207a1df019c3855f9067bbd26ef32b4ea656506bc
|
| 3 |
+
size 139576
|
app.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from joblib import load
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
# Get current directory where model files are located
|
| 9 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
+
|
| 11 |
+
# Set page config
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Healthcare Stroke Prediction System",
|
| 14 |
+
page_icon="โ๏ธ",
|
| 15 |
+
layout="wide",
|
| 16 |
+
initial_sidebar_state="expanded"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Custom CSS
|
| 20 |
+
st.markdown("""
|
| 21 |
+
<style>
|
| 22 |
+
.main {
|
| 23 |
+
padding-top: 2rem;
|
| 24 |
+
}
|
| 25 |
+
.prediction-box {
|
| 26 |
+
padding: 2rem;
|
| 27 |
+
border-radius: 10px;
|
| 28 |
+
margin-top: 2rem;
|
| 29 |
+
}
|
| 30 |
+
.high-risk {
|
| 31 |
+
background-color: #ffebee;
|
| 32 |
+
border-left: 4px solid #f44336;
|
| 33 |
+
}
|
| 34 |
+
.low-risk {
|
| 35 |
+
background-color: #e8f5e9;
|
| 36 |
+
border-left: 4px solid #4caf50;
|
| 37 |
+
}
|
| 38 |
+
</style>
|
| 39 |
+
""", unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
# Title and header
|
| 42 |
+
st.title("โ๏ธ Stroke Prediction System")
|
| 43 |
+
st.markdown("---")
|
| 44 |
+
st.markdown("**Predict stroke risk based on patient health data using Machine Learning**")
|
| 45 |
+
|
| 46 |
+
# Load model and preprocessing objects
|
| 47 |
+
try:
|
| 48 |
+
model = load("Final Model.pkl")
|
| 49 |
+
scaler = load("scaler.pkl")
|
| 50 |
+
columns = load("columns.pkl")
|
| 51 |
+
except FileNotFoundError:
|
| 52 |
+
st.error("โ Model files not found. Please ensure 'Final Model.pkl', 'scaler.pkl', and 'columns.pkl' are in the parent directory.")
|
| 53 |
+
st.stop()
|
| 54 |
+
|
| 55 |
+
# Create two columns for input
|
| 56 |
+
col1, col2 = st.columns(2)
|
| 57 |
+
|
| 58 |
+
with col1:
|
| 59 |
+
st.subheader("๐ค Personal Information")
|
| 60 |
+
age = st.slider("Age", min_value=18, max_value=100, value=45, step=1)
|
| 61 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 62 |
+
ever_married = st.selectbox("Ever Married", ["No", "Yes"])
|
| 63 |
+
|
| 64 |
+
st.subheader("๐ผ Work & Residence")
|
| 65 |
+
work_type = st.selectbox("Work Type", ["Private", "Self-employed", "Govt_job", "Never_worked", "children"])
|
| 66 |
+
residence_type = st.selectbox("Residence Type", ["Urban", "Rural"])
|
| 67 |
+
|
| 68 |
+
with col2:
|
| 69 |
+
st.subheader("๐ฅ Health Metrics")
|
| 70 |
+
avg_glucose_level = st.number_input("Average Glucose Level (mg/dL)", min_value=50.0, max_value=300.0, value=120.0, step=1.0)
|
| 71 |
+
bmi = st.number_input("Body Mass Index (BMI)", min_value=10.0, max_value=60.0, value=25.0, step=0.1)
|
| 72 |
+
hypertension = st.selectbox("Hypertension", ["No", "Yes"])
|
| 73 |
+
heart_disease = st.selectbox("Heart Disease", ["No", "Yes"])
|
| 74 |
+
|
| 75 |
+
st.subheader("๐ฌ Lifestyle")
|
| 76 |
+
smoking_status = st.selectbox("Smoking Status", ["never smoked", "formerly smoked", "smokes", "Unknown"])
|
| 77 |
+
|
| 78 |
+
# Encode categorical variables
|
| 79 |
+
gender_map = {"Female": 0, "Male": 1, "Other": 2}
|
| 80 |
+
work_map = {"Private": 0, "Self-employed": 1, "Govt_job": 2, "Never_worked": 3, "children": 4}
|
| 81 |
+
residence_map = {"Rural": 0, "Urban": 1}
|
| 82 |
+
smoking_map = {"never smoked": 0, "formerly smoked": 1, "smokes": 2, "Unknown": 3}
|
| 83 |
+
married_map = {"No": 0, "Yes": 1}
|
| 84 |
+
condition_map = {"No": 0, "Yes": 1}
|
| 85 |
+
|
| 86 |
+
# Prepare for scaling (scale only the numeric features)
|
| 87 |
+
numeric_features = np.array([[age, avg_glucose_level, bmi]])
|
| 88 |
+
scaled_features = scaler.transform(numeric_features)[0]
|
| 89 |
+
|
| 90 |
+
# Create final input for model (in correct order: gender, age, hypertension, heart_disease, ever_married, work_type, Residence_type, avg_glucose_level, bmi, smoking_status)
|
| 91 |
+
final_input = np.array([
|
| 92 |
+
gender_map[gender],
|
| 93 |
+
scaled_features[0], # scaled age
|
| 94 |
+
condition_map[hypertension],
|
| 95 |
+
condition_map[heart_disease],
|
| 96 |
+
married_map[ever_married],
|
| 97 |
+
work_map[work_type],
|
| 98 |
+
residence_map[residence_type],
|
| 99 |
+
scaled_features[1], # scaled avg_glucose_level
|
| 100 |
+
scaled_features[2], # scaled bmi
|
| 101 |
+
smoking_map[smoking_status]
|
| 102 |
+
]).reshape(1, -1)
|
| 103 |
+
|
| 104 |
+
# Prediction button and results
|
| 105 |
+
st.markdown("---")
|
| 106 |
+
col_btn1, col_btn2, col_btn3 = st.columns([1, 1, 2])
|
| 107 |
+
|
| 108 |
+
with col_btn1:
|
| 109 |
+
if st.button("๐ฎ Predict Stroke Risk", use_container_width=True):
|
| 110 |
+
# Make prediction
|
| 111 |
+
prediction = model.predict(final_input)[0]
|
| 112 |
+
probability = model.predict_proba(final_input)[0]
|
| 113 |
+
|
| 114 |
+
# Store in session state
|
| 115 |
+
st.session_state.prediction = prediction
|
| 116 |
+
st.session_state.probability = probability
|
| 117 |
+
|
| 118 |
+
with col_btn2:
|
| 119 |
+
if st.button("๐ Reset Form", use_container_width=True):
|
| 120 |
+
st.rerun()
|
| 121 |
+
|
| 122 |
+
# Display results
|
| 123 |
+
if "prediction" in st.session_state:
|
| 124 |
+
prediction = st.session_state.prediction
|
| 125 |
+
probability = st.session_state.probability
|
| 126 |
+
|
| 127 |
+
st.markdown("---")
|
| 128 |
+
st.subheader("๐ Prediction Results")
|
| 129 |
+
|
| 130 |
+
# Create result display
|
| 131 |
+
if prediction == 1:
|
| 132 |
+
risk_level = "HIGH RISK"
|
| 133 |
+
risk_class = "high-risk"
|
| 134 |
+
risk_color = "๐ด"
|
| 135 |
+
recommendation = "โ ๏ธ **Please consult with a healthcare professional immediately for further evaluation and preventive measures.**"
|
| 136 |
+
else:
|
| 137 |
+
risk_level = "LOW RISK"
|
| 138 |
+
risk_class = "low-risk"
|
| 139 |
+
risk_color = "๐ข"
|
| 140 |
+
recommendation = "โ
**Continue maintaining healthy lifestyle habits. Regular check-ups are recommended.**"
|
| 141 |
+
|
| 142 |
+
# Display prediction box
|
| 143 |
+
col_result1, col_result2 = st.columns(2)
|
| 144 |
+
|
| 145 |
+
with col_result1:
|
| 146 |
+
st.markdown(f"""
|
| 147 |
+
<div class="prediction-box {risk_class}">
|
| 148 |
+
<h2>{risk_color} {risk_level}</h2>
|
| 149 |
+
<p><strong>Stroke Risk Probability:</strong></p>
|
| 150 |
+
<h3>{probability[1]*100:.2f}%</h3>
|
| 151 |
+
</div>
|
| 152 |
+
""", unsafe_allow_html=True)
|
| 153 |
+
|
| 154 |
+
with col_result2:
|
| 155 |
+
st.markdown(f"""
|
| 156 |
+
<div class="prediction-box {risk_class}">
|
| 157 |
+
<h4>Recommendation</h4>
|
| 158 |
+
<p>{recommendation}</p>
|
| 159 |
+
</div>
|
| 160 |
+
""", unsafe_allow_html=True)
|
| 161 |
+
|
| 162 |
+
# Detailed breakdown
|
| 163 |
+
st.subheader("๐ Probability Breakdown")
|
| 164 |
+
col1, col2 = st.columns(2)
|
| 165 |
+
|
| 166 |
+
with col1:
|
| 167 |
+
st.metric("Low Risk Probability", f"{probability[0]*100:.2f}%")
|
| 168 |
+
with col2:
|
| 169 |
+
st.metric("High Risk Probability", f"{probability[1]*100:.2f}%")
|
| 170 |
+
|
| 171 |
+
# Risk factors summary
|
| 172 |
+
st.subheader("๐ Patient Summary")
|
| 173 |
+
summary_df = pd.DataFrame({
|
| 174 |
+
"Parameter": ["Age", "Gender", "Average Glucose Level", "BMI", "Work Type", "Smoking Status", "Marital Status", "Residence Type"],
|
| 175 |
+
"Value": [age, gender, f"{avg_glucose_level} mg/dL", f"{bmi}", work_type, smoking_status, ever_married, residence_type]
|
| 176 |
+
})
|
| 177 |
+
st.table(summary_df)
|
| 178 |
+
|
| 179 |
+
# Footer
|
| 180 |
+
st.markdown("---")
|
| 181 |
+
st.markdown("""
|
| 182 |
+
<div style="text-align: center; color: gray; font-size: 0.85rem;">
|
| 183 |
+
<p>โ๏ธ <strong>Disclaimer:</strong> This is an AI-based prediction tool for educational and awareness purposes only.
|
| 184 |
+
It should not be used as a substitute for professional medical advice. Always consult a healthcare provider for medical decisions.</p>
|
| 185 |
+
<p>Model: Gradient Boosting Classifier | Data: Healthcare Stroke Dataset</p>
|
| 186 |
+
</div>
|
| 187 |
+
""", unsafe_allow_html=True)
|
| 188 |
+
|
| 189 |
+
|
columns.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cfcf569aab73925d6a434b7e32fcb4f2a80ceaecee556571cabd2b6a1a562067
|
| 3 |
+
size 149
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.22.0
|
| 2 |
+
pandas>=1.4.0
|
| 3 |
+
scikit-learn>=1.0.0
|
| 4 |
+
xgboost>=1.5.0
|
| 5 |
+
matplotlib>=3.5.0
|
| 6 |
+
seaborn>=0.11.0
|
| 7 |
+
streamlit>=1.0.0
|
| 8 |
+
joblib
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba916a69082b99174c0881a039cb83abe8d88520c06047ff3a3384cdc2803c71
|
| 3 |
+
size 959
|