rasmodev commited on
Commit
e2f83d5
·
1 Parent(s): e47898e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -98
app.py CHANGED
@@ -48,106 +48,40 @@ st.sidebar.markdown("**Year of Migration**: Enter the year of migration for the
48
  st.sidebar.markdown("**Country of Birth**: Choose the individual's birth country (e.g., United-States, Other).")
49
  st.sidebar.markdown("**Importance of Record**: Enter the weight of the instance (numeric value, e.g., 0.9).")
50
 
51
- # Create the input fields in the order of your DataFrame
52
- input_data = {
53
- 'age': 0, # Default values, you can change these as needed
54
- 'gender': unique_values['gender'][0],
55
- 'education': unique_values['education'][0],
56
- 'worker_class': unique_values['worker_class'][0],
57
- 'marital_status': unique_values['marital_status'][0],
58
- 'race': unique_values['race'][0],
59
- 'is_hispanic': unique_values['is_hispanic'][0],
60
- 'employment_commitment': unique_values['employment_commitment'][0],
61
- 'employment_stat': unique_values['employment_stat'][0],
62
- 'wage_per_hour': 0, # Default value
63
- 'working_week_per_year': 0, # Default value
64
- 'industry_code': 0, # Default value
65
- 'industry_code_main': unique_values['industry_code_main'][0],
66
- 'occupation_code': 0, # Default value
67
- 'occupation_code_main': unique_values['occupation_code_main'][0],
68
- 'total_employed': 0, # Default value
69
- 'household_stat': unique_values['household_stat'][0],
70
- 'household_summary': unique_values['household_summary'][0],
71
- 'vet_benefit': 0, # Default value
72
- 'tax_status': unique_values['tax_status'][0],
73
- 'gains': 0, # Default value
74
- 'losses': 0, # Default value
75
- 'stocks_status': 0, # Default value
76
- 'citizenship': unique_values['citizenship'][0],
77
- 'mig_year': 0,
78
- 'country_of_birth_own': 'United-States',
79
- 'importance_of_record': 0.0 # Default value
80
- }
81
-
82
- # Create the input fields
83
  col1, col2, col3 = st.columns(3)
84
 
85
  with col1:
86
- input_data['age'] = st.number_input("Age", min_value=0, key='age')
87
- input_data['gender'] = st.selectbox("Gender", unique_values['gender'], key='gender')
88
- input_data['education'] = st.selectbox("Education", unique_values['education'], key='education')
89
- input_data['worker_class'] = st.selectbox("Class of Worker", unique_values['worker_class'], key='worker_class')
90
- input_data['marital_status'] = st.selectbox("Marital Status", unique_values['marital_status'], key='marital_status')
91
- input_data['race'] = st.selectbox("Race", unique_values['race'], key='race')
92
- input_data['is_hispanic'] = st.selectbox("Hispanic Origin", unique_values['is_hispanic'], key='is_hispanic')
93
- input_data['employment_commitment'] = st.selectbox("Full/Part-Time Employment", unique_values['employment_commitment'], key='employment_commitment')
94
- input_data['employment_stat'] = st.selectbox("Has Own Business Or Is Self Employed", unique_values['employment_stat'], key='employment_stat')
95
- input_data['wage_per_hour'] = st.number_input("Wage Per Hour", min_value=0, key='wage_per_hour')
96
 
97
  with col2:
98
- input_data['working_week_per_year'] = st.number_input("Weeks Worked Per Year", min_value=0, key='working_week_per_year')
99
- input_data['industry_code'] = st.selectbox("Category Code of Industry", unique_values['industry_code'], key='industry_code')
100
- input_data['industry_code_main'] = st.selectbox("Major Industry Code", unique_values['industry_code_main'], key='industry_code_main')
101
- input_data['occupation_code'] = st.selectbox("Category Code of Occupation", unique_values['occupation_code'], key='occupation_code')
102
- input_data['occupation_code_main'] = st.selectbox("Major Occupation Code", unique_values['occupation_code_main'], key='occupation_code_main')
103
- input_data['total_employed'] = st.number_input("Number of Persons Worked for Employer", min_value=0, key='total_employed')
104
- input_data['household_stat'] = st.selectbox("Detailed Household and Family Status", unique_values['household_stat'], key='household_stat')
105
- input_data['household_summary'] = st.selectbox("Detailed Household Summary", unique_values['household_summary'], key='household_summary')
106
- input_data['vet_benefit'] = st.selectbox("Veteran Benefits", unique_values['vet_benefit'], key='vet_benefit')
107
 
108
  with col3:
109
- input_data['tax_status'] = st.selectbox("Tax Filer Status", unique_values['tax_status'], key='tax_status')
110
- input_data['gains'] = st.number_input("Gains", min_value=0, key='gains')
111
- input_data['losses'] = st.number_input("Losses", min_value=0, key='losses')
112
- input_data['stocks_status'] = st.number_input("Dividends from Stocks", min_value=0, key='stocks_status')
113
- input_data['citizenship'] = st.selectbox("Citizenship", unique_values['citizenship'], key='citizenship')
114
- input_data['mig_year'] = st.selectbox("Migration Year", unique_values['mig_year'], key='migration_year')
115
- input_data['country_of_birth_own'] = st.selectbox("Country of Birth", unique_values['country_of_birth_own'], key='country_of_birth_own')
116
- input_data['importance_of_record'] = st.number_input("Importance of Record", min_value=0, key='importance_of_record')
117
-
118
- # Button to make predictions
119
- if st.button("Predict"):
120
- # Transform the input data to a DataFrame for prediction
121
- input_df = pd.DataFrame([input_data])
122
-
123
- # Make predictions
124
- prediction = dt_model.predict(input_df)
125
- prediction_proba = dt_model.predict_proba(input_df)
126
-
127
- # Display prediction result
128
- st.subheader("Prediction")
129
- if prediction[0] == 1:
130
- st.success("This individual is predicted to have an income of over $50K.")
131
- else:
132
- st.error("This individual is predicted to have an income of under $50K")
133
-
134
- # Show prediction probability
135
- st.subheader("Prediction Probability")
136
- st.write(f"The probability of the individual having an income over $50K is: {prediction_proba[0][1]:.2f}")
137
-
138
-
139
-
140
- import streamlit as st
141
- import requests
142
-
143
- # Streamlit UI for user input
144
- st.title("Income Prediction App")
145
- st.sidebar.header("Enter User Information")
146
-
147
- # Create input fields for user input
148
- age = st.number_input("Age", min_value=0)
149
- gender = st.selectbox("Gender", ["Male", "Female"])
150
- # Include other input fields...
151
 
152
  # Button to trigger prediction
153
  if st.button("Predict"):
@@ -155,18 +89,50 @@ if st.button("Predict"):
155
  user_input = {
156
  "age": age,
157
  "gender": gender,
158
- # Include other input fields...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  }
160
-
161
  # Send a POST request to the FastAPI server
162
- response = requests.post("http://fastapi-server:8000/predict/", json=user_input)
163
 
164
  # Check if the request was successful
165
  if response.status_code == 200:
166
  prediction_data = response.json()
 
167
  # Display prediction result to the user
168
  st.subheader("Prediction Result")
169
- st.write(f"Income: {prediction_data['income_prediction']}")
170
- st.write(f"Prediction Probability: {prediction_data['prediction_probability']:.2f}")
 
 
 
 
 
 
 
 
 
171
  else:
172
  st.error("Error: Unable to get prediction")
 
48
  st.sidebar.markdown("**Country of Birth**: Choose the individual's birth country (e.g., United-States, Other).")
49
  st.sidebar.markdown("**Importance of Record**: Enter the weight of the instance (numeric value, e.g., 0.9).")
50
 
51
+ # Create input fields for user input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  col1, col2, col3 = st.columns(3)
53
 
54
  with col1:
55
+ age = st.number_input("Age", min_value=0)
56
+ gender = st.selectbox("Gender", ["Male", "Female"])
57
+ education = st.selectbox("Education", unique_values['education'])
58
+ worker_class = st.selectbox("Class of Worker", unique_values['worker_class'])
59
+ marital_status = st.selectbox("Marital Status", unique_values['marital_status'])
60
+ race = st.selectbox("Race", unique_values['race'])
61
+ is_hispanic = st.selectbox("Hispanic Origin", unique_values['is_hispanic'])
62
+ employment_commitment = st.selectbox("Full/Part-Time Employment", unique_values['employment_commitment'])
63
+ wage_per_hour = st.number_input("Wage Per Hour", min_value=0)
 
64
 
65
  with col2:
66
+ working_week_per_year = st.number_input("Weeks Worked Per Year", min_value=0)
67
+ industry_code = st.selectbox("Category Code of Industry", unique_values['industry_code'])
68
+ industry_code_main = st.selectbox("Major Industry Code", unique_values['industry_code_main'])
69
+ occupation_code = st.selectbox("Category Code of Occupation", unique_values['occupation_code'])
70
+ occupation_code_main = st.selectbox("Major Occupation Code", unique_values['occupation_code_main'])
71
+ total_employed = st.number_input("Number of Persons Worked for Employer", min_value=0)
72
+ household_stat = st.selectbox("Detailed Household and Family Status", unique_values['household_stat'])
73
+ household_summary = st.selectbox("Detailed Household Summary", unique_values['household_summary'])
74
+ vet_benefit = st.selectbox("Veteran Benefits", unique_values['vet_benefit'])
75
 
76
  with col3:
77
+ tax_status = st.selectbox("Tax Filer Status", unique_values['tax_status'])
78
+ gains = st.number_input("Gains", min_value=0)
79
+ losses = st.number_input("Losses", min_value=0)
80
+ stocks_status = st.number_input("Dividends from Stocks", min_value=0)
81
+ citizenship = st.selectbox("Citizenship", unique_values['citizenship'])
82
+ mig_year = st.selectbox("Migration Year", unique_values['mig_year'])
83
+ country_of_birth_own = st.selectbox("Country of Birth", unique_values['country_of_birth_own'])
84
+ importance_of_record = st.number_input("Importance of Record", min_value=0.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  # Button to trigger prediction
87
  if st.button("Predict"):
 
89
  user_input = {
90
  "age": age,
91
  "gender": gender,
92
+ "education": education,
93
+ "worker_class": worker_class,
94
+ "marital_status": marital_status,
95
+ "race": race,
96
+ "is_hispanic": is_hispanic,
97
+ "employment_commitment": employment_commitment,
98
+ "wage_per_hour": wage_per_hour,
99
+ "working_week_per_year": working_week_per_year,
100
+ "industry_code": industry_code,
101
+ "industry_code_main": industry_code_main,
102
+ "occupation_code": occupation_code,
103
+ "occupation_code_main": occupation_code_main,
104
+ "total_employed": total_employed,
105
+ "household_stat": household_stat,
106
+ "household_summary": household_summary,
107
+ "vet_benefit": vet_benefit,
108
+ "tax_status": tax_status,
109
+ "gains": gains,
110
+ "losses": losses,
111
+ "stocks_status": stocks_status,
112
+ "citizenship": citizenship,
113
+ "mig_year": mig_year,
114
+ "country_of_birth_own": country_of_birth_own,
115
+ "importance_of_record": importance_of_record
116
  }
 
117
  # Send a POST request to the FastAPI server
118
+ response = requests.post("rasmodev-income-prediction-fastapi.hf.space/docs#/default/predict_income_predict__post", json=user_input)
119
 
120
  # Check if the request was successful
121
  if response.status_code == 200:
122
  prediction_data = response.json()
123
+
124
  # Display prediction result to the user
125
  st.subheader("Prediction Result")
126
+
127
+ # Determine income prediction and format message
128
+ if prediction_data['income_prediction'] == "Income over $50K":
129
+ st.success("This individual is predicted to have an income of over $50K.")
130
+ else:
131
+ st.error("This individual is predicted to have an income of under $50K")
132
+
133
+ # Display prediction probability
134
+ st.subheader("Prediction Probability")
135
+ probability = prediction_data['prediction_probability']
136
+ st.write(f"The probability of the individual having an income over $50K is: {probability:.2f}")
137
  else:
138
  st.error("Error: Unable to get prediction")