import gradio as gr import pandas as pd import joblib import numpy as np # === Load trained MLP model for NHNLC prediction === model = joblib.load("MLP_LDL_Predict_by_HNHLC.s3a") # Ensure model file is in the same directory # === Prediction function === def predict_ldl(tc, tg, hdl): try: # Step 1: Derived inputs nhc = tc - hdl tg_hdl_ratio = tg / hdl if hdl != 0 else 0 # avoid divide-by-zero # Step 2: Prediction of NHNLC input_df = pd.DataFrame([[nhc, tg_hdl_ratio]], columns=["NHC", "TG/HDL"]) nhnlc = model.predict(input_df)[0] # Step 3: LDL Calculation ldl = tc - hdl - nhnlc nhnlc = round(nhnlc, 0) ldl = round(ldl, 0) return f"The Non-HDL Non-LDL Cholesterol = {nhnlc} mg/dL\nThe final estimated LDL = {ldl} mg/dL" except Exception as e: return f"Error: {str(e)}" # === Gradio UI === interface = gr.Interface( fn=predict_ldl, inputs=[ gr.Number(label="Total Cholesterol (mg/dL)"), gr.Number(label="Triglycerides (mg/dL)"), gr.Number(label="HDL Cholesterol (mg/dL)") ], outputs=gr.Textbox(label="LDL Estimation Result"), title="MLP Neural Network LDL Estimator (via NHNLC)", description="This model estimates LDL using a 2-step approach: first it predicts Non-HDL Non-LDL Cholesterol (NHNLC) using NHC and TG/HDL ratio, then calculates LDL = TC - HDL - NHNLC." ) # === Launch interface === interface.launch()