high77 commited on
Commit
0007545
·
verified ·
1 Parent(s): ed0e562

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -2,10 +2,13 @@ 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,
@@ -23,16 +26,15 @@ def predict_diabetes(
23
  smoking_status,
24
  family_history_cardiovascular_disease
25
  ):
26
- # Convert radio inputs to numeric
27
  alcohol_consumption = int(alcohol_consumption)
28
  family_history_diabetes = int(family_history_diabetes)
29
 
30
- # One-hot encode categorical features
31
  Gender_FEMALE = 1 if Gender == "Female" else 0
32
  Gender_MALE = 1 if Gender == "Male" else 0
33
 
34
  dietary_non_vegetarian = 1 if dietary_habits == "Non-vegetarian" else 0
35
- dietary_non_vegetarian_dup = 1 if dietary_habits == "Non-vegetarian" else 0 # for duplicate col with space
36
  dietary_vegetarian = 1 if dietary_habits == "Vegetarian" else 0
37
 
38
  smoking_no = 1 if smoking_status == "No" else 0
@@ -41,11 +43,10 @@ def predict_diabetes(
41
  family_history_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
42
  family_history_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
43
 
44
- # Dummy patient ID and binary_status
45
- patient_id = 0
46
- binary_status = 0
47
 
48
- # Match training input order (21 features)
49
  input_data = np.array([[
50
  patient_id,
51
  Age,
@@ -66,29 +67,32 @@ def predict_diabetes(
66
  smoking_no,
67
  smoking_yes,
68
  family_history_cvd_no,
69
- family_history_cvd_yes,
70
- binary_status
71
  ]])
72
 
73
- # Scale input
74
  input_scaled = scaler.transform(input_data)
75
 
76
- # Predict and decode
77
- pred_enc = model.predict(input_scaled)
78
- pred_label = label_enc.inverse_transform(pred_enc)[0]
 
 
 
 
 
79
 
80
- return f"🔍 Predicted Status: {pred_label}"
81
 
 
82
  with gr.Blocks() as demo:
83
- gr.Markdown("# 🩺 Diabetes Predictor")
84
  gr.Markdown(
85
  """
86
  Developed by Dr. Vinod Kumar Yata's research group
87
  School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
88
 
89
- ⚠️ **Warning:**
90
- This is an experimental tool and should not be used for medical diagnosis.
91
- Always consult a licensed healthcare provider for medical advice.
92
  """
93
  )
94
 
 
2
  import numpy as np
3
  import joblib
4
 
5
+ # Load models and encoder
6
+ model_binary = joblib.load("model_binary_cv.pkl")
7
+ model_multiclass = joblib.load("model_multiclass_cv.pkl")
8
+ label_enc = joblib.load("label_encoder_multiclass.pkl")
9
+
10
+ # Define scaler
11
+ scaler = joblib.load("scaler.pkl")
12
 
13
  def predict_diabetes(
14
  Age,
 
26
  smoking_status,
27
  family_history_cardiovascular_disease
28
  ):
29
+ # Convert inputs
30
  alcohol_consumption = int(alcohol_consumption)
31
  family_history_diabetes = int(family_history_diabetes)
32
 
 
33
  Gender_FEMALE = 1 if Gender == "Female" else 0
34
  Gender_MALE = 1 if Gender == "Male" else 0
35
 
36
  dietary_non_vegetarian = 1 if dietary_habits == "Non-vegetarian" else 0
37
+ dietary_non_vegetarian_dup = 1 if dietary_habits == "Non-vegetarian" else 0 # duplicate column
38
  dietary_vegetarian = 1 if dietary_habits == "Vegetarian" else 0
39
 
40
  smoking_no = 1 if smoking_status == "No" else 0
 
43
  family_history_cvd_no = 1 if family_history_cardiovascular_disease == "No" else 0
44
  family_history_cvd_yes = 1 if family_history_cardiovascular_disease == "Yes" else 0
45
 
46
+ patient_id = 0 # Dummy
47
+ # No binary_status in input — it's what we're predicting
 
48
 
49
+ # Construct input feature array for binary model (drop target fields)
50
  input_data = np.array([[
51
  patient_id,
52
  Age,
 
67
  smoking_no,
68
  smoking_yes,
69
  family_history_cvd_no,
70
+ family_history_cvd_yes
 
71
  ]])
72
 
 
73
  input_scaled = scaler.transform(input_data)
74
 
75
+ # Step 1: Binary prediction
76
+ risk_pred = model_binary.predict(input_scaled)[0]
77
+ if risk_pred == 0:
78
+ return "✅ Prediction: No Diabetes Risk (Normal)"
79
+
80
+ # Step 2: Multiclass prediction (only if at risk)
81
+ status_pred_enc = model_multiclass.predict(input_scaled)[0]
82
+ status_pred = label_enc.inverse_transform([status_pred_enc])[0]
83
 
84
+ return f"⚠️ At Risk: {status_pred}"
85
 
86
+ # Gradio Interface
87
  with gr.Blocks() as demo:
88
+ gr.Markdown("# 🩺 Diabetes Risk and Type Predictor")
89
  gr.Markdown(
90
  """
91
  Developed by Dr. Vinod Kumar Yata's research group
92
  School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
93
 
94
+ ⚠️ **Note:** This tool predicts both diabetes **risk** and type.
95
+ Not a substitute for medical advice.
 
96
  """
97
  )
98