Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,11 +7,12 @@ from sklearn.impute import KNNImputer
|
|
7 |
from sklearn.decomposition import PCA
|
8 |
import pickle
|
9 |
from tensorflow.keras.models import load_model
|
|
|
10 |
|
11 |
|
12 |
|
13 |
# # Define the prediction function
|
14 |
-
def
|
15 |
# columns = {
|
16 |
# "age": [age], "workclass":[workclass], "educational-num":[education], "marital-status":[marital_status], "occupation":[occupation],
|
17 |
# "relationship":[relationship], "race":[race], "gender":[gender], "capital-gain":[capital_gain], "capital-loss":[capital_loss],
|
@@ -32,6 +33,27 @@ def predict(age, workclass, education, marital_status, occupation, relationship,
|
|
32 |
# prediction = 1
|
33 |
return "Income >50K" if prediction == 1 else "Income <=50K"
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def cleaning_features(data,race):
|
36 |
# with open('race_onehot_encoder.pkl', 'rb') as enc_file:
|
37 |
# encoder = pickle.load(enc_file)
|
@@ -135,58 +157,46 @@ def hbdscan_tranform(df_transformed):
|
|
135 |
df_transformed[numerical_features] = scaler.fit_transform(df_transformed[numerical_features])
|
136 |
return df_transformed
|
137 |
|
|
|
|
|
138 |
|
139 |
-
#
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
),
|
168 |
-
gr.Dropdown(
|
169 |
-
["Wife", "Husband", "Own-child", "Unmarried", "Other-relative", "Not-in-family"],
|
170 |
-
label="Relationship"
|
171 |
-
),
|
172 |
-
gr.Dropdown(
|
173 |
-
["White", "Black", "Asian-Pac-Islander", "Amer-Indian-Eskimo", "Other"],
|
174 |
-
label="Race"
|
175 |
-
),
|
176 |
-
gr.Dropdown(
|
177 |
-
["Male", "Female"],
|
178 |
-
label="Gender"
|
179 |
-
),
|
180 |
-
gr.Slider(1, 60, step=1, label="Hours Per Week"),
|
181 |
-
gr.Slider(0, 100000, step=100, label="Capital Gain"),
|
182 |
-
gr.Slider(0, 5000, step=50, label="Capital Loss"),
|
183 |
-
gr.Dropdown(
|
184 |
-
["United-States", "Other"],
|
185 |
-
label="Native Country"
|
186 |
-
)
|
187 |
-
],
|
188 |
outputs="text",
|
189 |
-
title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
)
|
191 |
|
192 |
# Launch the app
|
|
|
7 |
from sklearn.decomposition import PCA
|
8 |
import pickle
|
9 |
from tensorflow.keras.models import load_model
|
10 |
+
import pickle
|
11 |
|
12 |
|
13 |
|
14 |
# # Define the prediction function
|
15 |
+
def predict_ann(age, workclass, education, marital_status, occupation, relationship, race, gender, capital_gain, capital_loss, hours_per_week, native_country):
|
16 |
# columns = {
|
17 |
# "age": [age], "workclass":[workclass], "educational-num":[education], "marital-status":[marital_status], "occupation":[occupation],
|
18 |
# "relationship":[relationship], "race":[race], "gender":[gender], "capital-gain":[capital_gain], "capital-loss":[capital_loss],
|
|
|
33 |
# prediction = 1
|
34 |
return "Income >50K" if prediction == 1 else "Income <=50K"
|
35 |
|
36 |
+
def rf_interface(age, workclass, education, marital_status, occupation, relationship, race, gender, capital_gain, capital_loss, hours_per_week, native_country):
|
37 |
+
# columns = {
|
38 |
+
# "age": [age], "workclass":[workclass], "educational-num":[education], "marital-status":[marital_status], "occupation":[occupation],
|
39 |
+
# "relationship":[relationship], "race":[race], "gender":[gender], "capital-gain":[capital_gain], "capital-loss":[capital_loss],
|
40 |
+
# "hours-per-week":[hours_per_week], "native-country":[native_country]}
|
41 |
+
columns = { "0":[0],
|
42 |
+
"age": [age], "workclass":[workclass], "educational-num":[education], "occupation":[occupation],
|
43 |
+
"race":[race], "gender":[gender], "capital-gain":[capital_gain], "capital-loss":[capital_loss],
|
44 |
+
"hours-per-week":[hours_per_week], "native-country":[native_country]}
|
45 |
+
df = pd.DataFrame(data=columns)
|
46 |
+
fixed_features = cleaning_features(df,race)
|
47 |
+
print(fixed_features)
|
48 |
+
# with open('ann_model.pkl', 'rb') as ann_model_file:
|
49 |
+
# ann_model = pickle.load(ann_model_file)
|
50 |
+
scaler = StandardScaler()
|
51 |
+
X = scaler.fit_transform(fixed_features)
|
52 |
+
rf_model = pickle.load(open('rf_model.pkl', 'rb'))
|
53 |
+
prediction = rf_model.predict(fixed_features)
|
54 |
+
# prediction = 1
|
55 |
+
return "Income >50K" if prediction == 1 else "Income <=50K"
|
56 |
+
|
57 |
def cleaning_features(data,race):
|
58 |
# with open('race_onehot_encoder.pkl', 'rb') as enc_file:
|
59 |
# encoder = pickle.load(enc_file)
|
|
|
157 |
df_transformed[numerical_features] = scaler.fit_transform(df_transformed[numerical_features])
|
158 |
return df_transformed
|
159 |
|
160 |
+
# Launch the app
|
161 |
+
interface.launch()
|
162 |
|
163 |
+
# Shared inputs
|
164 |
+
inputs = [
|
165 |
+
gr.Slider(18, 90, step=1, label="Age"),
|
166 |
+
gr.Dropdown(["Male", "Female"], label="Gender"),
|
167 |
+
gr.Dropdown(["Private", "Self-emp-not-inc", "Self-emp-inc", "Federal-gov", "Local-gov", "State-gov", "Without-pay", "Never-worked"], label="Workclass"),
|
168 |
+
gr.Dropdown(["Preschool", "1st-4th", "5th-6th", "7th-8th", "9th", "10th", "11th", "12th", "HS-grad", "Some-college", "Assoc-voc", "Assoc-acdm", "Bachelors", "Masters", "Doctorate", "Prof-school"], label="Education"),
|
169 |
+
gr.Dropdown(["Married-civ-spouse", "Divorced", "Never-married", "Separated", "Widowed", "Married-spouse-absent", "Married-AF-spouse"], label="Marital Status"),
|
170 |
+
gr.Dropdown(["Tech-support", "Craft-repair", "Other-service", "Sales", "Exec-managerial", "Prof-specialty", "Handlers-cleaners", "Machine-op-inspct", "Adm-clerical", "Farming-fishing", "Transport-moving", "Priv-house-serv", "Protective-serv", "Armed-Forces"], label="Occupation"),
|
171 |
+
gr.Dropdown(["Wife", "Husband", "Own-child", "Not-in-family", "Other-relative", "Unmarried"], label="Relationship"),
|
172 |
+
gr.Dropdown(["White", "Black", "Asian-Pac-Islander", "Amer-Indian-Eskimo", "Other"], label="Race"),
|
173 |
+
gr.Slider(0, 100000, step=100, label="Capital Gain"),
|
174 |
+
gr.Slider(0, 5000, step=50, label="Capital Loss"),
|
175 |
+
gr.Slider(1, 60, step=1, label="Hours Per Week"),
|
176 |
+
gr.Dropdown(["United-States", "Canada", "Mexico", "Other"], label="Native Country")
|
177 |
+
]
|
178 |
+
|
179 |
+
# Interfaces for each model
|
180 |
+
ann_interface = gr.Interface(
|
181 |
+
fn=predict_ann,
|
182 |
+
inputs=inputs,
|
183 |
+
outputs="text",
|
184 |
+
title="Artificial Neural Network",
|
185 |
+
description="Predict income using an Artificial Neural Network."
|
186 |
+
)
|
187 |
+
|
188 |
+
rf_interface = gr.Interface(
|
189 |
+
fn=predict_rf,
|
190 |
+
inputs=inputs,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
outputs="text",
|
192 |
+
title="Random Forest",
|
193 |
+
description="Predict income using a Random Forest model."
|
194 |
+
)
|
195 |
+
|
196 |
+
# Combine the interfaces into tabs
|
197 |
+
interface = gr.TabbedInterface(
|
198 |
+
[ann_interface, rf_interface],
|
199 |
+
["ANN Model", "Random Forest Model"]
|
200 |
)
|
201 |
|
202 |
# Launch the app
|