tenzinlodoe commited on
Commit
b75206e
·
verified ·
1 Parent(s): 1f28ffa

Add app.py for income prediction

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import pandas as pd
4
+
5
+ # Load the model
6
+ with open("salar_xgb_team.pkl", "rb") as f:
7
+ model = pickle.load(f)
8
+
9
+ # Define prediction function
10
+ def predict_salary(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
11
+ # sex: "Male" = 0, "Female" = 1
12
+ sex_num = 0 if sex == "Male" else 1
13
+
14
+ df = pd.DataFrame([[age, education_num, sex_num, capital_gain, capital_loss, hours_per_week]],
15
+ columns=['age', 'education-num', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week'])
16
+
17
+ pred = model.predict(df)[0]
18
+ return ">50K" if pred == 1 else "<=50K"
19
+
20
+ # Create Gradio UI
21
+ demo = gr.Interface(
22
+ fn=predict_salary,
23
+ inputs=[
24
+ gr.Number(label="Age"),
25
+ gr.Number(label="Education Level (numeric)"),
26
+ gr.Radio(["Male", "Female"], label="Sex"),
27
+ gr.Number(label="Capital Gain"),
28
+ gr.Number(label="Capital Loss"),
29
+ gr.Number(label="Hours per Week")
30
+ ],
31
+ outputs="text",
32
+ title="Income Prediction App",
33
+ description="Enter demographic and income-related data to predict if salary is >50K or <=50K"
34
+ )
35
+
36
+ demo.launch()