high77 commited on
Commit
bfd1741
·
verified ·
1 Parent(s): 9afc34e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -55
app.py CHANGED
@@ -1,13 +1,26 @@
1
  import gradio as gr
2
  import numpy as np
 
3
  import joblib
4
 
5
- # Load models, scaler, and label encoder
6
  model_binary = joblib.load("model_binary_cv.pkl")
7
  model_multiclass = joblib.load("model_multiclass_cv.pkl")
8
  scaler = joblib.load("scaler.pkl")
9
  label_enc = joblib.load("label_encoder_multiclass.pkl")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def predict_diabetes(
12
  Age,
13
  Total_cholesterol,
@@ -24,95 +37,86 @@ def predict_diabetes(
24
  smoking_status,
25
  family_history_cardiovascular_disease
26
  ):
27
- # Convert binary inputs to int
28
  alcohol_consumption = int(alcohol_consumption)
29
  family_history_diabetes = int(family_history_diabetes)
30
 
31
- # One-hot encode categorical variables
32
  Gender_FEMALE = 1 if Gender == "Female" else 0
33
  Gender_MALE = 1 if Gender == "Male" else 0
34
 
35
- dietary_non_vegetarian = 1 if dietary_habits == "Non-vegetarian" else 0
36
- dietary_non_vegetarian_dup = dietary_non_vegetarian # duplicate column in training data
37
- dietary_vegetarian = 1 if dietary_habits == "Vegetarian" else 0
38
 
39
  smoking_no = 1 if smoking_status == "No" else 0
40
  smoking_yes = 1 if smoking_status == "Yes" else 0
41
 
42
- family_history_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
43
- family_history_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
44
-
45
- # Prepare input array (excluding 'binary_status' and matching training order)
46
- input_data = np.array([[
47
- 0, # dummy Patient ID
48
- Age,
49
- Total_cholesterol,
50
- HDL,
51
- LDL,
52
- VLDL,
53
- TRIGLYCERIDES,
54
- before_random_blood_sugar,
55
- before_HbA1c,
56
- alcohol_consumption,
57
- family_history_diabetes,
58
- Gender_FEMALE,
59
- Gender_MALE,
60
- dietary_non_vegetarian,
61
- dietary_non_vegetarian_dup,
62
- dietary_vegetarian,
63
- smoking_no,
64
- smoking_yes,
65
- family_history_cvd_no,
66
- family_history_cvd_yes
67
- ]])
68
-
69
- # Scale
70
- input_scaled = scaler.transform(input_data)
71
-
72
- # Step 1: Binary classification
73
  binary_pred = model_binary.predict(input_scaled)[0]
74
  if binary_pred == 0:
75
  return "✅ Prediction: No Diabetes Risk (Normal)"
76
 
77
  # Step 2: Multiclass prediction
78
- multiclass_pred = model_multiclass.predict(input_scaled)[0]
79
- status_label = label_enc.inverse_transform([multiclass_pred])[0]
 
 
 
80
 
81
- return f"⚠️ At Risk: {status_label.capitalize()}"
82
 
83
- # Gradio interface
84
  with gr.Blocks() as demo:
85
- gr.Markdown("# 🩺 Diabetes Risk and Type Predictor")
86
  gr.Markdown(
87
  """
88
- Developed by Dr. Vinod Kumar Yata's research group
89
  School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
90
- ⚠️ **Note:** This tool predicts both diabetes **risk** and **type** (diabetes vs prediabetes).
91
- This is a research prototype. Do not use it for medical diagnosis.
 
 
 
92
  """
93
  )
94
 
95
  with gr.Row():
96
  with gr.Column():
97
  Age = gr.Number(label="Age", value=30)
98
- Total_cholesterol = gr.Number(label="Total cholesterol", value=180)
99
  HDL = gr.Number(label="HDL", value=50)
100
  LDL = gr.Number(label="LDL", value=100)
101
  VLDL = gr.Number(label="VLDL", value=20)
102
  TRIGLYCERIDES = gr.Number(label="TRIGLYCERIDES", value=150)
103
- before_random_blood_sugar = gr.Number(label="Before glycemic control random blood sugar", value=120)
104
- before_HbA1c = gr.Number(label="Before glycemic control HbA1c", value=5.5)
105
 
106
  with gr.Column():
107
- alcohol_consumption = gr.Radio(label="Alcohol consumption", choices=["0", "1"], value="0")
108
- family_history_diabetes = gr.Radio(label="Family history of diabetes", choices=["0", "1"], value="0")
109
  Gender = gr.Radio(label="Gender", choices=["Female", "Male"], value="Female")
110
- dietary_habits = gr.Radio(label="Dietary habits", choices=["Vegetarian", "Non-vegetarian"], value="Vegetarian")
111
- smoking_status = gr.Radio(label="Smoking status", choices=["No", "Yes"], value="No")
112
- family_history_cardiovascular_disease = gr.Radio(label="Family history of cardiovascular disease", choices=["No", "Yes"], value="No")
113
 
114
- output = gr.Textbox(label="Prediction")
115
- submit_btn = gr.Button("Submit")
116
 
117
  submit_btn.click(
118
  fn=predict_diabetes,
 
1
  import gradio as gr
2
  import numpy as np
3
+ import pandas as pd
4
  import joblib
5
 
6
+ # Load trained 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 for consistency with scaler
13
+ feature_names = [
14
+ 'Patient Id', 'Age', 'Total cholesterol', 'HDL', 'LDL', 'VLDL',
15
+ 'TRIGLYCERIDES', 'before glycemic control random blood sugar',
16
+ 'before glycemic control HbA1c', 'alcohol consumption', 'family_history_diabetes',
17
+ 'Gender_FEMALE', 'Gender_MALE', 'dietary habits_non-vegetarian',
18
+ 'dietary habits_non-vegetarian ', 'dietary habits_vegetarian',
19
+ 'smoking status_no', 'smoking status_yes',
20
+ 'family_history_cardiovascular_disease_no',
21
+ 'family_history_cardiovascular_disease_yes'
22
+ ]
23
+
24
  def predict_diabetes(
25
  Age,
26
  Total_cholesterol,
 
37
  smoking_status,
38
  family_history_cardiovascular_disease
39
  ):
40
+ # Convert binary inputs
41
  alcohol_consumption = int(alcohol_consumption)
42
  family_history_diabetes = int(family_history_diabetes)
43
 
44
+ # One-hot encoding
45
  Gender_FEMALE = 1 if Gender == "Female" else 0
46
  Gender_MALE = 1 if Gender == "Male" else 0
47
 
48
+ dietary_non_veg = 1 if dietary_habits == "Non-vegetarian" else 0
49
+ dietary_non_veg_dup = dietary_non_veg
50
+ dietary_veg = 1 if dietary_habits == "Vegetarian" else 0
51
 
52
  smoking_no = 1 if smoking_status == "No" else 0
53
  smoking_yes = 1 if smoking_status == "Yes" else 0
54
 
55
+ family_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
56
+ family_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
57
+
58
+ # Create input DataFrame
59
+ input_values = [[
60
+ 0, Age, Total_cholesterol, HDL, LDL, VLDL, TRIGLYCERIDES,
61
+ before_random_blood_sugar, before_HbA1c, alcohol_consumption, family_history_diabetes,
62
+ Gender_FEMALE, Gender_MALE, dietary_non_veg, dietary_non_veg_dup, dietary_veg,
63
+ smoking_no, smoking_yes, family_cvd_no, family_cvd_yes
64
+ ]]
65
+ input_df = pd.DataFrame(input_values, columns=feature_names)
66
+
67
+ # Scale input
68
+ input_scaled = scaler.transform(input_df)
69
+
70
+ # Step 1: Binary prediction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  binary_pred = model_binary.predict(input_scaled)[0]
72
  if binary_pred == 0:
73
  return "✅ Prediction: No Diabetes Risk (Normal)"
74
 
75
  # Step 2: Multiclass prediction
76
+ multi_pred = model_multiclass.predict(input_scaled)[0]
77
+ status = label_enc.inverse_transform([multi_pred])[0]
78
+
79
+ status_display = "Prediabetes" if status == "prediabetes" else "Diabetes"
80
+ emoji = "⚠️🟡" if status == "prediabetes" else "🚨🔴"
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(
88
  """
89
+ **Developed by Dr. Vinod Kumar Yata's research group**
90
  School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
91
+
92
+ ---
93
+ ⚠️ This AI tool is for **research purposes only**.
94
+ It predicts **diabetes risk** and if at risk, whether it's **prediabetes or diabetes**.
95
+ Please consult a medical professional for any diagnosis.
96
  """
97
  )
98
 
99
  with gr.Row():
100
  with gr.Column():
101
  Age = gr.Number(label="Age", value=30)
102
+ Total_cholesterol = gr.Number(label="Total Cholesterol", value=180)
103
  HDL = gr.Number(label="HDL", value=50)
104
  LDL = gr.Number(label="LDL", value=100)
105
  VLDL = gr.Number(label="VLDL", value=20)
106
  TRIGLYCERIDES = gr.Number(label="TRIGLYCERIDES", value=150)
107
+ before_random_blood_sugar = gr.Number(label="Random Blood Sugar (before control)", value=120)
108
+ before_HbA1c = gr.Number(label="HbA1c (before control)", value=5.5)
109
 
110
  with gr.Column():
111
+ alcohol_consumption = gr.Radio(label="Alcohol Consumption", choices=["0", "1"], value="0")
112
+ family_history_diabetes = gr.Radio(label="Family History of Diabetes", choices=["0", "1"], value="0")
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")
116
+ family_history_cardiovascular_disease = gr.Radio(label="Family History of Cardiovascular Disease", choices=["No", "Yes"], value="No")
117
 
118
+ submit_btn = gr.Button("🧪 Predict")
119
+ output = gr.Textbox(label="Prediction Result")
120
 
121
  submit_btn.click(
122
  fn=predict_diabetes,