Mateusz Paszynski
fix naming from XGB to GBR
2f90873
raw
history blame
4.46 kB
import gradio as gr
import joblib
import numpy as np
import pandas as pd
from models.svr import NuSVRInsuranceModel
from models.randomforest import RandomForestInsuranceModel
from models.XGBoost import XGBoostInsuranceModel
from models.knn import KNNInsuranceModel
import os
# -------------------------------------------------------
# Placeholder code to load pre-trained models
# (Replace 'model_NuSVR.joblib', etc. with your actual file paths)
# -------------------------------------------------------
model_nusvr = joblib.load(os.path.join('.', 'NuSVRInsuranceModel.joblib'))
model_xgb = joblib.load(os.path.join('.', 'XGBoostInsuranceModel.joblib'))
model_knn = joblib.load(os.path.join('.', 'KNNInsuranceModel.joblib'))
model_rf = joblib.load(os.path.join('.', 'RandomForestInsuranceModel.joblib'))
# # Dictionary to map model choice to the actual loaded model
models_dict = {
"NuSVR": model_nusvr,
"XGBoost": model_xgb,
"KNN": model_knn,
"Random Forest": model_rf
}
# -------------------------------------------------------
# Simple label encodings or numeric mappings:
# - gender: {"male":0, "female":1}
# - diabetic: {"No":0, "Yes":1}
# - smoker: {"No":0, "Yes":1}
# - region: {"southwest":0, "southeast":1, "northwest":2, "northeast":3}
# Adjust this to however you pre-processed the data for training!
# -------------------------------------------------------
region_mapping = {"southwest": 0, "southeast": 1, "northwest": 2, "northeast": 3}
gender_mapping = {"male": 0, "female": 1}
yes_no_mapping = {"No": 0, "Yes": 1}
def predict_insurance_claim(
model_choice,
age,
gender,
bmi,
blood_pressure,
diabetic,
children,
smoker,
region
):
# Convert categorical values using the same logic used during training
try:
gender_val = gender_mapping[gender.lower()]
diabetic_val = yes_no_mapping[diabetic]
smoker_val = yes_no_mapping[smoker]
region_val = region_mapping[region.lower()]
except KeyError:
return "Invalid input for categorical field."
# Construct a single-row DataFrame with the correct column names
user_data = {
'age': [age],
'gender': [gender],
'bmi': [bmi],
'bloodpressure': [blood_pressure],
'diabetic': [diabetic],
'children': [children],
'smoker': [smoker],
'region': [region]
}
user_df = pd.DataFrame(user_data)
chosen_model = models_dict[model_choice]
d = chosen_model.preprocessing(user_df)
y_pred = chosen_model.predict(d)
return float(y_pred[0])
# -------------------------------------------------------
# Gradio Interface
# -------------------------------------------------------
def build_interface():
# Dropdown to choose model
with gr.Row():
model_dropdown = gr.Dropdown(
choices=["NuSVR", "Gradient Boost Regressor", "KNN", "Random Forest"],
value="NuSVR",
label="Select Model"
)
with gr.Row():
# Numeric inputs
age_input = gr.Number(value=39.0, label="Age")
bmi_input = gr.Number(value=23.2, label="BMI")
bp_input = gr.Number(value=91.0, label="Blood Pressure")
children_input = gr.Number(value=0, label="Children")
# Dropdowns for categorical data
gender_input = gr.Dropdown(choices=["male", "female"], value="male", label="Gender")
diabetic_input = gr.Dropdown(choices=["No", "Yes"], value="Yes", label="Diabetic")
smoker_input = gr.Dropdown(choices=["No", "Yes"], value="No", label="Smoker")
region_input = gr.Dropdown(choices=["southwest", "southeast", "northwest", "northeast"],
value="southeast",
label="Region")
# Output
output_label = gr.Textbox(label="Predicted Claim")
# Interface
demo = gr.Interface(
fn=predict_insurance_claim,
inputs=[
model_dropdown,
age_input,
gender_input,
bmi_input,
bp_input,
diabetic_input,
children_input,
smoker_input,
region_input
],
outputs=output_label,
title="Insurance Claim Prediction"
)
return demo
if __name__ == "__main__":
interface = build_interface()
interface.launch()