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(
"""
""",
unsafe_allow_html=True,
)
st.markdown("
💜 Heart Disease Risk Predictor
", 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" {result_text}
",
unsafe_allow_html=True,
)
# Display Probability Score
st.markdown(
f" 📊 Risk Probability: {probability:.2f}%
",
unsafe_allow_html=True,
)
except Exception as e:
st.error(f"⚠️ Prediction failed: {e}")