Spaces:
Build error
Build error
Commit ·
3f9a7ec
1
Parent(s): 37f219a
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Importing the libraries
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pickle
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import joblib
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
#using joblib to load the model:
|
| 10 |
+
encoder = joblib.load('encoder.joblib') # loading the encoder
|
| 11 |
+
scaler = joblib.load('scaler.joblib') # loading the scaler
|
| 12 |
+
model = joblib.load('model.joblib') # loading the model
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Create a function that applies the ML pipeline and makes predictions
|
| 16 |
+
def predict(age,gender,education,marital_status,race,employment_stat,wage_per_hour,working_week_per_year,industry_code,occupation_code,
|
| 17 |
+
total_employed,vet_benefit,tax_status,gains,losses,stocks_status,citizenship,mig_year,importance_of_record):
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Create a dataframe with the input data
|
| 22 |
+
input_df = pd.DataFrame({
|
| 23 |
+
'age': [age],
|
| 24 |
+
'gender': [gender],
|
| 25 |
+
'education': [education],
|
| 26 |
+
'marital_status': [marital_status],
|
| 27 |
+
'race': [race],
|
| 28 |
+
'employment_stat': [employment_stat],
|
| 29 |
+
'wage_per_hour': [wage_per_hour],
|
| 30 |
+
'working_week_per_year': [working_week_per_year],
|
| 31 |
+
'industry_code': [industry_code],
|
| 32 |
+
'occupation_code': [occupation_code],
|
| 33 |
+
'total_employed': [total_employed],
|
| 34 |
+
'vet_benefit': [vet_benefit],
|
| 35 |
+
'tax_status': [tax_status],
|
| 36 |
+
'gains': [gains],
|
| 37 |
+
'losses': [losses],
|
| 38 |
+
'stocks_status': [stocks_status],
|
| 39 |
+
'citizenship': [citizenship],
|
| 40 |
+
'mig_year': [mig_year],
|
| 41 |
+
'importance_of_record': [importance_of_record]
|
| 42 |
+
|
| 43 |
+
}) # type: ignore
|
| 44 |
+
|
| 45 |
+
# Create a list with the categorical and numerical columns
|
| 46 |
+
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
| 47 |
+
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
| 48 |
+
|
| 49 |
+
# # Impute the missing values
|
| 50 |
+
# input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
| 51 |
+
# input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
| 52 |
+
|
| 53 |
+
# Encode the categorical columns
|
| 54 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df[cat_columns]).toarray(),
|
| 55 |
+
columns=encoder.get_feature_names_out(cat_columns))
|
| 56 |
+
|
| 57 |
+
# Scale the numerical columns
|
| 58 |
+
input_df_scaled = scaler.transform(input_encoded_df)
|
| 59 |
+
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
#joining the cat encoded and num scaled
|
| 63 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# Make predictions using the model
|
| 67 |
+
predict = model.predict(final_df)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
prediction_label = "INCOME ABOVE LIMIT" if predict.item() == '1' else "INCOME BELOW LIMIT"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
return prediction_label
|
| 74 |
+
|
| 75 |
+
#return predictions
|
| 76 |
+
|
| 77 |
+
#define the input interface
|
| 78 |
+
|
| 79 |
+
input_interface = []
|
| 80 |
+
|
| 81 |
+
with gr.Blocks(css=".gradio-container {background-color:silver}") as app:
|
| 82 |
+
title = gr.Label('INCOME PREDICTION APP.')
|
| 83 |
+
img = gr.Image("income_image.png").style(height= 210 , width= 1250)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
gr.Markdown("This application provides predictions on whether a person earns above or below the income level. Please enter the person's information below and click PREDICT to view the prediction outcome.")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column(scale=4, min_width=500):
|
| 91 |
+
input_interface = [
|
| 92 |
+
gr.components.Number(label="How Old are you?"),
|
| 93 |
+
gr.components.Radio(['male', 'female'], label='What is your Gender?'),
|
| 94 |
+
gr.components.Dropdown(['High School', 'left', 'Undergrad', 'Grad', 'Associate Degree',
|
| 95 |
+
'Doctorate'], label='What is your level of education?'),
|
| 96 |
+
gr.components.Dropdown(['Widowed', 'Single', 'Married', 'Divorced', 'Separated'], label='Marital Status?'),
|
| 97 |
+
gr.components.Dropdown([' White', ' Black', ' Asian or Pacific Islander',
|
| 98 |
+
' Amer Indian Aleut or Eskimo', ' Other'], label='Whats your race?'),
|
| 99 |
+
gr.components.Dropdown([0, 2, 1], label='Whats your emploment status? (0 = Unemployed, 1 = Self-Employed, 2 = Employed)'),
|
| 100 |
+
gr.components.Number(label='How much is your Wage per Hour? (0 - 10000)'),
|
| 101 |
+
gr.components.Number(label='How many weeks have you worked in a year? (1 - 52)'),
|
| 102 |
+
gr.components.Number(label='How many working weeks per year do you work?'),
|
| 103 |
+
gr.components.Number(label='What is your Industry Code? (1 - 51)'),
|
| 104 |
+
gr.components.Number(label='What is your occupation Code? (1 - 46)'),
|
| 105 |
+
gr.components.Number(label='Number of persons working for employer? (1 - 7)'),
|
| 106 |
+
gr.components.Number(label='Benefit? (1 - 3)'),
|
| 107 |
+
gr.components.Dropdown([' Head of household', ' Single', ' Nonfiler', ' Joint both 65+',
|
| 108 |
+
' Joint one 65+ & one under 65', ' Joint one under 65 & one 65+'],label='Whats your tax status?'),
|
| 109 |
+
gr.components.Number(label='What is your Gain'),
|
| 110 |
+
gr.components.Number(label='What is your Loss'),
|
| 111 |
+
gr.components.Number(label='What is your Stock Status'),
|
| 112 |
+
gr.components.Dropdown(['Native', ' Foreign born- Not a citizen of U S ',
|
| 113 |
+
' Foreign born- U S citizen by naturalization',
|
| 114 |
+
' Native- Born abroad of American Parent(s)',
|
| 115 |
+
' Native- Born in U S',' Native- Born in Puerto Rico or U S Outlying'], label='Whats is your Citizenshiip?'),
|
| 116 |
+
gr.components.Radio([94,95], label='Whats your year of migration?'),
|
| 117 |
+
gr.components.Number(label='Whats your Weight Of Instance?')
|
| 118 |
+
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
with gr.Row():
|
| 122 |
+
predict_btn = gr.Button('Predict')
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
# Define the output interfaces
|
| 127 |
+
output_interface = gr.Label(label="INCOME ABOVE LIMIT")
|
| 128 |
+
|
| 129 |
+
predict_btn.click(fn=predict, inputs=input_interface, outputs=output_interface)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
app.launch(share=False)
|