Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,11 @@ capital_gain = [0, 99999]
|
|
| 20 |
capital_loss = [0, 4356]
|
| 21 |
hours_per_week = [20, 60]
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Mapping for education
|
| 24 |
education_mapping = "{'Preschool': 1, '1st-4th': 2, '5th-6th': 3, '7th-8th': 4, '9th': 5, '10th': 6, '11th': 7, '12th': 8, 'HS-grad': 9, 'Some-college': 10, 'Assoc-voc': 11, 'Assoc-acdm': 12, 'Bachelors': 13, 'Masters': 14, 'Prof-school': 15, 'Doctorate': 16}"
|
| 25 |
education_dict = ast.literal_eval(education_mapping)
|
|
@@ -44,12 +49,12 @@ columns = ['age', 'education-num', 'sex', 'capital-gain', 'capital-loss',
|
|
| 44 |
'race_Asian-Pac-Islander', 'race_Black', 'race_Other', 'race_White']
|
| 45 |
|
| 46 |
# Code for SVM
|
| 47 |
-
def
|
| 48 |
-
with open('models/best_svm_OvM_Salary_Classification.pkl', 'rb') as f:
|
| 49 |
loaded_model = pickle.load(f)
|
| 50 |
|
| 51 |
# Loading the scaler and transform the data
|
| 52 |
-
with open('models/z-
|
| 53 |
scaler = pickle.load(f)
|
| 54 |
|
| 55 |
new_data = {
|
|
@@ -105,19 +110,85 @@ def SVM(workclass, education, marital_status, occupation, relationship, race, se
|
|
| 105 |
|
| 106 |
return "Predicted Salary Class:", salary_result
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# Code for LogisticRegression
|
| 109 |
-
def
|
| 110 |
# Task 2 logic
|
| 111 |
return "Task 2 Result"
|
| 112 |
|
| 113 |
# Code for
|
| 114 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Task 2 logic
|
| 116 |
return "Task 2 Result"
|
| 117 |
|
| 118 |
# interface one
|
| 119 |
iface1 = gr.Interface(
|
| 120 |
-
fn=
|
| 121 |
inputs=[
|
| 122 |
gr.Dropdown(choices=workclass_options, label="Workclass"),
|
| 123 |
gr.Dropdown(choices=education_option, label="Education"),
|
|
@@ -132,26 +203,59 @@ iface1 = gr.Interface(
|
|
| 132 |
gr.Slider(minimum=hours_per_week[0], maximum=hours_per_week[1], step=1, label="Hours per Week"),
|
| 133 |
],
|
| 134 |
outputs="text",
|
| 135 |
-
title="SVM"
|
| 136 |
)
|
| 137 |
|
| 138 |
# interface two
|
| 139 |
iface2 = gr.Interface(
|
| 140 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
inputs="image",
|
| 142 |
outputs="text",
|
| 143 |
title="Logistic Regression"
|
| 144 |
)
|
| 145 |
|
| 146 |
-
# interface
|
| 147 |
-
|
| 148 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
inputs="image",
|
| 150 |
outputs="text",
|
| 151 |
title="Random Forests"
|
| 152 |
)
|
| 153 |
|
| 154 |
-
demo = gr.TabbedInterface([iface1, iface2, iface3], ["SVM - Jerome Agius", "
|
|
|
|
|
|
|
| 155 |
|
| 156 |
# Run the interface
|
| 157 |
demo.launch(share=True)
|
|
|
|
| 20 |
capital_loss = [0, 4356]
|
| 21 |
hours_per_week = [20, 60]
|
| 22 |
|
| 23 |
+
children_count = [0, 15]
|
| 24 |
+
bmi = [10, 100]
|
| 25 |
+
region_option = ['southwest', 'southeast', 'northwest', 'northeast']
|
| 26 |
+
smoker_option = ['yes', 'no']
|
| 27 |
+
|
| 28 |
# Mapping for education
|
| 29 |
education_mapping = "{'Preschool': 1, '1st-4th': 2, '5th-6th': 3, '7th-8th': 4, '9th': 5, '10th': 6, '11th': 7, '12th': 8, 'HS-grad': 9, 'Some-college': 10, 'Assoc-voc': 11, 'Assoc-acdm': 12, 'Bachelors': 13, 'Masters': 14, 'Prof-school': 15, 'Doctorate': 16}"
|
| 30 |
education_dict = ast.literal_eval(education_mapping)
|
|
|
|
| 49 |
'race_Asian-Pac-Islander', 'race_Black', 'race_Other', 'race_White']
|
| 50 |
|
| 51 |
# Code for SVM
|
| 52 |
+
def SVM_Salary(workclass, education, marital_status, occupation, relationship, race, sex, age, capital_gain, capital_loss, hours_per_week):
|
| 53 |
+
with open('../SVM/models/best_svm_OvM_Salary_Classification.pkl', 'rb') as f:
|
| 54 |
loaded_model = pickle.load(f)
|
| 55 |
|
| 56 |
# Loading the scaler and transform the data
|
| 57 |
+
with open('../SVM/models/z-score_scaler_svm_salary_classification.pkl', 'rb') as f:
|
| 58 |
scaler = pickle.load(f)
|
| 59 |
|
| 60 |
new_data = {
|
|
|
|
| 110 |
|
| 111 |
return "Predicted Salary Class:", salary_result
|
| 112 |
|
| 113 |
+
def SVM_Health(age, sex, bmi, children, smoker, region):
|
| 114 |
+
with open('models/best_health_svm_OvM_Charges_Classification.pkl', 'rb') as f:
|
| 115 |
+
loaded_model = pickle.load(f)
|
| 116 |
+
|
| 117 |
+
# Loading the scaler and transform the data
|
| 118 |
+
with open('models/z-score_scaler_svm_charges_classification.pkl', 'rb') as f:
|
| 119 |
+
scaler = pickle.load(f)
|
| 120 |
+
|
| 121 |
+
#Inverting the dict to map the 'charges' values back to 'charges' labels
|
| 122 |
+
inverse_mapping_charges = {
|
| 123 |
+
0: 'Very Low (<= 5000)',
|
| 124 |
+
1: 'Low (5001 - 10000)',
|
| 125 |
+
2: 'Moderate (10001 - 15000)',
|
| 126 |
+
3: 'High (15001 - 20000)',
|
| 127 |
+
4: 'Very High (> 20001)',
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
new_data = {
|
| 131 |
+
'age': age,
|
| 132 |
+
'sex': sex,
|
| 133 |
+
'bmi': bmi,
|
| 134 |
+
'children': children,
|
| 135 |
+
'smoker': smoker,
|
| 136 |
+
'region': region,
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
new_data = pd.DataFrame([new_data])
|
| 140 |
+
|
| 141 |
+
# Create an empty DataFrame with these columns
|
| 142 |
+
formattedDF = pd.DataFrame(columns=columns)
|
| 143 |
+
|
| 144 |
+
# Copying over the continuous columns
|
| 145 |
+
formattedDF['age'] = new_data['age']
|
| 146 |
+
formattedDF['sex'] = new_data['sex'].apply(lambda x: 1 if x == 'Male' else 0)
|
| 147 |
+
formattedDF['bmi'] = new_data['bmi']
|
| 148 |
+
formattedDF['children'] = new_data['children']
|
| 149 |
+
formattedDF['smoker'] = new_data['smoker'].apply(lambda x: 1 if x == 'Yes' else 0)
|
| 150 |
+
formattedDF['marital-status_'+new_data['marital-status']] = 1
|
| 151 |
+
formattedDF['region_'+new_data['region']] = 1
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# Fill remaining columns with 0
|
| 155 |
+
formattedDF.fillna(0, inplace=True)
|
| 156 |
+
formattedDF = formattedDF.astype(int)
|
| 157 |
+
formattedDF = formattedDF[formattedDF.columns.intersection(columns)]
|
| 158 |
+
|
| 159 |
+
# Apply the scaler to the unseen data
|
| 160 |
+
continuous_columns = ['age', 'bmi']
|
| 161 |
+
formattedDF[continuous_columns] = scaler.transform(formattedDF[continuous_columns])
|
| 162 |
+
|
| 163 |
+
# Make predictions with the loaded model
|
| 164 |
+
prediction = loaded_model.predict(formattedDF)[0]
|
| 165 |
+
prediction = inverse_mapping_charges[prediction]
|
| 166 |
+
|
| 167 |
+
return "Predicted Charges Class:", prediction
|
| 168 |
+
|
| 169 |
+
# Code for LogisticRegression
|
| 170 |
+
def LogisticRegression_Salary(input_image):
|
| 171 |
+
# Task 2 logic
|
| 172 |
+
return "Task 2 Result"
|
| 173 |
+
|
| 174 |
# Code for LogisticRegression
|
| 175 |
+
def LogisticRegression_Health(input_image):
|
| 176 |
# Task 2 logic
|
| 177 |
return "Task 2 Result"
|
| 178 |
|
| 179 |
# Code for
|
| 180 |
+
def RandomForests_Salary(input_image):
|
| 181 |
+
# Task 2 logic
|
| 182 |
+
return "Task 2 Result"
|
| 183 |
+
|
| 184 |
+
# Code for
|
| 185 |
+
def RandomForests_Health(input_image):
|
| 186 |
# Task 2 logic
|
| 187 |
return "Task 2 Result"
|
| 188 |
|
| 189 |
# interface one
|
| 190 |
iface1 = gr.Interface(
|
| 191 |
+
fn=SVM_Salary,
|
| 192 |
inputs=[
|
| 193 |
gr.Dropdown(choices=workclass_options, label="Workclass"),
|
| 194 |
gr.Dropdown(choices=education_option, label="Education"),
|
|
|
|
| 203 |
gr.Slider(minimum=hours_per_week[0], maximum=hours_per_week[1], step=1, label="Hours per Week"),
|
| 204 |
],
|
| 205 |
outputs="text",
|
| 206 |
+
title="SVM - Salary"
|
| 207 |
)
|
| 208 |
|
| 209 |
# interface two
|
| 210 |
iface2 = gr.Interface(
|
| 211 |
+
fn=SVM_Health,
|
| 212 |
+
inputs=[
|
| 213 |
+
gr.Slider(minimum=age[0], maximum=age[1], step=1, label="Age"),
|
| 214 |
+
gr.Dropdown(choices=sex_option, label="Sex"),
|
| 215 |
+
gr.Slider(minimum=bmi[0], maximum=bmi[1], step=0.1, label="BMI"),
|
| 216 |
+
gr.Slider(minimum=children_count[0], maximum=children_count[1], step=1, label="Children"),
|
| 217 |
+
gr.Dropdown(choices=smoker_option, label="Smoker"),
|
| 218 |
+
gr.Dropdown(choices=region_option, label="Region"),
|
| 219 |
+
],
|
| 220 |
+
outputs="text",
|
| 221 |
+
title="SVM - Health"
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# interface three
|
| 225 |
+
iface3 = gr.Interface(
|
| 226 |
+
fn=LogisticRegression_Salary,
|
| 227 |
inputs="image",
|
| 228 |
outputs="text",
|
| 229 |
title="Logistic Regression"
|
| 230 |
)
|
| 231 |
|
| 232 |
+
# interface four
|
| 233 |
+
iface4 = gr.Interface(
|
| 234 |
+
fn=LogisticRegression_Health,
|
| 235 |
+
inputs="image",
|
| 236 |
+
outputs="text",
|
| 237 |
+
title="Logistic Regression"
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# interface five
|
| 241 |
+
iface5 = gr.Interface(
|
| 242 |
+
fn=RandomForests_Salary,
|
| 243 |
+
inputs="image",
|
| 244 |
+
outputs="text",
|
| 245 |
+
title="Random Forests"
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# interface six
|
| 249 |
+
iface6 = gr.Interface(
|
| 250 |
+
fn=RandomForests_Health,
|
| 251 |
inputs="image",
|
| 252 |
outputs="text",
|
| 253 |
title="Random Forests"
|
| 254 |
)
|
| 255 |
|
| 256 |
+
demo = gr.TabbedInterface([iface1, iface2, iface3, iface4, iface5, iface6], ["SVM - Jerome Agius", "SVM - Jerome Agius",
|
| 257 |
+
"Logistic Regression - Isaac Muscat", "Logistic Regression - Isaac Muscat",
|
| 258 |
+
"Random Forests - Kyle Demicoli", "Random Forests - Kyle Demicoli"])
|
| 259 |
|
| 260 |
# Run the interface
|
| 261 |
demo.launch(share=True)
|