Delete app.py
Browse files
app.py
DELETED
|
@@ -1,244 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import pickle
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# Set paths
|
| 7 |
-
base_dir = os.path.dirname(__file__)
|
| 8 |
-
model_path = os.path.join(base_dir, 'Model', 'model.pkl')
|
| 9 |
-
scaler_path = os.path.join(base_dir, 'Model', 'scaler.pkl')
|
| 10 |
-
|
| 11 |
-
# Load the model and scaler
|
| 12 |
-
@st.cache_resource
|
| 13 |
-
def load_resources():
|
| 14 |
-
try:
|
| 15 |
-
if not os.path.exists(model_path) or not os.path.exists(scaler_path):
|
| 16 |
-
st.warning(f"Resources not found at {model_path} or {scaler_path}. Did you run the training pipeline?")
|
| 17 |
-
return None, None
|
| 18 |
-
|
| 19 |
-
with open(model_path, 'rb') as f:
|
| 20 |
-
model = pickle.load(f)
|
| 21 |
-
with open(scaler_path, 'rb') as f:
|
| 22 |
-
scaler = pickle.load(f)
|
| 23 |
-
return model, scaler
|
| 24 |
-
except Exception as e:
|
| 25 |
-
st.error(f"Error loading model or scaler: {e}")
|
| 26 |
-
return None, None
|
| 27 |
-
|
| 28 |
-
model, scaler = load_resources()
|
| 29 |
-
|
| 30 |
-
# Page configuration
|
| 31 |
-
st.set_page_config(
|
| 32 |
-
page_title="Cardiovascular Health Risk Predictor",
|
| 33 |
-
page_icon="❤️",
|
| 34 |
-
layout="wide"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Custom Styling (Rich Aesthetics)
|
| 38 |
-
st.markdown("""
|
| 39 |
-
<style>
|
| 40 |
-
.main {
|
| 41 |
-
background-color: #f8f9fa;
|
| 42 |
-
color: #2c3e50;
|
| 43 |
-
}
|
| 44 |
-
.stButton>button {
|
| 45 |
-
background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
|
| 46 |
-
color: white;
|
| 47 |
-
border-radius: 20px;
|
| 48 |
-
padding: 10px 40px;
|
| 49 |
-
font-weight: bold;
|
| 50 |
-
border: none;
|
| 51 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 52 |
-
transition: transform 0.2s, box-shadow 0.2s;
|
| 53 |
-
}
|
| 54 |
-
.stButton>button:hover {
|
| 55 |
-
transform: translateY(-2px);
|
| 56 |
-
box-shadow: 0 6px 10px rgba(0,0,0,0.2);
|
| 57 |
-
}
|
| 58 |
-
.metric-card {
|
| 59 |
-
background: white;
|
| 60 |
-
padding: 20px;
|
| 61 |
-
border-radius: 15px;
|
| 62 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.05);
|
| 63 |
-
margin-bottom: 20px;
|
| 64 |
-
border-left: 5px solid #e74c3c;
|
| 65 |
-
}
|
| 66 |
-
.section-header {
|
| 67 |
-
color: #c0392b;
|
| 68 |
-
font-weight: bold;
|
| 69 |
-
margin-top: 30px;
|
| 70 |
-
margin-bottom: 15px;
|
| 71 |
-
border-bottom: 2px solid #ecf0f1;
|
| 72 |
-
padding-bottom: 5px;
|
| 73 |
-
}
|
| 74 |
-
</style>
|
| 75 |
-
""", unsafe_allow_html=True)
|
| 76 |
-
|
| 77 |
-
# Main Header
|
| 78 |
-
st.title("❤️ Cardiovascular Health Risk Predictor")
|
| 79 |
-
st.markdown("---")
|
| 80 |
-
st.info("💡 **Welcome!** Please provide your details below to estimate your cardiovascular health risk.")
|
| 81 |
-
|
| 82 |
-
# Input Layout
|
| 83 |
-
col1, col2 = st.columns(2)
|
| 84 |
-
|
| 85 |
-
with col1:
|
| 86 |
-
st.markdown("<div class='section-header'>👤 Personal Information</div>", unsafe_allow_html=True)
|
| 87 |
-
# age in training is (age/365).astype(int), so we take years directly
|
| 88 |
-
age = st.number_input("Age (Years)", min_value=1, max_value=120, value=50, help="Your age in years.")
|
| 89 |
-
gender = st.selectbox("Gender", options=["Female", "Male"], help="Select your gender.")
|
| 90 |
-
height = st.number_input("Height (cm)", min_value=1, max_value=250, value=170, help="Your height in centimeters.")
|
| 91 |
-
weight = st.number_input("Weight (kg)", min_value=1.0, max_value=500.0, value=75.0, help="Your weight in kilograms.")
|
| 92 |
-
|
| 93 |
-
with col2:
|
| 94 |
-
st.markdown("<div class='section-header'>🔬 Clinical Measurements</div>", unsafe_allow_html=True)
|
| 95 |
-
systolic_bp = st.number_input("Systolic Blood Pressure (mmHg)", min_value=50, max_value=250, value=120)
|
| 96 |
-
diastolic_bp = st.number_input("Diastolic Blood Pressure (mmHg)", min_value=30, max_value=150, value=80)
|
| 97 |
-
cholesterol = st.selectbox("Cholesterol Level",
|
| 98 |
-
options=["Normal", "Above Normal", "Well Above Normal"],
|
| 99 |
-
index=0)
|
| 100 |
-
gluc = st.selectbox("Glucose Level",
|
| 101 |
-
options=["Normal", "Above Normal", "Well Above Normal"],
|
| 102 |
-
index=0)
|
| 103 |
-
|
| 104 |
-
st.markdown("<div class='section-header'>🚭 Lifestyle Habits</div>", unsafe_allow_html=True)
|
| 105 |
-
l_col1, l_col2, l_col3 = st.columns(3)
|
| 106 |
-
|
| 107 |
-
with l_col1:
|
| 108 |
-
smoke = st.checkbox("Do you smoke?")
|
| 109 |
-
with l_col2:
|
| 110 |
-
alco = st.checkbox("Do you consume alcohol?")
|
| 111 |
-
with l_col3:
|
| 112 |
-
active = st.checkbox("Are you physically active?")
|
| 113 |
-
|
| 114 |
-
# Preprocessing function
|
| 115 |
-
def preprocess_input(age, gender, height, weight, systolic_bp, diastolic_bp, cholesterol, gluc, smoke, alco, active):
|
| 116 |
-
# Mapping
|
| 117 |
-
# In training: df.gender = df.gender.replace({1: 0, 2: 1})
|
| 118 |
-
# where 1 was female and 2 was male. So Female=0, Male=1.
|
| 119 |
-
gender_mapped = 1 if gender == "Male" else 0
|
| 120 |
-
cholesterol_mapped = {"Normal": 1, "Above Normal": 2, "Well Above Normal": 3}[cholesterol]
|
| 121 |
-
gluc_mapped = {"Normal": 1, "Above Normal": 2, "Well Above Normal": 3}[gluc]
|
| 122 |
-
|
| 123 |
-
# Feature Engineering
|
| 124 |
-
bmi = weight / ((height / 100) ** 2)
|
| 125 |
-
pulse_pressure = systolic_bp - diastolic_bp
|
| 126 |
-
|
| 127 |
-
# Create DataFrame
|
| 128 |
-
data = pd.DataFrame({
|
| 129 |
-
'age': [age],
|
| 130 |
-
'gender': [gender_mapped],
|
| 131 |
-
'height': [height],
|
| 132 |
-
'weight': [weight],
|
| 133 |
-
'systolic_bp': [systolic_bp],
|
| 134 |
-
'diastolic_bp': [diastolic_bp],
|
| 135 |
-
'cholesterol': [cholesterol_mapped],
|
| 136 |
-
'gluc': [gluc_mapped],
|
| 137 |
-
'smoke': [1 if smoke else 0],
|
| 138 |
-
'alco': [1 if alco else 0],
|
| 139 |
-
'active': [1 if active else 0],
|
| 140 |
-
'bmi': [bmi],
|
| 141 |
-
'pulse_pressure': [pulse_pressure]
|
| 142 |
-
})
|
| 143 |
-
|
| 144 |
-
# Reorder columns to match training data order exactly
|
| 145 |
-
# Derived from PrepareData and SplitData logic
|
| 146 |
-
cols_order = ['age', 'gender', 'height', 'weight', 'systolic_bp', 'diastolic_bp',
|
| 147 |
-
'cholesterol', 'gluc', 'smoke', 'alco', 'active', 'bmi', 'pulse_pressure']
|
| 148 |
-
data = data[cols_order]
|
| 149 |
-
|
| 150 |
-
# Scaling
|
| 151 |
-
# These are the columns the scaler was fitted on in TrainableData.scalling()
|
| 152 |
-
scl_col = ['age', 'height', 'weight', 'systolic_bp', 'diastolic_bp', 'bmi', 'pulse_pressure']
|
| 153 |
-
data[scl_col] = scaler.transform(data[scl_col])
|
| 154 |
-
|
| 155 |
-
return data
|
| 156 |
-
|
| 157 |
-
# Predict Button
|
| 158 |
-
st.markdown("---")
|
| 159 |
-
if st.button("Calculate Health Risk Score"):
|
| 160 |
-
if model and scaler:
|
| 161 |
-
with st.spinner("Analyzing your data..."):
|
| 162 |
-
# Preprocess and Predict
|
| 163 |
-
processed_data = preprocess_input(age, gender, height, weight, systolic_bp, diastolic_bp, cholesterol, gluc, smoke, alco, active)
|
| 164 |
-
prediction = model.predict(processed_data)[0]
|
| 165 |
-
probability = model.predict_proba(processed_data)[0][1]
|
| 166 |
-
|
| 167 |
-
# Results Section
|
| 168 |
-
st.markdown("<div class='section-header'>📊 Analysis Result</div>", unsafe_allow_html=True)
|
| 169 |
-
|
| 170 |
-
res_col1, res_col2 = st.columns([1, 2])
|
| 171 |
-
|
| 172 |
-
with res_col1:
|
| 173 |
-
# Gauge representation or simple metrics
|
| 174 |
-
st.metric("Risk probability", f"{probability*100:.1f}%")
|
| 175 |
-
|
| 176 |
-
with res_col2:
|
| 177 |
-
if prediction == 1:
|
| 178 |
-
st.error("⚠️ **Risk Identified**: Based on the provided data, the model indicates a higher risk of cardiovascular issues.")
|
| 179 |
-
st.warning("We recommend consulting with a healthcare professional for a detailed evaluation.")
|
| 180 |
-
else:
|
| 181 |
-
st.success("✅ **Low Risk**: Based on the provided data, the model predicts a low risk of cardiovascular issues.")
|
| 182 |
-
st.info("Maintain a healthy lifestyle with regular exercise and a balanced diet.")
|
| 183 |
-
|
| 184 |
-
# Display individual metrics in cards
|
| 185 |
-
st.markdown("<div class='section-header'>📋 Your Bio-Metrics Overview</div>", unsafe_allow_html=True)
|
| 186 |
-
|
| 187 |
-
# Categories for BMI
|
| 188 |
-
bmi_val = weight / ((height / 100) ** 2)
|
| 189 |
-
if bmi_val < 18.5: bmi_cat, bmi_color = "Underweight", "#3498db"
|
| 190 |
-
elif 18.5 <= bmi_val < 25: bmi_cat, bmi_color = "Normal", "#27ae60"
|
| 191 |
-
elif 25 <= bmi_val < 30: bmi_cat, bmi_color = "Overweight", "#f39c12"
|
| 192 |
-
else: bmi_cat, bmi_color = "Obese", "#e74c3c"
|
| 193 |
-
|
| 194 |
-
# Categories for Blood Pressure
|
| 195 |
-
if systolic_bp < 120 and diastolic_bp < 80: bp_cat, bp_color = "Normal", "#27ae60"
|
| 196 |
-
elif 120 <= systolic_bp < 130 and diastolic_bp < 80: bp_cat, bp_color = "Elevated", "#f1c40f"
|
| 197 |
-
elif (130 <= systolic_bp < 140) or (80 <= diastolic_bp < 90): bp_cat, bp_color = "Hypertension Stage 1", "#f39c12"
|
| 198 |
-
else: bp_cat, bp_color = "Hypertension Stage 2", "#e74c3c"
|
| 199 |
-
|
| 200 |
-
m_col1, m_col2, m_col3 = st.columns(3)
|
| 201 |
-
|
| 202 |
-
with m_col1:
|
| 203 |
-
st.markdown(f"""
|
| 204 |
-
<div class='metric-card' style='border-left-color: {bmi_color}'>
|
| 205 |
-
<div style='font-size: 0.9em; color: #7f8c8d;'>Body Mass Index (BMI)</div>
|
| 206 |
-
<div style='font-size: 1.8em; color: #2c3e50; font-weight: bold;'>{bmi_val:.1f}</div>
|
| 207 |
-
<div style='color: {bmi_color}; font-weight: bold;'>{bmi_cat}</div>
|
| 208 |
-
</div>
|
| 209 |
-
""", unsafe_allow_html=True)
|
| 210 |
-
|
| 211 |
-
with m_col2:
|
| 212 |
-
st.markdown(f"""
|
| 213 |
-
<div class='metric-card' style='border-left-color: {bp_color}'>
|
| 214 |
-
<div style='font-size: 0.9em; color: #7f8c8d;'>Blood Pressure</div>
|
| 215 |
-
<div style='font-size: 1.8em; color: #2c3e50; font-weight: bold;'>{systolic_bp}/{diastolic_bp}</div>
|
| 216 |
-
<div style='color: {bp_color}; font-weight: bold;'>{bp_cat}</div>
|
| 217 |
-
</div>
|
| 218 |
-
""", unsafe_allow_html=True)
|
| 219 |
-
|
| 220 |
-
with m_col3:
|
| 221 |
-
pp_val = systolic_bp - diastolic_bp
|
| 222 |
-
st.markdown(f"""
|
| 223 |
-
<div class='metric-card' style='border-left-color: #9b59b6'>
|
| 224 |
-
<div style='font-size: 0.9em; color: #7f8c8d;'>Pulse Pressure</div>
|
| 225 |
-
<div style='font-size: 1.8em; color: #2c3e50; font-weight: bold;'>{pp_val} mmHg</div>
|
| 226 |
-
<div style='color: #9b59b6;'>Typical range: 40-60</div>
|
| 227 |
-
</div>
|
| 228 |
-
""", unsafe_allow_html=True)
|
| 229 |
-
|
| 230 |
-
# Lifestyle Summary
|
| 231 |
-
st.markdown("### ���� Lifestyle Indicators")
|
| 232 |
-
l_sum1, l_sum2, l_sum3 = st.columns(3)
|
| 233 |
-
with l_sum1:
|
| 234 |
-
st.write(f"**Smoking:** {'🚭 No' if not smoke else '🚬 Yes'}")
|
| 235 |
-
with l_sum2:
|
| 236 |
-
st.write(f"**Alcohol:** {'🍷 No' if not alco else '🍺 Yes'}")
|
| 237 |
-
with l_sum3:
|
| 238 |
-
st.write(f"**Active:** {'🏃 Yes' if active else '🛋️ No'}")
|
| 239 |
-
else:
|
| 240 |
-
st.error("Resources (model/scaler) missing or failed to load. Please run 'python src/pipeline/train_model.py' first.")
|
| 241 |
-
|
| 242 |
-
# Footer
|
| 243 |
-
st.markdown("---")
|
| 244 |
-
st.caption("⚠️ **Disclaimer**: This tool uses a machine learning model for informational purposes only. It is NOT a substitute for professional medical advice, diagnosis, or treatment.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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