import re import pandas as pd import numpy as np import gradio as gr import warnings import time # Suppress warnings warnings.simplefilter(action='ignore', category=FutureWarning) # Load model def load_model(model_path): return pd.read_pickle(model_path) model = load_model("hear_disease_detection.pkl") def predict_heart_failure(age, blood_pressure, cholesterol, fasting_blood_sugar, max_heart_rate, old_peak, gender, chest_pain_type, ecg, exercise_angina, st_slope): # Check for missing inputs if age is None or blood_pressure is None or cholesterol is None or max_heart_rate is None or old_peak is None: return "Please enter all required numerical values." if gender is None or chest_pain_type is None or ecg is None or exercise_angina is None or st_slope is None: return "Please select all required options." # Convert inputs patient_fasting_blood_sugar = 1 if fasting_blood_sugar == "Greater Than 120 mg/dl" else 0 patient_gender = [1] if gender == "Male" else [0] patient_chest_pain_type = [0, 0, 0] if chest_pain_type == "Typical Angina": patient_chest_pain_type = [0, 0, 1] elif chest_pain_type == "Atypical Angina": patient_chest_pain_type = [1, 0, 0] elif chest_pain_type == "Non-anginal Pain": patient_chest_pain_type = [0, 1, 0] patient_ecg = [1, 0] if ecg == "Normal" else [0, 1] if ecg == "ST" else [0, 0] patient_exercise_angina = [1] if exercise_angina == "Yes" else [0] patient_slope = [0, 0] if st_slope == "Down" else [1, 0] if st_slope == "Flat" else [0, 1] # Prepare input for the model new_data = [age, blood_pressure, cholesterol, patient_fasting_blood_sugar, max_heart_rate, old_peak] new_data.extend(patient_gender) new_data.extend(patient_chest_pain_type) new_data.extend(patient_ecg) new_data.extend(patient_exercise_angina) new_data.extend(patient_slope) # Prediction predicted_value = model.predict([new_data])[0] prediction_prop = np.round(model.predict_proba([new_data]) * 100) prediction = ( "Based on the information provided, you are not a heart patient." if predicted_value == 0 else "Based on the information provided, you are at risk of heart disease." ) # Return individual outputs return prediction, prediction_prop[0, 0], prediction_prop[0, 1] # Gradio interface inputs = [ gr.Slider(minimum=1, maximum=90, value=48, label="Age"), gr.Slider(minimum=0, maximum=200, value=140, label="Resting Blood Pressure"), gr.Slider(minimum=0, maximum=510, value=228, label="Cholesterol"), gr.Radio(["Greater Than 120 mg/dl", "Less Than 120 mg/dl"], label="Fasting Blood Sugar"), gr.Slider(minimum=0, maximum=200, value=100, label="Max Heart Rate"), gr.Slider(minimum=-3.0, maximum=4.5, value=2.5, label="Old Peak"), gr.Radio(["Male", "Female"], label="Gender"), gr.Radio(["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"], label="Chest Pain Type"), gr.Radio(["Normal", "ST", "LVH"], label="ECG"), gr.Radio(["Yes", "No"], label="Exercise Angina"), gr.Radio(["Up", "Flat", "Down"], label="ST Slope"), ] outputs = [ gr.Textbox(label="Prediction"), ] # Launch the Gradio app gr.Interface(fn=predict_heart_failure, inputs=inputs, outputs=outputs, title="Heart Failure Prediction", description="Enter the details to predict heart disease.").launch()