Spaces:
Sleeping
Sleeping
| 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!") | |