File size: 3,810 Bytes
09ab4c6
 
8d00c57
75cdc52
12775b3
f78a1f4
 
 
 
 
 
 
75cdc52
12775b3
8ae0ee8
 
 
8f31913
 
 
 
8ae0ee8
8f31913
 
8ae0ee8
 
 
8f31913
8ae0ee8
8f31913
8ae0ee8
ece0334
8ae0ee8
8f31913
8ae0ee8
 
8f31913
 
 
 
 
 
 
8ae0ee8
 
 
ece0334
8ae0ee8
 
ece0334
 
8ae0ee8
8f31913
 
 
 
 
8ae0ee8
 
8f31913
8ae0ee8
ece0334
8f31913
ece0334
 
8f31913
8ae0ee8
 
 
 
 
 
12775b3
8f31913
9aa4251
12775b3
ece0334
 
 
9aa4251
ece0334
 
 
8f31913
ece0334
 
 
 
 
 
 
 
 
 
 
 
 
 
75cdc52
ece0334
 
 
 
 
 
 
8ae0ee8
ece0334
 
 
 
 
598873d
ece0334
 
 
 
 
598873d
ece0334
 
598873d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
import numpy as np
import pickle


try:
    with open("final_model.pkl", "rb") as f:
        model = pickle.load(f)
    st.success("βœ… Model loaded successfully!")
except FileNotFoundError:
    st.error("❌ Model file not found! Please upload `final_model.pkl`.")
    model = None


st.markdown(
    """
    <style>
        body {
            background-color: #121212;
            color: #FFFFFF;
        }
        .stApp {
            background-color: #121212;
            color: #FFFFFF;
        }
        .title {
            text-align: center;
            font-size: 36px;
            font-weight: bold;
            color: #BB86FC;
        }
        .stButton > button {
            width: 100%;
            background-color: #6200EE;
            color: white;
            font-size: 18px;
            border-radius: 8px;
            padding: 10px;
            transition: 0.3s ease-in-out;
        }
        .stButton > button:hover {
            background-color: #3700B3;
            transform: scale(1.05);
        }
        .result-box {
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            color: white;
            padding: 12px;
            border-radius: 8px;
            margin-top: 20px;
            box-shadow: 0px 4px 8px rgba(255, 255, 255, 0.2);
            transition: 0.3s ease-in-out;
        }
        .result-box:hover {
            transform: scale(1.05);
        }
        .high-risk {
            background-color: #D32F2F;
        }
        .low-risk {
            background-color: #388E3C;
        }
        .probability {
            background-color: #FFA726;
        }
    </style>
    """,
    unsafe_allow_html=True,
)


st.markdown("<h1 class='title'>πŸ’œ Heart Disease Risk Predictor</h1>", unsafe_allow_html=True)


with st.expander("πŸ”Ή **Personal Information**", expanded=True):
    Age = st.slider("Age", 18, 80, 40)
    Sex = st.selectbox("Sex", ["Male", "Female"])

with st.expander("πŸ”Ή **Medical Information**", expanded=True):
    RestingBP = st.number_input("Resting Blood Pressure", min_value=80, max_value=200, value=120)
    Cholesterol = st.number_input("Cholesterol Level", min_value=50, max_value=400, value=200)
    FastingBS = 0  
    MaxHR = st.number_input("Maximum Heart Rate", min_value=66, max_value=250, value=202)
    Oldpeak = st.number_input("Oldpeak (ST Depression)", min_value=-2.0, max_value=4.0, value=0.60)
    ChestPainType = st.selectbox("Chest Pain Type", ["ASY", "NAP", "ATA", "TA"])
    RestingECG = st.selectbox("Resting ECG", ["Normal", "LVH", "ST"])
    ExerciseAngina = st.selectbox("Exercise Induced Angina", ["No", "Yes"])
    ST_Slope = st.selectbox("ST Slope", ["Flat", "Up", "Down"])

if st.button("πŸ” Predict Risk"):
    try:
        input_data = [[Age, Sex, ChestPainType, RestingBP, Cholesterol, FastingBS,
                       RestingECG, MaxHR, ExerciseAngina, Oldpeak, ST_Slope]]

        prediction = model.predict(input_data)[0]  
        probability = model.predict_proba(input_data)[0][1] * 100  

        # Determine Risk Category
        if prediction == 1:
            result_text = "❌⚠️ High Risk of Heart Disease!"
            result_class = "high-risk"
        else:
            result_text = "βœ… Low Risk of Heart Disease!"
            result_class = "low-risk"

        # Display Result
        st.markdown(
            f"<div class='result-box {result_class}'> {result_text} </div>",
            unsafe_allow_html=True,
        )

        # Display Probability Score
        st.markdown(
            f"<div class='result-box probability'> πŸ“Š Risk Probability: {probability:.2f}% </div>",
            unsafe_allow_html=True,
        )

    except Exception as e:
        st.error(f"⚠️ Prediction failed: {e}")