high77's picture
Update app.py
ff343d7 verified
import gradio as gr
import numpy as np
import pandas as pd
import joblib
# Load models and scaler
model_binary = joblib.load("model_binary_cv.pkl")
model_multiclass = joblib.load("model_multiclass_cv.pkl")
scaler = joblib.load("scaler.pkl")
label_enc = joblib.load("label_encoder_multiclass.pkl")
feature_names = [
'Patient Id', 'Age', 'Total cholesterol', 'HDL', 'LDL', 'VLDL',
'TRIGLYCERIDES', 'before glycemic control random blood sugar',
'before glycemic control HbA1c', 'alcohol consumption', 'family_history_diabetes',
'Gender_FEMALE', 'Gender_MALE', 'dietary habits_non-vegetarian',
'dietary habits_non-vegetarian ', 'dietary habits_vegetarian',
'smoking status_no', 'smoking status_yes',
'family_history_cardiovascular_disease_no',
'family_history_cardiovascular_disease_yes'
]
def predict_diabetes(
Age,
Total_cholesterol,
HDL,
LDL,
VLDL,
TRIGLYCERIDES,
before_random_blood_sugar,
before_HbA1c,
alcohol_consumption,
family_history_diabetes,
Gender,
dietary_habits,
smoking_status,
family_history_cardiovascular_disease
):
try:
alcohol_consumption = int(alcohol_consumption)
family_history_diabetes = int(family_history_diabetes)
Gender_FEMALE = 1 if Gender == "Female" else 0
Gender_MALE = 1 if Gender == "Male" else 0
dietary_non_veg = 1 if dietary_habits == "Non-vegetarian" else 0
dietary_non_veg_dup = dietary_non_veg
dietary_veg = 1 if dietary_habits == "Vegetarian" else 0
smoking_no = 1 if smoking_status == "No" else 0
smoking_yes = 1 if smoking_status == "Yes" else 0
family_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
family_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
input_values = [[
0, Age, Total_cholesterol, HDL, LDL, VLDL, TRIGLYCERIDES,
before_random_blood_sugar, before_HbA1c, alcohol_consumption, family_history_diabetes,
Gender_FEMALE, Gender_MALE, dietary_non_veg, dietary_non_veg_dup, dietary_veg,
smoking_no, smoking_yes, family_cvd_no, family_cvd_yes
]]
input_df = pd.DataFrame(input_values, columns=feature_names)
input_scaled = scaler.transform(input_df)
binary_pred = model_binary.predict(input_scaled)[0]
if binary_pred == 0:
return "✅ Prediction: No Diabetes Risk (Normal)"
multi_pred = model_multiclass.predict(input_scaled)[0]
status = label_enc.inverse_transform([multi_pred])[0]
status_display = "Prediabetes" if status == "prediabetes" else "Diabetes"
emoji = "⚠️🟡" if status == "prediabetes" else "🚨🔴"
return f"{emoji} At Risk: {status_display}"
except Exception as e:
return f"❌ Error during prediction: {str(e)}"
with gr.Blocks() as demo:
gr.Markdown("## 🩺 Diabetes Risk and Type Predictor")
gr.Markdown(
"""
**Developed by Dr. Vinod Kumar Yata's research group**
School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
---
⚠️ This AI tool is for **research purposes only**.
It predicts **diabetes risk** and if at risk, whether it's **prediabetes or diabetes**.
Please consult a medical professional for any diagnosis.
"""
)
with gr.Row():
with gr.Column():
Age = gr.Number(label="Age", value=30)
Total_cholesterol = gr.Number(label="Total Cholesterol", value=180)
HDL = gr.Number(label="HDL", value=50)
LDL = gr.Number(label="LDL", value=100)
VLDL = gr.Number(label="VLDL", value=20)
TRIGLYCERIDES = gr.Number(label="TRIGLYCERIDES", value=150)
before_random_blood_sugar = gr.Number(label="Random Blood Sugar (before control)", value=120)
before_HbA1c = gr.Number(label="HbA1c (before control)", value=5.5)
with gr.Column():
alcohol_consumption = gr.Checkbox(label="Alcohol Consumption (Yes)", value=False)
family_history_diabetes = gr.Checkbox(label="Family History of Diabetes (Yes)", value=False)
Gender = gr.Radio(label="Gender", choices=["Female", "Male"], value="Female")
dietary_habits = gr.Radio(label="Dietary Habits", choices=["Vegetarian", "Non-vegetarian"], value="Vegetarian")
smoking_status = gr.Radio(label="Smoking Status", choices=["No", "Yes"], value="No")
family_history_cardiovascular_disease = gr.Radio(label="Family History of Cardiovascular Disease", choices=["No", "Yes"], value="No")
submit_btn = gr.Button("🧪 Predict")
output = gr.Textbox(label="Prediction Result")
submit_btn.click(
fn=predict_diabetes,
inputs=[
Age, Total_cholesterol, HDL, LDL, VLDL, TRIGLYCERIDES,
before_random_blood_sugar, before_HbA1c,
alcohol_consumption, family_history_diabetes,
Gender, dietary_habits, smoking_status,
family_history_cardiovascular_disease
],
outputs=output
)
if __name__ == "__main__":
demo.launch()