| import streamlit as st |
| import pandas as pd |
| import joblib |
| import os |
|
|
| |
| |
| |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| model = joblib.load(os.path.join(BASE_DIR, "heart_model.pkl")) |
|
|
| |
| |
| |
| st.set_page_config( |
| page_title="Heart Disease Prediction", |
| page_icon="❤️", |
| layout="centered" |
| ) |
|
|
| st.title("❤️ Heart Disease Prediction") |
| st.write("Predict the risk of heart disease based on patient data") |
|
|
| |
| |
| |
| st.sidebar.header("Patient Information") |
|
|
| age = st.sidebar.slider("Age", 20, 100, 50) |
| sex = st.sidebar.selectbox("Sex", ["Female", "Male"]) |
| sex = 0 if sex == "Female" else 1 |
| cp = st.sidebar.slider("Chest Pain Type (cp)", 0, 3, 1) |
| trestbps = st.sidebar.number_input("Resting Blood Pressure", 80, 200, 120) |
| chol = st.sidebar.number_input("Cholesterol", 100, 600, 200) |
| fbs = st.sidebar.selectbox("Fasting Blood Sugar > 120 mg/dl", [0, 1]) |
| thalach = st.sidebar.number_input("Max Heart Rate Achieved", 60, 220, 150) |
| exang = st.sidebar.selectbox("Exercise Induced Angina", [0, 1]) |
| oldpeak = st.sidebar.slider("ST Depression (oldpeak)", 0.0, 6.0, 1.0) |
|
|
| |
| |
| |
| input_df = pd.DataFrame({ |
| "age": [age], |
| "sex": [sex], |
| "cp": [cp], |
| "trestbps": [trestbps], |
| "chol": [chol], |
| "fbs": [fbs], |
| "thalach": [thalach], |
| "exang": [exang], |
| "oldpeak": [oldpeak] |
| }) |
|
|
| st.subheader("Patient Data") |
| st.write(input_df) |
|
|
| |
| |
| |
| if st.button("Predict"): |
|
|
| prediction = model.predict(input_df)[0] |
| probability = model.predict_proba(input_df)[0][1] |
|
|
| st.subheader("Result") |
|
|
| if prediction == 1: |
| st.error(f"⚠️ High Risk of Heart Disease ({probability:.2%})") |
| else: |
| st.success(f"✅ Low Risk of Heart Disease ({1 - probability:.2%})") |