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
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import ast
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# Set the option to opt into future behavior
|
| 8 |
+
pd.set_option('future.no_silent_downcasting', True)
|
| 9 |
|
| 10 |
+
# List of options for the dropdown
|
| 11 |
+
workclass_options = sorted(['State-gov', 'Self-emp-not-inc', 'Private', 'Federal-gov', 'Local-gov', 'Self-emp-inc', 'Without-pay'])
|
| 12 |
+
education_option = ['Preschool', '1st-4th', '5th-6th', '7th-8th', '9th', '10th', '11th', '12th', 'HS-grad', 'Some-college', 'Assoc-voc', 'Assoc-acdm', 'Bachelors', 'Masters', 'Prof-school', 'Doctorate']
|
| 13 |
+
marital_status_option = sorted(['Never-married', 'Married-civ-spouse', 'Divorced', 'Separated', 'Married-AF-spouse', 'Widowed', 'Married-spouse-absent'])
|
| 14 |
+
occupation_option = sorted(['Adm-clerical', 'Exec-managerial', 'Handlers-cleaners','Prof-specialty', 'Sales', 'Farming-fishing', 'Machine-op-inspct','Other-service', 'Transport-moving', 'Tech-support','Craft-repair', 'Protective-serv', 'Armed-Forces','Priv-house-serv'])
|
| 15 |
+
relationship_option = sorted(['Not-in-family', 'Husband', 'Wife', 'Own-child', 'Unmarried', 'Other-relative'])
|
| 16 |
+
race_option = sorted(['White', 'Black', 'Other', 'Asian-Pac-Islander', 'Amer-Indian-Eskimo'])
|
| 17 |
+
sex_option = sorted(['Male', 'Female'])
|
| 18 |
+
age = [0, 100]
|
| 19 |
+
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)
|
| 26 |
+
|
| 27 |
+
# List of the columns present in dataframe used to train the model
|
| 28 |
+
columns = ['age', 'education-num', 'sex', 'capital-gain', 'capital-loss',
|
| 29 |
+
'hours-per-week', 'workclass_Local-gov', 'workclass_Private',
|
| 30 |
+
'workclass_Self-emp-inc', 'workclass_Self-emp-not-inc',
|
| 31 |
+
'workclass_State-gov', 'workclass_Without-pay',
|
| 32 |
+
'marital-status_Married-AF-spouse', 'marital-status_Married-civ-spouse',
|
| 33 |
+
'marital-status_Married-spouse-absent', 'marital-status_Never-married',
|
| 34 |
+
'marital-status_Separated', 'marital-status_Widowed',
|
| 35 |
+
'occupation_Armed-Forces', 'occupation_Craft-repair',
|
| 36 |
+
'occupation_Exec-managerial', 'occupation_Farming-fishing',
|
| 37 |
+
'occupation_Handlers-cleaners', 'occupation_Machine-op-inspct',
|
| 38 |
+
'occupation_Other-service', 'occupation_Priv-house-serv',
|
| 39 |
+
'occupation_Prof-specialty', 'occupation_Protective-serv',
|
| 40 |
+
'occupation_Sales', 'occupation_Tech-support',
|
| 41 |
+
'occupation_Transport-moving', 'relationship_Not-in-family',
|
| 42 |
+
'relationship_Other-relative', 'relationship_Own-child',
|
| 43 |
+
'relationship_Unmarried', 'relationship_Wife',
|
| 44 |
+
'race_Asian-Pac-Islander', 'race_Black', 'race_Other', 'race_White']
|
| 45 |
+
|
| 46 |
+
# Code for SVM
|
| 47 |
+
def SVM(workclass, education, marital_status, occupation, relationship, race, sex, age, capital_gain, capital_loss, hours_per_week):
|
| 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-score_scaler_svm_Salary_Classification.pkl', 'rb') as f:
|
| 53 |
+
scaler = pickle.load(f)
|
| 54 |
+
|
| 55 |
+
new_data = {
|
| 56 |
+
'age': age,
|
| 57 |
+
'workclass': workclass,
|
| 58 |
+
'education': education,
|
| 59 |
+
'marital-status': marital_status,
|
| 60 |
+
'occupation': occupation,
|
| 61 |
+
'relationship': relationship,
|
| 62 |
+
'race': race,
|
| 63 |
+
'sex': sex,
|
| 64 |
+
'capital-gain': capital_gain,
|
| 65 |
+
'capital-loss': capital_loss,
|
| 66 |
+
'hours-per-week': hours_per_week,
|
| 67 |
+
}
|
| 68 |
+
new_data = pd.DataFrame([new_data])
|
| 69 |
+
new_data['education'] = new_data['education'].map(education_dict)
|
| 70 |
+
new_data = new_data.rename(columns={'education': 'education-num'})
|
| 71 |
+
|
| 72 |
+
# Create an empty DataFrame with these columns
|
| 73 |
+
formattedDF = pd.DataFrame(columns=columns)
|
| 74 |
+
|
| 75 |
+
# Copying over the continuous columns
|
| 76 |
+
formattedDF['age'] = new_data['age']
|
| 77 |
+
formattedDF['education-num'] = new_data['education-num']
|
| 78 |
+
formattedDF['capital-gain'] = new_data['capital-gain']
|
| 79 |
+
formattedDF['capital-loss'] = new_data['capital-loss']
|
| 80 |
+
formattedDF['hours-per-week'] = new_data['hours-per-week']
|
| 81 |
+
formattedDF['workclass_'+new_data['workclass']] = 1
|
| 82 |
+
formattedDF['marital-status_'+new_data['marital-status']] = 1
|
| 83 |
+
formattedDF['occupation_'+new_data['occupation']] = 1
|
| 84 |
+
formattedDF['relationship_'+new_data['relationship']] = 1
|
| 85 |
+
formattedDF['race_'+new_data['race']] = 1
|
| 86 |
+
formattedDF['sex'] = formattedDF['sex'].apply(lambda x: 1 if x == 'Male' else 0)
|
| 87 |
+
|
| 88 |
+
# Fill remaining columns with 0
|
| 89 |
+
formattedDF.fillna(0, inplace=True)
|
| 90 |
+
formattedDF = formattedDF.astype(int)
|
| 91 |
+
formattedDF = formattedDF[formattedDF.columns.intersection(columns)]
|
| 92 |
+
|
| 93 |
+
# Assuming 'high_skew_columns' from training is a list of columns with high skewness
|
| 94 |
+
for column in ['capital-gain', 'capital-loss']:
|
| 95 |
+
formattedDF[column] = np.log1p(formattedDF[column])
|
| 96 |
+
|
| 97 |
+
# Apply the scaler to the unseen data
|
| 98 |
+
continuous_columns = ['age', 'education-num', 'capital-gain', 'capital-loss', 'hours-per-week']
|
| 99 |
+
formattedDF[continuous_columns] = scaler.transform(formattedDF[continuous_columns])
|
| 100 |
+
|
| 101 |
+
# Make predictions with the loaded model
|
| 102 |
+
prediction = loaded_model.predict(formattedDF)
|
| 103 |
+
|
| 104 |
+
salary_result = '<=50K' if prediction[0] == 0 else '>50K'
|
| 105 |
+
|
| 106 |
+
return "Predicted Salary Class:", salary_result
|
| 107 |
+
|
| 108 |
+
# Code for LogisticRegression
|
| 109 |
+
def LogisticRegression(input_image):
|
| 110 |
+
# Task 2 logic
|
| 111 |
+
return "Task 2 Result"
|
| 112 |
+
|
| 113 |
+
# Code for
|
| 114 |
+
def RandomForests(input_image):
|
| 115 |
+
# Task 2 logic
|
| 116 |
+
return "Task 2 Result"
|
| 117 |
+
|
| 118 |
+
# interface one
|
| 119 |
+
iface1 = gr.Interface(
|
| 120 |
+
fn=SVM,
|
| 121 |
+
inputs=[
|
| 122 |
+
gr.Dropdown(choices=workclass_options, label="Workclass"),
|
| 123 |
+
gr.Dropdown(choices=education_option, label="Education"),
|
| 124 |
+
gr.Dropdown(choices=marital_status_option, label="Marital Status"),
|
| 125 |
+
gr.Dropdown(choices=occupation_option, label="Occupation"),
|
| 126 |
+
gr.Dropdown(choices=relationship_option, label="Relationship"),
|
| 127 |
+
gr.Dropdown(choices=race_option, label="Race"),
|
| 128 |
+
gr.Dropdown(choices=sex_option, label="Sex"),
|
| 129 |
+
gr.Slider(minimum=age[0], maximum=age[1], step=1, label="Age"),
|
| 130 |
+
gr.Slider(minimum=capital_gain[0], maximum=capital_gain[1], step=1, label="Capital Gain"),
|
| 131 |
+
gr.Slider(minimum=capital_loss[0], maximum=capital_loss[1], step=1, label="Capital Loss"),
|
| 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=LogisticRegression,
|
| 141 |
+
inputs="image",
|
| 142 |
+
outputs="text",
|
| 143 |
+
title="Logistic Regression"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# interface two
|
| 147 |
+
iface3 = gr.Interface(
|
| 148 |
+
fn=RandomForests,
|
| 149 |
+
inputs="image",
|
| 150 |
+
outputs="text",
|
| 151 |
+
title="Random Forests"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
demo = gr.TabbedInterface([iface1, iface2, iface3], ["SVM - Jerome Agius", "Logistic Regression - Isaac Muscat", "Random Forests - Kyle Demicoli"])
|
| 155 |
+
|
| 156 |
+
# Run the interface
|
| 157 |
+
demo.launch(share=True)
|