Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,12 @@ import numpy as np
|
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
model_binary = joblib.load("model_binary_cv.pkl")
|
| 8 |
model_multiclass = joblib.load("model_multiclass_cv.pkl")
|
| 9 |
scaler = joblib.load("scaler.pkl")
|
| 10 |
label_enc = joblib.load("label_encoder_multiclass.pkl")
|
| 11 |
|
| 12 |
-
# Feature names for consistency with scaler
|
| 13 |
feature_names = [
|
| 14 |
'Patient Id', 'Age', 'Total cholesterol', 'HDL', 'LDL', 'VLDL',
|
| 15 |
'TRIGLYCERIDES', 'before glycemic control random blood sugar',
|
|
@@ -37,51 +36,49 @@ def predict_diabetes(
|
|
| 37 |
smoking_status,
|
| 38 |
family_history_cardiovascular_disease
|
| 39 |
):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
Gender_MALE = 1 if Gender == "Male" else 0
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
input_df = pd.DataFrame(input_values, columns=feature_names)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
input_scaled = scaler.transform(input_df)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return "✅ Prediction: No Diabetes Risk (Normal)"
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
status = label_enc.inverse_transform([multi_pred])[0]
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
return f"{emoji} At Risk: {status_display}"
|
| 83 |
|
| 84 |
-
# Gradio UI
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
gr.Markdown("## 🩺 Diabetes Risk and Type Predictor")
|
| 87 |
gr.Markdown(
|
|
@@ -108,8 +105,8 @@ with gr.Blocks() as demo:
|
|
| 108 |
before_HbA1c = gr.Number(label="HbA1c (before control)", value=5.5)
|
| 109 |
|
| 110 |
with gr.Column():
|
| 111 |
-
alcohol_consumption = gr.
|
| 112 |
-
family_history_diabetes = gr.
|
| 113 |
Gender = gr.Radio(label="Gender", choices=["Female", "Male"], value="Female")
|
| 114 |
dietary_habits = gr.Radio(label="Dietary Habits", choices=["Vegetarian", "Non-vegetarian"], value="Vegetarian")
|
| 115 |
smoking_status = gr.Radio(label="Smoking Status", choices=["No", "Yes"], value="No")
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
|
| 6 |
+
# Load models and scaler
|
| 7 |
model_binary = joblib.load("model_binary_cv.pkl")
|
| 8 |
model_multiclass = joblib.load("model_multiclass_cv.pkl")
|
| 9 |
scaler = joblib.load("scaler.pkl")
|
| 10 |
label_enc = joblib.load("label_encoder_multiclass.pkl")
|
| 11 |
|
|
|
|
| 12 |
feature_names = [
|
| 13 |
'Patient Id', 'Age', 'Total cholesterol', 'HDL', 'LDL', 'VLDL',
|
| 14 |
'TRIGLYCERIDES', 'before glycemic control random blood sugar',
|
|
|
|
| 36 |
smoking_status,
|
| 37 |
family_history_cardiovascular_disease
|
| 38 |
):
|
| 39 |
+
try:
|
| 40 |
+
alcohol_consumption = int(alcohol_consumption)
|
| 41 |
+
family_history_diabetes = int(family_history_diabetes)
|
| 42 |
|
| 43 |
+
Gender_FEMALE = 1 if Gender == "Female" else 0
|
| 44 |
+
Gender_MALE = 1 if Gender == "Male" else 0
|
|
|
|
| 45 |
|
| 46 |
+
dietary_non_veg = 1 if dietary_habits == "Non-vegetarian" else 0
|
| 47 |
+
dietary_non_veg_dup = dietary_non_veg
|
| 48 |
+
dietary_veg = 1 if dietary_habits == "Vegetarian" else 0
|
| 49 |
|
| 50 |
+
smoking_no = 1 if smoking_status == "No" else 0
|
| 51 |
+
smoking_yes = 1 if smoking_status == "Yes" else 0
|
| 52 |
|
| 53 |
+
family_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
|
| 54 |
+
family_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
|
| 55 |
|
| 56 |
+
input_values = [[
|
| 57 |
+
0, Age, Total_cholesterol, HDL, LDL, VLDL, TRIGLYCERIDES,
|
| 58 |
+
before_random_blood_sugar, before_HbA1c, alcohol_consumption, family_history_diabetes,
|
| 59 |
+
Gender_FEMALE, Gender_MALE, dietary_non_veg, dietary_non_veg_dup, dietary_veg,
|
| 60 |
+
smoking_no, smoking_yes, family_cvd_no, family_cvd_yes
|
| 61 |
+
]]
|
| 62 |
+
input_df = pd.DataFrame(input_values, columns=feature_names)
|
|
|
|
| 63 |
|
| 64 |
+
input_scaled = scaler.transform(input_df)
|
|
|
|
| 65 |
|
| 66 |
+
binary_pred = model_binary.predict(input_scaled)[0]
|
| 67 |
+
if binary_pred == 0:
|
| 68 |
+
return "✅ Prediction: No Diabetes Risk (Normal)"
|
|
|
|
| 69 |
|
| 70 |
+
multi_pred = model_multiclass.predict(input_scaled)[0]
|
| 71 |
+
status = label_enc.inverse_transform([multi_pred])[0]
|
|
|
|
| 72 |
|
| 73 |
+
status_display = "Prediabetes" if status == "prediabetes" else "Diabetes"
|
| 74 |
+
emoji = "⚠️🟡" if status == "prediabetes" else "🚨🔴"
|
| 75 |
+
|
| 76 |
+
return f"{emoji} At Risk: {status_display}"
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"❌ Error during prediction: {str(e)}"
|
| 80 |
|
|
|
|
| 81 |
|
|
|
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
gr.Markdown("## 🩺 Diabetes Risk and Type Predictor")
|
| 84 |
gr.Markdown(
|
|
|
|
| 105 |
before_HbA1c = gr.Number(label="HbA1c (before control)", value=5.5)
|
| 106 |
|
| 107 |
with gr.Column():
|
| 108 |
+
alcohol_consumption = gr.Checkbox(label="Alcohol Consumption (Yes)", value=False)
|
| 109 |
+
family_history_diabetes = gr.Checkbox(label="Family History of Diabetes (Yes)", value=False)
|
| 110 |
Gender = gr.Radio(label="Gender", choices=["Female", "Male"], value="Female")
|
| 111 |
dietary_habits = gr.Radio(label="Dietary Habits", choices=["Vegetarian", "Non-vegetarian"], value="Vegetarian")
|
| 112 |
smoking_status = gr.Radio(label="Smoking Status", choices=["No", "Yes"], value="No")
|