Harika22's picture
Update app.py
a2240a2 verified
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import streamlit as st
import pickle
from scipy import stats
from sklearn.impute import KNNImputer
from scipy.stats import chi2_contingency
from sklearn.model_selection import train_test_split, cross_validate,StratifiedKFold
from imblearn.over_sampling import SMOTE
import optuna
from sklearn.preprocessing import StandardScaler, OneHotEncoder, OrdinalEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from imblearn.pipeline import Pipeline as ImbPipeline
from optuna.samplers import TPESampler
from optuna.visualization import plot_param_importances,plot_optimization_history
from sklearn.tree import DecisionTreeClassifier
# Load Model
model = pickle.load(open("final_pipeline.pkl", "rb"))
st.set_page_config(page_title="Heart Disease Risk Assessment", page_icon="πŸ«€", layout="wide")
st.markdown("<h1 style='text-align: center;'>πŸ«€ Heart Disease Risk Assessment πŸ₯</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center;'>Use this tool to assess your risk of developing heart disease in the next 10 years based on health metrics and medical history.</p>", unsafe_allow_html=True)
st.sidebar.title("πŸ” Navigation")
page = st.sidebar.radio("Go to", ["🏠 Home", "πŸ“Š Predict"])
if page == "🏠 Home":
st.write("### Welcome to the Heart Risk Prediction Tool πŸ₯")
st.markdown("This tool helps predict the likelihood of developing heart disease based on your health metrics and medical history.")
st.markdown("Navigate to the **Predict** section to enter your details and get a risk assessment.")
elif page == "πŸ“Š Predict":
st.sidebar.header("πŸ“ Enter Your Health Details")
# Demographic Factors
st.sidebar.markdown("<h3 style='color:blue;'>πŸ‘€ Demographic Factors</h3>", unsafe_allow_html=True)
age = st.sidebar.number_input("Age", 1, 120, 30)
education = st.sidebar.selectbox("Education Level", [1, 2, 3, 4])
sex = st.sidebar.radio("Sex", ["Male", "Female"], horizontal=True)
# Lifestyle Factors
st.sidebar.markdown("<h3 style='color:green;'>🚬 Lifestyle Factors</h3>", unsafe_allow_html=True)
is_smoking = st.sidebar.radio("Do you smoke?", ["Yes", "No"], horizontal=True)
if is_smoking == "Yes":
cigs_per_day = st.sidebar.slider("Cigarettes Per Day", 0, 100, 0)
else:
cigs_per_day = 0
# Medical History
st.sidebar.markdown("<h3 style='color:purple;'>🩺 Medical History</h3>", unsafe_allow_html=True)
bp_meds = st.sidebar.radio("Blood Pressure Medication", ["Yes", "No"], horizontal=True)
prevalent_stroke = st.sidebar.radio("Had a stroke?", ["Yes", "No"], horizontal=True)
prevalent_hyp = st.sidebar.radio("Hypertension?", ["Yes", "No"], horizontal=True)
diabetes = st.sidebar.radio("Diabetes?", ["Yes", "No"], horizontal=True)
# Health Measurements
st.sidebar.markdown("<h3 style='color:red;'>πŸ“Š Health Measurements</h3>", unsafe_allow_html=True)
total_cholesterol = st.sidebar.number_input("Total Cholesterol (mg/dL) (125-200 Normal)", 100.0, 400.0, 200.0)
systolic_bp = st.sidebar.slider("Systolic BP (mmHg) (90-120 Normal)", 50.0, 250.0, 120.0)
diastolic_bp = st.sidebar.slider("Diastolic BP (mmHg) (60-80 Normal)", 30.0, 150.0, 80.0)
bmi = st.sidebar.number_input("BMI (18.5-24.9 Normal)", 10.0, 50.0, 25.0)
heart_rate = st.sidebar.slider("Heart Rate (bpm) (60-100 Normal)", 30.0, 200.0, 70.0)
glucose = st.sidebar.number_input("Glucose (mg/dL) (70-140 Normal)", 50.0, 300.0, 90.0)
if st.sidebar.button("πŸ” Predict Heart Risk"):
prediction = model.predict([[age, education, sex, is_smoking, cigs_per_day, bp_meds, prevalent_stroke,
prevalent_hyp, diabetes, total_cholesterol, systolic_bp, diastolic_bp,
bmi, heart_rate, glucose]])
result = prediction[0]
st.subheader("🩺 Prediction Result πŸ₯")
if result == 1:
st.error("⚠️ **High Risk:** You have a higher chance of developing Heart Disease in the next 10 years. Please consult a doctor.")
else:
st.success("βœ… **Low Risk:** Your predicted risk of developing Heart Disease in the next 10 years is low. Keep maintaining a healthy lifestyle!")