Heart_Risk.Ai / app.py
DOMMETI's picture
Update app.py
12775b3 verified
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}")