Parthebhan commited on
Commit
3a95fc9
·
verified ·
1 Parent(s): 8cf39e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -35
app.py CHANGED
@@ -7,51 +7,49 @@ model = tf.keras.models.load_model("census.h5")
7
 
8
  # Mapping of categorical variables to encoded values
9
  mapping = {
10
- 'workclass': {' ?': 0, ' Federal-gov': 1, ' Local-gov': 2, ' Never-worked': 3, ' Private': 4, ' Self-emp-inc': 5, ' Self-emp-not-inc': 6, ' State-gov': 7, ' Without-pay': 8},
11
- 'education': {' 10th': 0, ' 11th': 1, ' 12th': 2, ' 1st-4th': 3, ' 5th-6th': 4, ' 7th-8th': 5, ' 9th': 6, ' Assoc-acdm': 7, ' Assoc-voc': 8, ' Bachelors': 9, ' Doctorate': 10, ' HS-grad': 11, ' Masters': 12, ' Preschool': 13, ' Prof-school': 14, ' Some-college': 15},
12
- 'marital_status': {' Divorced': 0, ' Married-AF-spouse': 1, ' Married-civ-spouse': 2, ' Married-spouse-absent': 3, ' Never-married': 4, ' Separated': 5, ' Widowed': 6},
13
- 'occupation': {' ?': 0, ' Adm-clerical': 1, ' Armed-Forces': 2, ' Craft-repair': 3, ' Exec-managerial': 4, ' Farming-fishing': 5, ' Handlers-cleaners': 6, ' Machine-op-inspct': 7, ' Other-service': 8, ' Priv-house-serv': 9, ' Prof-specialty': 10, ' Protective-serv': 11, ' Sales': 12, ' Tech-support': 13, ' Transport-moving': 14},
14
- 'relationship': {' Husband': 0, ' Not-in-family': 1, ' Other-relative': 2, ' Own-child': 3, ' Unmarried': 4, ' Wife': 5},
15
- 'race': {' Amer-Indian-Eskimo': 0, ' Asian-Pac-Islander': 1, ' Black': 2, ' Other': 3, ' White': 4},
16
- 'gender': {' Female': 0, ' Male': 1},
17
  'native_country': {' ?': 0, ' Cambodia': 1, ' Canada': 2, ' China': 3, ' Columbia': 4, ' Cuba': 5, ' Dominican-Republic': 6, ' Ecuador': 7, ' El-Salvador': 8, ' England': 9, ' France': 10, ' Germany': 11, ' Greece': 12, ' Guatemala': 13, ' Haiti': 14, ' Honduras': 15, ' Hong': 16, ' Hungary': 17, ' India': 18, ' Iran': 19, ' Ireland': 20, ' Italy': 21, ' Jamaica': 22, ' Japan': 23, ' Laos': 24, ' Mexico': 25, ' Nicaragua': 26, ' Outlying-US(Guam-USVI-etc)': 27, ' Peru': 28, ' Philippines': 29, ' Poland': 30, ' Portugal': 31, ' Puerto-Rico': 32, ' Scotland': 33, ' South': 34, ' Taiwan': 35, ' Thailand': 36, ' Trinadad&Tobago': 37, ' United-States': 38, ' Vietnam': 39, ' Yugoslavia': 40}
18
  }
19
 
20
  # Define the function for making predictions
21
- def salarybracket(age, workclass, education, education_num, marital_status, occupation, relationship, race, gender, capital_gain, capital_loss, hours_per_week, native_country):
22
- inputs = np.array([[age, workclass, education, education_num, marital_status, occupation, relationship, race, gender, capital_gain, capital_loss, hours_per_week, native_country]])
23
  prediction = model.predict(inputs)
24
  prediction_value = prediction[0][0] # Assuming the prediction is a scalar
25
  result = "Income_bracket lesser than or equal to 50K ⬇️" if prediction_value <= 0.5 else "Income_bracket greater than 50K ⬆️"
26
  return f"{result}"
27
 
28
- # Convert mapping to markdown table
29
- markdown_table = "| Column | Category | Encoded Value |\n|--------|----------|---------------|\n"
30
- for column, categories in mapping.items():
31
- for category, value in categories.items():
32
- markdown_table += f"| {column} | {category} | {value} |\n"
 
 
33
 
34
  # Create the Gradio interface
35
  salarybracket_ga = gr.Interface(fn=salarybracket,
36
- inputs=[
37
- gr.inputs.Slider(17, 90, label="Age"),
38
- gr.inputs.Dropdown(list(mapping['workclass'].keys()), label="Workclass"),
39
- gr.inputs.Dropdown(list(mapping['education'].keys()), label="Education"),
40
- gr.inputs.Slider(1, 16, label="Education Num"),
41
- gr.inputs.Dropdown(list(mapping['marital_status'].keys()), label="Marital Status"),
42
- gr.inputs.Dropdown(list(mapping['occupation'].keys()), label="Occupation"),
43
- gr.inputs.Dropdown(list(mapping['relationship'].keys()), label="Relationship"),
44
- gr.inputs.Dropdown(list(mapping['race'].keys()), label="Race"),
45
- gr.inputs.Dropdown(list(mapping['gender'].keys()), label="Gender"),
46
- gr.inputs.Slider(0, 99999, label="Capital Gain"),
47
- gr.inputs.Slider(0, 4356, label="Capital Loss"),
48
- gr.inputs.Slider(1, 99, label="Hours per Week"),
49
- gr.inputs.Dropdown(list(mapping['native_country'].keys()), label="Native Country"),
50
- ],
51
- outputs="text",
52
- title="Salary Bracket Prediction - Income <=50k or >50K ",
53
- description=f"Predicting Income_bracket Prediction Using TensorFlow\n\n### Mapping of Categorical Variables\n{markdown_table}",
54
- theme='dark'
55
- )
56
 
