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"""
""",
unsafe_allow_html=True
)
# Main content
st.markdown("
Heart attack Prediction
", 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("Select a Patient Row for Prediction:
", unsafe_allow_html=True)
# Option to select a row by index
st.markdown("Select a Row Index:
", 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("Row selected for heart disease prediction:
", 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"Prediction Result: {result}
", 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",
)