-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
148 lines (132 loc) · 5.83 KB
/
Copy pathapp.py
File metadata and controls
148 lines (132 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import gradio as gr
import pandas as pd
import os
import joblib
models_folder = "models"
# Load pre-trained models
model_files = {
"Random Forest": "RandomForest.pkl",
"Decision Tree (CART)": "DecisionTree.pkl",
"Support Vector Machine": "SVM.pkl",
"K-Means (Clustering)": "KMeans.pkl",
"Logistic Regression": "LogisticRegression.pkl",
"XGBoost": "XGBoost.pkl",
"Artificial Neural Network (ANN)": "ANN.pkl"
}
models = {
name: joblib.load(os.path.join(models_folder, file))
for name, file in model_files.items()
}
# BMI categorization function
def categorize_bmi(bmi):
if bmi < 18:
return 0
elif 18 <= bmi <= 24:
return 1
elif 25 <= bmi <= 29:
return 2
elif 30 <= bmi <= 39:
return 3
else:
return 4
# Prediction function
def predict_diabetes(
model_name,
HighBP,
HighChol,
CholCheck,
Smoker,
Stroke,
HeartDiseaseorAttack,
PhysActivity,
Fruits,
Veggies,
HvyAlcoholConsump,
AnyHealthcare,
NoDocbcCost,
GenHlth,
MentHlth,
PhysHlth,
DiffWalk,
Sex,
Age,
Education,
Income,
BMI
):
model = models[model_name]
binary_map = {"Yes": 1, "No": 0}
sex_map = {"Male": 1, "Female": 0}
# Create a dataframe from user inputs
input_data = pd.DataFrame([{
"HighBP": binary_map[HighBP],
"HighChol": binary_map[HighChol],
"CholCheck": binary_map[CholCheck],
"Smoker": binary_map[Smoker],
"Stroke": binary_map[Stroke],
"HeartDiseaseorAttack": binary_map[HeartDiseaseorAttack],
"PhysActivity": binary_map[PhysActivity],
"Fruits": binary_map[Fruits],
"Veggies": binary_map[Veggies],
"HvyAlcoholConsump": binary_map[HvyAlcoholConsump],
"AnyHealthcare": binary_map[AnyHealthcare],
"NoDocbcCost": binary_map[NoDocbcCost],
"GenHlth": GenHlth,
"MentHlth": MentHlth,
"PhysHlth": PhysHlth,
"DiffWalk": binary_map[DiffWalk],
"Sex": sex_map[Sex],
"Age": Age,
"Education": Education,
"Income": Income,
"BMI": categorize_bmi(BMI)
}])
# Convert specific columns to categorical types
categorical_columns = ['GenHlth', 'Age', 'BMI', 'Education', 'Income']
for col in categorical_columns:
input_data[col] = input_data[col].astype('category')
# Ensure all necessary columns exist, filling with 0 if missing
expected_columns = pd.read_csv('dataset/diabetes_binary_5050split_health_indicators_BRFSS2015.csv').drop(columns=['Diabetes_binary']).columns
input_data = input_data.reindex(columns=expected_columns, fill_value=0)
# Make prediction
if model_name == "K-Means (Clustering)":
prediction = model.predict(input_data)
prediction = ['At Risk' if p == 1 else 'Not at Risk' for p in prediction]
else:
prediction = model.predict(input_data)
print(prediction)
prediction = ['At Risk' if p == 1 else 'Not at Risk' for p in prediction]
return prediction[0]
# Gradio UI with manual input fields
model_options = list(models.keys())
inputs = [
gr.Dropdown(choices=model_options, label="Select Model", value="Random Forest"),
gr.Radio(choices=["No", "Yes"], label="HighBP", value="No", info="Do you have High Blood Pressure?"),
gr.Radio(choices=["No", "Yes"], label="HighChol", value="No", info="Do you have High Cholestrerol?"),
gr.Radio(choices=["No", "Yes"], label="CholCheck", value="Yes", info="Have you had your cholesterol checked in past 5 years?"),
gr.Radio(choices=["No", "Yes"], label="Smoker", value="No", info="Have you have smoked 100 cigarettes in entire life?"),
gr.Radio(choices=["No", "Yes"], label="Stroke", value="No", info="Ever told you had a stroke?"),
gr.Radio(choices=["No", "Yes"], label="HeartDiseaseorAttack", value="No", info="Have you ever had a heart disease or heart attack?"),
gr.Radio(choices=["No", "Yes"], label="PhysActivity", value="Yes", info="Physical activity in past 30 days?"),
gr.Radio(choices=["No", "Yes"], label="Fruits", value="Yes", info="Consumed fruit 1 or more times per day?"),
gr.Radio(choices=["No", "Yes"], label="Veggies", value="Yes", info="Consumed veggies 1 or more times per day?"),
gr.Radio(choices=["No", "Yes"], label="HvyAlcoholConsump", value="No", info="Adult men >= 14 drinks per week, Adult women >= 7 drinks per week"),
gr.Radio(choices=["No", "Yes"], label="AnyHealthcare", value="Yes", info="Have any kind of healthcare coverage?"),
gr.Radio(choices=["No", "Yes"], label="NoDocbcCost", value="No", info="Any time in past year needed a doctor but could not go because of cost?"),
gr.Slider(1, 5, step=1, label="GenHlth", value=3, info="Rate general health 1-5 (1=excellent and 5=poor)"),
gr.Slider(0, 30, step=1, label="MentHlth", value=0, info="Days of poor mental Health in past 30 days"),
gr.Slider(0, 30, step=1, label="PhysHlth", value=0, info="Days of physcial illness or injury in past 30 days"),
gr.Radio(choices=["No", "Yes"], label="DiffWalk", value="No", info="Difficulty walking"),
gr.Radio(choices=["Male", "Female"], label="Sex", value="Female"),
gr.Slider(1, 13, step=1, label="Age", value=7, info="Scale 1-13, 1=18-24 increments every 5 years 13=80+"),
gr.Slider(1, 6, step=1, label="Education", value=4, info="Highest grate or year of school completed\n1=Never Attended\n2=Elementary\n3=Some Highschool\n4=Graduated Highschool\n5=Some College\n6=Graduated College"),
gr.Slider(1, 8, step=1, label="Income", value=4, info="Scale 1-8 increments every $5k, 1=less than $10k\n5=$20-$35k\n8=$75k+"),
gr.Slider(10, 50, step=1, label="BMI", value=25),
]
gr.Interface(
fn=predict_diabetes,
inputs=inputs,
outputs="text",
title="Diabetes Risk Classification",
description="Enter health indicators to classify diabetes risk using different models."
).launch()