57
- salarybracket_ga.launch(share=True, debug=True)
 
7
 
8
  # Mapping of categorical variables to encoded values
9
  mapping = {
10
+ 'workclass': {' ?': 0, ' Federal-gov': 1, ' Local-gov': 2, ' Never-worked': 3, ' Private': 4, ' Self-emp-inc': 5, ' Self-emp-not-inc': 6, ' State-gov': 7, ' Without-pay': 8},
11
+ 'education': {' 10th': 0, ' 11th': 1, ' 12th': 2, ' 1st-4th': 3, ' 5th-6th': 4, ' 7th-8th': 5, ' 9th': 6, ' Assoc-acdm': 7, ' Assoc-voc': 8, ' Bachelors': 9, ' Doctorate': 10, ' HS-grad': 11, ' Masters': 12, ' Preschool': 13, ' Prof-school': 14, ' Some-college': 15},
12
+ 'marital_status': {' Divorced': 0, ' Married-AF-spouse': 1, ' Married-civ-spouse': 2, ' Married-spouse-absent': 3, ' Never-married': 4, ' Separated': 5, ' Widowed': 6},
13
+ 'occupation': {' ?': 0, ' Adm-clerical': 1, ' Armed-Forces': 2, ' Craft-repair': 3, ' Exec-managerial': 4, ' Farming-fishing': 5, ' Handlers-cleaners': 6, ' Machine-op-inspct': 7, ' Other-service': 8, ' Priv-house-serv': 9, ' Prof-specialty': 10, ' Protective-serv': 11, ' Sales': 12, ' Tech-support': 13, ' Transport-moving': 14},
14
+ 'relationship': {' Husband': 0, ' Not-in-family': 1, ' Other-relative': 2, ' Own-child': 3, ' Unmarried': 4, ' Wife': 5},
15
+ 'race': {' Amer-Indian-Eskimo': 0, ' Asian-Pac-Islander': 1, ' Black': 2, ' Other': 3, ' White': 4},
16
+ 'gender': {' Female': 0, ' Male': 1},
17
  'native_country': {' ?': 0, ' Cambodia': 1, ' Canada': 2, ' China': 3, ' Columbia': 4, ' Cuba': 5, ' Dominican-Republic': 6, ' Ecuador': 7, ' El-Salvador': 8, ' England': 9, ' France': 10, ' Germany': 11, ' Greece': 12, ' Guatemala': 13, ' Haiti': 14, ' Honduras': 15, ' Hong': 16, ' Hungary': 17, ' India': 18, ' Iran': 19, ' Ireland': 20, ' Italy': 21, ' Jamaica': 22, ' Japan': 23, ' Laos': 24, ' Mexico': 25, ' Nicaragua': 26, ' Outlying-US(Guam-USVI-etc)': 27, ' Peru': 28, ' Philippines': 29, ' Poland': 30, ' Portugal': 31, ' Puerto-Rico': 32, ' Scotland': 33, ' South': 34, ' Taiwan': 35, ' Thailand': 36, ' Trinadad&Tobago': 37, ' United-States': 38, ' Vietnam': 39, ' Yugoslavia': 40}
18
  }
19
 
20
  # Define the function for making predictions
21
+ def salarybracket(age, workclass, education, marital_status, occupation, relationship, race, gender, native_country):
22
+ inputs = np.array([[age, workclass, education, marital_status, occupation, relationship, race, gender, native_country]])
23
  prediction = model.predict(inputs)
24
  prediction_value = prediction[0][0] # Assuming the prediction is a scalar
25
  result = "Income_bracket lesser than or equal to 50K ⬇️" if prediction_value <= 0.5 else "Income_bracket greater than 50K ⬆️"
26
  return f"{result}"
27
 
28
+ # Convert mapping to Gradio dropdown options
29
+ dropdown_options = {}
30
+ for column, values in mapping.items():
31
+ options = []
32
+ for label, value in values.items():
33
+ options.append({"label": label, "value": value})
34
+ dropdown_options[column] = options
35
 
36
  # Create the Gradio interface
37
  salarybracket_ga = gr.Interface(fn=salarybracket,
38
+ inputs=[
39
+ gr.Slider(17, 90, label="Age"),
40
+ gr.Dropdown(dropdown_options['workclass'], label="Workclass"),
41
+ gr.Dropdown(dropdown_options['education'], label="Education"),
42
+ gr.Dropdown(dropdown_options['marital_status'], label="Marital Status"),
43
+ gr.Dropdown(dropdown_options['occupation'], label="Occupation"),
44
+ gr.Dropdown(dropdown_options['relationship'], label="Relationship"),
45
+ gr.Dropdown(dropdown_options['race'], label="Race"),
46
+ gr.Dropdown(dropdown_options['gender'], label="Gender"),
47
+ gr.Dropdown(dropdown_options['native_country'], label="Native Country")
48
+ ],
49
+ outputs="text",
50
+ title="Salary Bracket Prediction",
51
+ description="Predicting Income Bracket Using TensorFlow",
52
+ theme='dark'
53
+ )
 
 
 
 
54
 
55
+ salarybracket_ga.launch(share=True, debug=True)