datasciencedojo/Insurance-Price-Prediction
0
1import gradio as gr2import pickle3 4filename = 'GBR-insurance-prediction.sav'5loaded_model = pickle.load(open(filename, 'rb'))6 7 8def age_group_selector(age_grp):9 if age_grp == '18-24': return [1,0,0,0,0,0,0,0]10 elif age_grp =='25-30':return [0,1,0,0,0,0,0,0]11 elif age_grp =='31-36':return [0,0,1,0,0,0,0,0]12 elif age_grp =='37-42':return [0,0,0,1,0,0,0,0] 13 elif age_grp =='43-48':return [0,0,0,0,1,0,0,0]14 elif age_grp =='49-54':return [0,0,0,0,0,1,0,0]15 elif age_grp =='54-60':return [0,0,0,0,0,0,1,0]16 elif age_grp =='61-64':return [0,0,0,0,0,0,0,1]17 elif age_grp =='64+:': return [0,0,0,0,0,0,0,1]18 19def bmi_selector(bmi):20 if bmi == '0-18.4 (Underweight)':return [1,0,0,0,0]21 elif bmi == '18.5-24.9 (Healthy Weight)':return [0,1,0,0,0]22 elif bmi == '25.0-29.9 (Overweight)':return [0,0,1,0,0]23 elif bmi == '30-39.0 (Obese)': return [0,0,0,1,0]24 elif bmi == '40+ (Severely Obese)': return [0,0,0,0,1]25 26 27 28def predict_insurance(sex,smoker,children,age_grp,bmi):29 30 to_predict = []31 32 if sex == 'male':33 to_predict.append(1)34 else:35 to_predict.append(0)36 37 if smoker == 'yes':38 to_predict.append(1)39 else:40 to_predict.append(0)41 42 to_predict.append(children)43 44 to_predict = to_predict + age_group_selector(age_grp)45 46 to_predict = to_predict + bmi_selector(bmi)47 48 return (loaded_model.predict([to_predict]))49 # return to_predict50 51css = """52footer {display:none !important}53.output-markdown{display:none !important}54 55.gr-button-lg {56 z-index: 14;57 width: 113px;58 height: 30px;59 left: 0px;60 top: 0px;61 padding: 0px;62 cursor: pointer !important; 63 background: none rgb(17, 20, 45) !important;64 border: none !important;65 text-align: center !important;66 font-size: 14px !important;67 font-weight: 500 !important;68 color: rgb(255, 255, 255) !important;69 line-height: 1 !important;70 border-radius: 6px !important;71 transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;72 box-shadow: none !important;73}74.gr-button-lg:hover{75 z-index: 14;76 width: 113px;77 height: 30px;78 left: 0px;79 top: 0px;80 padding: 0px;81 cursor: pointer !important; 82 background: none rgb(66, 133, 244) !important;83 border: none !important;84 text-align: center !important;85 font-size: 14px !important;86 font-weight: 500 !important;87 color: rgb(255, 255, 255) !important;88 line-height: 1 !important;89 border-radius: 6px !important;90 transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;91 box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;92}93"""94with gr.Blocks(title="Insurance Price Prediction | Data Science Dojo", css=css) as demo:95 with gr.Row():96 input_sex = gr.Radio(["male", "female"],label="Sex")97 input_smoker = gr.Radio(["yes", "no"],label="Smoker")98 with gr.Row(): 99 input_children = gr.Slider(0, 5,label='Children',step=1)100 with gr.Row():101 input_age_group = gr.Dropdown(['18-24','25-30','31-36','37-42','43-48','49-54','54-60','61-64','64+'],label='Age Group')102 with gr.Row():103 input_bmi = gr.Dropdown(['0-18.4 (Underweight)','18.5-24.9 (Healthy Weight)','25.0-29.9 (Overweight)','30-39.0 (Obese)','40+ (Severely Obese)'],label='BMI Range')104 with gr.Row():105 insurance = gr.Textbox(label='Estimated Insurance')106 btn_ins = gr.Button(value="Submit")107 btn_ins.click(fn=predict_insurance, inputs=[input_sex,input_smoker,input_children,input_age_group,input_bmi], outputs=[insurance])108 109 110demo.launch()