high77 commited on
Commit
acc6a60
·
verified ·
1 Parent(s): ea8ad25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -55
app.py CHANGED
@@ -1,63 +1,117 @@
1
  import gradio as gr
2
- import joblib
3
  import numpy as np
4
- import pandas as pd
5
 
6
- # Load saved artifacts
7
- model = joblib.load('best_multiclass_model.pkl')
8
- scaler = joblib.load('feature_scaler.pkl')
9
  label_enc = joblib.load('label_encoder.pkl')
10
 
11
- # List of feature names in correct order (exclude Patient Id and target)
12
- feature_names = [
13
- 'Age', 'Total cholesterol', 'HDL', 'LDL', 'VLDL', 'TRIGLYCERIDES',
14
- 'before glycemic control random blood sugar', 'before glycemic control HbA1c',
15
- 'alcohol consumption', 'family_history_diabetes', 'Gender_FEMALE', 'Gender_MALE',
16
- 'dietary habits_non-vegetarian', 'dietary habits_non-vegetarian ', 'dietary habits_vegetarian',
17
- 'smoking status_no', 'smoking status_yes',
18
- 'family_history_cardiovascular_disease_no', 'family_history_cardiovascular_disease_yes'
19
- ]
20
-
21
- # Function to predict diabetes status
22
- def predict_diabetes(*inputs):
23
- # Convert inputs to dataframe
24
- input_df = pd.DataFrame([inputs], columns=feature_names)
25
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Scale input features
27
- scaled_features = scaler.transform(input_df)
28
-
29
- # Predict label index
30
- pred_encoded = model.predict(scaled_features)[0]
31
-
32
- # Decode label to original class
33
- pred_label = label_enc.inverse_transform([pred_encoded])[0]
34
-
35
- return pred_label
36
-
37
- # Define Gradio input components
38
- inputs = [
39
- gr.Number(label=feature) for feature in feature_names
40
- ]
41
-
42
- # Gradio interface
43
- title = "Diabetes Predictor"
44
- description = """
45
- Developed by Dr. Vinod Kumar Yata's research group
46
- School of Allied and Healthcare Sciences, Malla Reddy University, Hyderabad, India
47
-
48
- ⚠️ Warning:
49
- This is an experimental tool and should not be used for medical diagnosis.
50
- Always consult a licensed healthcare provider for medical advice.
51
- """
52
-
53
- iface = gr.Interface(
54
- fn=predict_diabetes,
55
- inputs=inputs,
56
- outputs=gr.Textbox(label="Predicted Diabetes Status"),
57
- title=title,
58
- description=description,
59
- theme="default"
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- iface.launch()
 
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()