File size: 4,279 Bytes
dbfae11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import pickle
import numpy as np
import base64


# Load the background image
def get_base64_of_bin_file(bin_file):
    with open(bin_file, "rb") as f:
        data = f.read()  # Read the file only once
    return base64.b64encode(data).decode()


# Path to the background image
background_image_path = "background2.jpg"  # Replace with your local image path
background_base64 = get_base64_of_bin_file(background_image_path)

# CSS for the full-screen background image
st.markdown(
    f"""
    <style>
    .stApp {{
        background-image: url("data:image/jpeg;base64,{background_base64}");
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        width: 100%;
        height: 100vh;
    }}
    </style>
    """,
    unsafe_allow_html=True
)

# Main content
st.markdown("<h1 style='text-align: center; color: red;'>Heart attack Prediction</h1>", unsafe_allow_html=True)


# Load the dataset
@st.cache_data
def load_dataset():
    return pd.read_csv('heart_disease_health_indicators.csv')  # Update with the path to your dataset

# Cache function to convert DataFrame to CSV
@st.cache_data
def convert_df(df):
    return df.to_csv(index=False).encode("utf-8")

# Load pre-trained model
@st.cache_resource
def load_model():
    with open("model.pkl", "rb") as f:  # Update the model path
        model = pickle.load(f)
    return model

# Load the dataset
df = load_dataset()

# Ensure the dataset has the required columns
required_columns = ['HeartDiseaseorAttack', 'HighBP', 'HighChol', 'CholCheck', 'BMI', 'Smoker', 
                    'Stroke', 'Diabetes', 'PhysActivity', 'Fruits', 'Veggies', 'HvyAlcoholConsump', 
                    'AnyHealthcare', 'NoDocbcCost', 'GenHlth', 'MentHlth', 'PhysHlth', 'DiffWalk', 
                    'Sex', 'Age', 'Education', 'Income']

if not all(col in df.columns for col in required_columns):
    st.error(f"The dataset must contain the following columns: {', '.join(required_columns)}")
else:
    # Add functionality for row selection
    st.markdown("<h3 style='color: red;'>Select a Patient Row for Prediction:</h3>", unsafe_allow_html=True)


    # Option to select a row by index
    st.markdown("<p style='color: red; font-size:16px;'>Select a Row Index:</p>", unsafe_allow_html=True)
    selected_row_index = st.selectbox("", options=range(len(df)), index=0)

    # Add a button below the row selection for heart disease prediction
    predict_button = st.button("Predict Heart attack Risk")

    # If the "Predict" button is clicked
    if predict_button:
        # Row to use for the model
        row_to_use = df.iloc[selected_row_index]

        # Prepare input data for the model (assuming all columns are numeric)
        row_to_use_for_model = row_to_use.drop('HeartDiseaseorAttack')  # Remove target column

        # Load the model
        model = load_model()

        # Check if the number of features matches the model's expectations
        if len(row_to_use_for_model) != model.n_features_in_:
            st.error(f"The model expects {model.n_features_in_} features, but {len(row_to_use_for_model)} were provided.")
        else:
            # Apply the model for heart disease prediction
            prediction = model.predict([row_to_use_for_model])

            # Display the row and the prediction result
            st.markdown("<h3 style='color: red;'>Row selected for heart disease prediction:</h3>", unsafe_allow_html=True)
            st.write(row_to_use)

            # Show the prediction result
            result = "Does Not have Heart Disease or Attack" if prediction[0] == 1 else "Does Not Has Heart Disease or Attack"
            st.markdown(f"<h3 style='color: red;'>Prediction Result: {result}</h3>", unsafe_allow_html=True)


            # Provide option to download the result
            result_df = row_to_use.to_frame().T  # Convert Series to DataFrame
            result_df['Heart Disease Prediction'] = result
            result_csv = convert_df(result_df)
            st.download_button(
                label="Download Prediction Result",
                data=result_csv,
                file_name="Heart_Disease_Prediction_Result.csv",
                mime="text/csv",
            )