import pandas as pd import streamlit as st import numpy as np import pickle import catboost from sklearn.impute import SimpleImputer import requests # Load the saved model and unique values: with open("model_and_key_components.pkl", "rb") as f: components = pickle.load(f) # Extract the individual components dt_model = components["model"] unique_values = components["unique_values"] st.image("https://i.ytimg.com/vi/WULwst0vW8g/maxresdefault.jpg") st.title("Income Prediction App") # Sidebar with input field descriptions st.sidebar.header("Description of the Required Input Fields") st.sidebar.markdown("**Age**: Enter the age of the individual (e.g., 25, 42, 57).") st.sidebar.markdown("**Gender**: Select the gender of the individual (e.g., Male, Female).") st.sidebar.markdown("**Education**: Choose the highest education level of the individual (e.g., Bachelors Degree, High School Graduate, Masters Degree).") st.sidebar.markdown("**Worker Class**: Select the class of worker for the individual (e.g., Private, Government, Self-employed).") st.sidebar.markdown("**Marital Status**: Choose the marital status of the individual (e.g., Married, Never married, Divorced).") st.sidebar.markdown("**Race**: Select the race of the individual (e.g., White, Black, Asian-Pac-Islander).") st.sidebar.markdown("**Hispanic Origin**: Choose the Hispanic origin of the individual (e.g., Mexican, Puerto Rican, Cuban).") st.sidebar.markdown("**Full/Part-Time Employment**: Select the employment status as full-time or part-time (e.g., Full-time schedules, Part-time schedules).") st.sidebar.markdown("**Wage Per Hour**: Enter the wage per hour of the individual (numeric value, e.g., 20.50).") st.sidebar.markdown("**Weeks Worked Per Year**: Specify the number of weeks the individual worked in a year (numeric value, e.g., 45).") st.sidebar.markdown("**Industry Code**: Choose the category code of the industry where the individual works (e.g., Category 1, Category 2).") st.sidebar.markdown("**Major Industry Code**: Select the major industry code of the individual's work (e.g., Industry A, Industry B).") st.sidebar.markdown("**Occupation Code**: Choose the category code of the occupation of the individual (e.g., Category X, Category Y).") st.sidebar.markdown("**Major Occupation Code**: Select the major occupation code of the individual (e.g., Occupation 1, Occupation 2).") st.sidebar.markdown("**Total Employed**: Specify the number of persons worked for the employer (numeric value, e.g., 3, 5).") st.sidebar.markdown("**Household Stat**: Choose the detailed household and family status of the individual (e.g., Single, Married-civilian spouse present).") st.sidebar.markdown("**Household Summary**: Select the detailed household summary (e.g., Child under 18 never married, Spouse of householder).") st.sidebar.markdown("**Veteran Benefits**: Choose whether the individual receives veteran benefits (Yes or No).") st.sidebar.markdown("**Tax Filer Status**: Select the tax filer status of the individual (e.g., Single, Joint both 65+).") st.sidebar.markdown("**Gains**: Specify any gains the individual has (numeric value, e.g., 1500.0).") st.sidebar.markdown("**Losses**: Specify any losses the individual has (numeric value, e.g., 300.0).") st.sidebar.markdown("**Dividends from Stocks**: Specify any dividends from stocks for the individual (numeric value, e.g., 120.5).") st.sidebar.markdown("**Citizenship**: Select the citizenship status of the individual (e.g., Native, Foreign Born- Not a citizen of U S).") st.sidebar.markdown("**Year of Migration**: Enter the year of migration for the individual (numeric value, e.g., 2005).") st.sidebar.markdown("**Country of Birth**: Choose the individual's birth country (e.g., United-States, Other).") st.sidebar.markdown("**Importance of Record**: Enter the weight of the instance (numeric value, e.g., 0.9).") # Create input fields for user input col1, col2, col3 = st.columns(3) with col1: age = st.number_input("Age", min_value=0) gender = st.selectbox("Gender", ["Male", "Female"]) education = st.selectbox("Education", unique_values['education']) worker_class = st.selectbox("Class of Worker", unique_values['worker_class']) marital_status = st.selectbox("Marital Status", unique_values['marital_status']) race = st.selectbox("Race", unique_values['race']) is_hispanic = st.selectbox("Hispanic Origin", unique_values['is_hispanic']) employment_commitment = st.selectbox("Full/Part-Time Employment", unique_values['employment_commitment']) wage_per_hour = st.number_input("Wage Per Hour", min_value=0) with col2: working_week_per_year = st.number_input("Weeks Worked Per Year", min_value=0) industry_code = st.selectbox("Category Code of Industry", unique_values['industry_code']) industry_code_main = st.selectbox("Major Industry Code", unique_values['industry_code_main']) occupation_code = st.selectbox("Category Code of Occupation", unique_values['occupation_code']) occupation_code_main = st.selectbox("Major Occupation Code", unique_values['occupation_code_main']) total_employed = st.number_input("Number of Persons Worked for Employer", min_value=0) household_stat = st.selectbox("Detailed Household and Family Status", unique_values['household_stat']) household_summary = st.selectbox("Detailed Household Summary", unique_values['household_summary']) vet_benefit = st.selectbox("Veteran Benefits", unique_values['vet_benefit']) with col3: tax_status = st.selectbox("Tax Filer Status", unique_values['tax_status']) gains = st.number_input("Gains", min_value=0) losses = st.number_input("Losses", min_value=0) stocks_status = st.number_input("Dividends from Stocks", min_value=0) citizenship = st.selectbox("Citizenship", unique_values['citizenship']) mig_year = st.selectbox("Migration Year", unique_values['mig_year']) country_of_birth_own = st.selectbox("Country of Birth", unique_values['country_of_birth_own']) importance_of_record = st.number_input("Importance of Record", min_value=0.0) # Button to trigger prediction if st.button("Predict"): # Create a dictionary of user input user_input = { "age": int(age), "gender": gender, "education": education, "worker_class": worker_class, "marital_status": marital_status, "race": race, "is_hispanic": is_hispanic, "employment_commitment": employment_commitment, "wage_per_hour": int(wage_per_hour), "working_week_per_year": int(working_week_per_year), "industry_code": int(industry_code), "industry_code_main": industry_code_main, "occupation_code": int(occupation_code), "occupation_code_main": occupation_code_main, "total_employed": int(total_employed), "household_stat": household_stat, "household_summary": household_summary, "vet_benefit": int(vet_benefit), "tax_status": tax_status, "gains": int(gains), "losses": int(losses), "stocks_status": int(stocks_status), "citizenship": citizenship, "mig_year": int(mig_year), "country_of_birth_own": country_of_birth_own, "importance_of_record": float(importance_of_record) } # Send a POST request to the FastAPI server response = requests.post("https://rasmodev-income-prediction-fastapi.hf.space/predict/", json=user_input) # Check if the request was successful if response.status_code == 200: prediction_data = response.json() # Display prediction result to the user st.subheader("Prediction Result") # Determine income prediction and format message if prediction_data['income_prediction'] == "Income over $50K": st.success("This individual is predicted to have an income of over $50K.") else: st.error("This individual is predicted to have an income of under $50K") # Display prediction probability st.subheader("Prediction Probability") probability = prediction_data['prediction_probability'] st.write(f"The probability of the individual having an income over $50K is: {probability:.2f}") else: st.error("Error: Unable to get prediction")