Spaces:
Sleeping
Sleeping
File size: 5,217 Bytes
6b881c1 bfd1741 acc6a60 6b881c1 ff343d7 0007545 9afc34e 6b881c1 bfd1741 acc6a60 ff343d7 5802474 ff343d7 acc6a60 ff343d7 acc6a60 ff343d7 acc6a60 ff343d7 bfd1741 ff343d7 bfd1741 ff343d7 bfd1741 ff343d7 0007545 ff343d7 bfd1741 ff343d7 acc6a60 bfd1741 acc6a60 bfd1741 9afc34e bfd1741 acc6a60 bfd1741 acc6a60 bfd1741 acc6a60 ff343d7 acc6a60 bfd1741 acc6a60 bfd1741 acc6a60 9afc34e acc6a60 6b881c1 acc6a60 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
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()
|