Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,117 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Load saved
|
| 7 |
-
model = joblib.load('
|
| 8 |
-
scaler = joblib.load('
|
| 9 |
label_enc = joblib.load('label_encoder.pkl')
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Scale input features
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Predict
|
| 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 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
|
| 5 |
+
# Load saved objects
|
| 6 |
+
model = joblib.load('model.pkl')
|
| 7 |
+
scaler = joblib.load('scaler.pkl')
|
| 8 |
label_enc = joblib.load('label_encoder.pkl')
|
| 9 |
|
| 10 |
+
def predict_diabetes(
|
| 11 |
+
Age,
|
| 12 |
+
Total_cholesterol,
|
| 13 |
+
HDL,
|
| 14 |
+
LDL,
|
| 15 |
+
VLDL,
|
| 16 |
+
TRIGLYCERIDES,
|
| 17 |
+
before_random_blood_sugar,
|
| 18 |
+
before_HbA1c,
|
| 19 |
+
alcohol_consumption,
|
| 20 |
+
family_history_diabetes,
|
| 21 |
+
Gender,
|
| 22 |
+
dietary_habits,
|
| 23 |
+
smoking_status,
|
| 24 |
+
family_history_cardiovascular_disease
|
| 25 |
+
):
|
| 26 |
+
# Convert categorical inputs to numeric
|
| 27 |
+
Gender_FEMALE = 1 if Gender == "Female" else 0
|
| 28 |
+
Gender_MALE = 1 if Gender == "Male" else 0
|
| 29 |
+
|
| 30 |
+
dietary_non_vegetarian = 1 if dietary_habits == "Non-vegetarian" else 0
|
| 31 |
+
dietary_vegetarian = 1 if dietary_habits == "Vegetarian" else 0
|
| 32 |
+
|
| 33 |
+
smoking_no = 1 if smoking_status == "No" else 0
|
| 34 |
+
smoking_yes = 1 if smoking_status == "Yes" else 0
|
| 35 |
+
|
| 36 |
+
family_history_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
|
| 37 |
+
family_history_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
|
| 38 |
+
|
| 39 |
+
# Prepare input in the right order as during training
|
| 40 |
+
input_data = np.array([[
|
| 41 |
+
Age,
|
| 42 |
+
Total_cholesterol,
|
| 43 |
+
HDL,
|
| 44 |
+
LDL,
|
| 45 |
+
VLDL,
|
| 46 |
+
TRIGLYCERIDES,
|
| 47 |
+
before_random_blood_sugar,
|
| 48 |
+
before_HbA1c,
|
| 49 |
+
int(alcohol_consumption),
|
| 50 |
+
int(family_history_diabetes),
|
| 51 |
+
Gender_FEMALE,
|
| 52 |
+
Gender_MALE,
|
| 53 |
+
dietary_non_vegetarian,
|
| 54 |
+
dietary_vegetarian,
|
| 55 |
+
smoking_no,
|
| 56 |
+
smoking_yes,
|
| 57 |
+
family_history_cvd_no,
|
| 58 |
+
family_history_cvd_yes
|
| 59 |
+
]])
|
| 60 |
+
|
| 61 |
# Scale input features
|
| 62 |
+
input_scaled = scaler.transform(input_data)
|
| 63 |
+
|
| 64 |
+
# Predict
|
| 65 |
+
pred_enc = model.predict(input_scaled)
|
| 66 |
+
pred_label = label_enc.inverse_transform(pred_enc)[0]
|
| 67 |
+
|
| 68 |
+
return f"Predicted Status: {pred_label}"
|
| 69 |
+
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("# Diabetes Predictor")
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
"""
|
| 74 |
+
Developed by Dr. Vinod Kumar Yata's research group
|
| 75 |
+
School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
|
| 76 |
+
|
| 77 |
+
⚠️ **Warning:**
|
| 78 |
+
This is an experimental tool and should not be used for medical diagnosis.
|
| 79 |
+
Always consult a licensed healthcare provider for medical advice.
|
| 80 |
+
"""
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column():
|
| 85 |
+
Age = gr.Number(label="Age", value=30)
|
| 86 |
+
Total_cholesterol = gr.Number(label="Total cholesterol", value=180)
|
| 87 |
+
HDL = gr.Number(label="HDL", value=50)
|
| 88 |
+
LDL = gr.Number(label="LDL", value=100)
|
| 89 |
+
VLDL = gr.Number(label="VLDL", value=20)
|
| 90 |
+
TRIGLYCERIDES = gr.Number(label="TRIGLYCERIDES", value=150)
|
| 91 |
+
before_random_blood_sugar = gr.Number(label="Before glycemic control random blood sugar", value=120)
|
| 92 |
+
before_HbA1c = gr.Number(label="Before glycemic control HbA1c", value=5.5)
|
| 93 |
+
|
| 94 |
+
with gr.Column():
|
| 95 |
+
alcohol_consumption = gr.Radio(label="Alcohol consumption", choices=["0", "1"], value="0")
|
| 96 |
+
family_history_diabetes = gr.Radio(label="Family history of diabetes", choices=["0", "1"], value="0")
|
| 97 |
+
Gender = gr.Radio(label="Gender", choices=["Female", "Male"], value="Female")
|
| 98 |
+
dietary_habits = gr.Radio(label="Dietary habits", choices=["Vegetarian", "Non-vegetarian"], value="Vegetarian")
|
| 99 |
+
smoking_status = gr.Radio(label="Smoking status", choices=["No", "Yes"], value="No")
|
| 100 |
+
family_history_cardiovascular_disease = gr.Radio(label="Family history of cardiovascular disease", choices=["No", "Yes"], value="No")
|
| 101 |
+
|
| 102 |
+
submit_btn = gr.Button("Submit")
|
| 103 |
+
output = gr.Textbox(label="Prediction")
|
| 104 |
+
|
| 105 |
+
submit_btn.click(
|
| 106 |
+
fn=predict_diabetes,
|
| 107 |
+
inputs=[
|
| 108 |
+
Age, Total_cholesterol, HDL, LDL, VLDL, TRIGLYCERIDES,
|
| 109 |
+
before_random_blood_sugar, before_HbA1c,
|
| 110 |
+
alcohol_consumption, family_history_diabetes, Gender,
|
| 111 |
+
dietary_habits, smoking_status, family_history_cardiovascular_disease
|
| 112 |
+
],
|
| 113 |
+
outputs=output
|
| 114 |
+
)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|