Nisha2004's picture
Upload app.py
21f6288
import streamlit as st
import pickle
import numpy as np
loaded_model = None # Define the variable outside the try block
try:
with open('heart_disease_model.pkl', 'rb') as file:
loaded_model = pickle.load(file)
print("Model loaded successfully!")
except FileNotFoundError:
print("Error: The file 'heart_disease_model.pkl' was not found.")
except Exception as e:
print("An error occurred while loading the model:", e)
def predict_heart_disease(input_data):
if loaded_model is None:
return None # Handle case where the model wasn't loaded successfully
input_data_reshaped = np.asarray(input_data).reshape(1, -1)
prediction = loaded_model.predict(input_data_reshaped)
return prediction[0]
def main():
st.set_page_config(layout="centered")
st.title('Heart Disease Prediction')
st.markdown('Enter the values for the input features:')
feature_names = [
'age',
'sex',
'cp',
'trestbps',
'chol',
'fbs',
'restecg',
'thalach',
'exang',
'oldpeak',
'ca',
'thal',
'target',
]
col1, col2 = st.columns([2, 1])
with col1:
input_data = []
for feature_name in feature_names:
input_value = st.number_input(feature_name, step=0.01)
input_data.append(float(input_value))
if st.button('Predict'):
prediction = predict_heart_disease(input_data)
if prediction is None:st.warning('The Person has Heart Disease')
elif prediction == 0:
st.error('The Person has Heart Disease')
else:
st.success('The Person does not have Heart Disease')
with col2:
st.markdown('**IF YES**')
st.write('The person has heart disease, and they should seek medical care immediately. '
'It requires prompt medical attention and treatment.')
st.markdown('**NO**')
st.write('The person does not have any heart-related issues. '
'There is no need to worry.')
st.markdown('**NOTE:** The prediction provided by this app is for informational purposes only. '
'It is not a substitute for professional medical advice or diagnosis.')
st.markdown('<br>', unsafe_allow_html=True)
if __name__ == '__main__':
main()