edominicDukeHuggingFace's picture
Upload 5 files
9949d25 verified
import streamlit as st
from tensorflow.keras.models import load_model
import numpy as np
import joblib
def get_user_values():
# Create a dictionary for the user_values
user_values = {}
# Add a title.
st.title("Heart Disease Detector")
# Add a slider for age.
user_values["age"] = st.slider(label="What is your age?", min_value=0, max_value=100)
# Add a radio button widget for sex.
user_values["sex"] = st.radio(label="What is your sex?", options=["Male", "Female"])
# Add a slider for chest pain type.
user_values["cp"] = st.radio(label="Select a chest pain type:", options=["0 - Typical angina", "1 - Atypical angina", "2 - Non-anginal pain", "3 - Asymptomatic"])
# Add a numeric input widget for resting blood pressure.
user_values["trestbps"] = st.number_input(label="What is your resting blood pressure?")
# Add a numeric input widget for total serum cholesterol.
user_values["chol"] = st.number_input(label="What is your total serum cholesterol (in mg/dl)?")
# Add a radio button widget for whether there is fasting blood sugar.
user_values["fbs"] = st.radio(label="Is your fasting blood sugar greater than 120 mg/dl?", options=["Yes", "No"])
# Add a radio button widget for restecg.
user_values["restecg"] = st.radio(label="What is your resting electrocardiographic result?", options=["0 — Normal ECG", "1 — ST-T Wave Abnormality", "2 — Left Ventricular Hypertrophy (LVH) by Estes’ Criteria"])
# Add a numeric input widget for maximum heart rate.
user_values["thalach"] = st.number_input(label="What is your maximum heart rate?")
# Add a radio button widget for exercise induced angina.
user_values["exang"] = st.radio(label="Do you have exercise induced angina?", options=["Yes", "No"])
# Add a numeric input widget for ST depression induced by exercise relative to rest.
user_values["oldpeak"] = st.number_input(label="Type the ST depression induced by exercise relative to rest:")
# Add a numeric input widget for the slope of the peak exercise ST segment.
user_values["slope"] = st.number_input(label="Type the the slope of the peak exercise ST segment:")
# Add a radio button widget for ca.
user_values["ca"] = st.radio(label="How many major blood vessels were colored by flourosopy?", options=["0", "1", "2", "3"])
# Add a radio button widget for thal.
user_values["thal"] = st.radio(label="What is your thalassemia test result?", options=["1 - Normal", "2 - Fixed Defect", "3 - Reversible Defect"])
# Return the user's values.
return user_values
# Method generated by ChatGPT 5.1 on 12/10/25
def translate_user_values(user_values):
translated = {}
# Age (already numeric from slider, but cast to int for safety)
translated["age"] = int(user_values["age"])
# Sex: 1 = male; 0 = female
translated["sex"] = 1 if user_values["sex"] == "Male" else 0
# Chest pain type (cp): just the number before the " - "
# e.g., "0 - Typical angina" -> 0
translated["cp"] = int(user_values["cp"].split(" ")[0])
# Resting blood pressure
translated["trestbps"] = float(user_values["trestbps"])
# Serum cholesterol
translated["chol"] = float(user_values["chol"])
# Fasting blood sugar > 120 mg/dl: 1 = yes; 0 = no
translated["fbs"] = 1 if user_values["fbs"] == "Yes" else 0
# Resting ECG: number before the " — "
# e.g., "0 — Normal ECG" -> 0
translated["restecg"] = int(user_values["restecg"].split(" ")[0])
# Maximum heart rate
translated["thalach"] = float(user_values["thalach"])
# Exercise induced angina: 1 = yes; 0 = no
translated["exang"] = 1 if user_values["exang"] == "Yes" else 0
# ST depression
translated["oldpeak"] = float(user_values["oldpeak"])
# Slope of peak exercise ST segment
translated["slope"] = float(user_values["slope"])
# Number of major vessels (ca): radio options are "0", "1", "2", "3"
translated["ca"] = int(user_values["ca"])
# Thalassemia:
thal_raw = int(user_values["thal"].split(" ")[0])
translated["thal"] = thal_raw
return translated
# Determine whether the given data indicates the presence of heart disease or not.
def report_heart_disease_verdict(tf_model, X, fitted_stand_scaler, fitted_rob_scaler):
# Transform the given data with the fitted scalers.
X = fitted_stand_scaler.transform(X)
X = fitted_rob_scaler.transform(X)
# Perform model inference to get a verdict (0 - No heart disease or 1 - Heart disease)
verdict = tf_model.predict(X)
# Report the verdict.
if verdict == 1:
st.write("Verdict: You likely have a heart disease.")
else:
st.write("Verdict: You likely do not have a heart disease.")
def main():
# Load the custom TensorFlow model that will be used for heart disease detection.
tf_model = load_model(filepath="models/tf_mlp.keras")
# Load the fitted Standard Scaler and Robut Scaler from the custom TensorFlow model's training.
# Following two lines of code generated by ChatGPT 5.1 on 12/10/25.
fitted_stand_scaler = joblib.load("models/standard_scaler.pkl")
fitted_rob_scaler = joblib.load("models/robust_scaler.pkl")
# Get the user's values.
user_values = get_user_values()
# Get the translated values.
translated_values = translate_user_values(user_values)
# Perform feature engineering on the translated values.
translated_values["age_squared"] = translated_values["age"] * translated_values["age"]
translated_values["chol_times_trestbps"] = translated_values["chol"] * translated_values["trestbps"]
translated_values["age_times_thalach"] = translated_values["age"] * translated_values["thalach"]
translated_values["oldpeak_squared"] = translated_values["oldpeak"] * translated_values["oldpeak"]
# Convert to the translated values dictionary into a NumPy ndarray.
translated_values_array = np.array([list(translated_values.values())], dtype=float) # Line of code generated by ChatGPT 5.1 on 12/10/25
# Predict whether the translated values indicate the presence of heart disease or not.
want_prediction = st.radio(label="Predict?", options=["No", "Yes"])
if want_prediction == "Yes":
report_heart_disease_verdict(tf_model, translated_values_array, fitted_stand_scaler, fitted_rob_scaler)
if __name__ == '__main__':
main()