Spaces:
Sleeping
Sleeping
File size: 4,887 Bytes
5de1466 72d9d59 5de1466 7faaa90 72d9d59 5de1466 72d9d59 5de1466 72d9d59 5de1466 72d9d59 5de1466 a4cd84f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 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
from models.linreg import LinearRegressionInsuranceModel
from models.polyreg import PolynomialRegressionInsuranceModel
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'))
model_lr = joblib.load(os.path.join('.', 'LinearRegressionInsuranceModel.joblib'))
model_pr = joblib.load(os.path.join('.', 'PolynomialRegressionInsuranceModel.joblib'))
# # Dictionary to map model choice to the actual loaded model
models_dict = {
"NuSVR": model_nusvr,
"Gradient Boost Regressor": model_xgb,
"KNN": model_knn,
"Random Forest": model_rf,
"Linear Regression": model_lr,
"Polynomial Regression": model_pr
}
# -------------------------------------------------------
# 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", 'Linear Regression', 'Polynomial Regression'],
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()
|