import gradio as gr import pickle from sklearn import preprocessing import pandas as pd filename = 'knn_model.sav' loaded_model = pickle.load(open(filename, 'rb')) def hptension(hp): if hp == 'yes': return 1 else: return 0 def ht_dis(ht): if ht == 'yes': return 1 else: return 0 def gender_select(gen): if gen == 'male': return 1 else: return 0 def age_group_selector(age_grp): if age_grp == '0-16': return 0 elif age_grp =='17-32': return 1 elif age_grp =='33-48': return 2 elif age_grp =='49-64': return 3 else: return 4 def smoker_cat(smoke): if smoke == 'formerly smoked': return 0 elif smoke =='never smoked': return 1 elif smoke =='smokes': return 2 else: return 3 def predict_insurance(input_gender,input_age_group,input_hypertension,input_heart_disease,input_avg_glucose_level,input_bmi,input_smoking_status): input_gender,input_age_group,input_hypertension,input_heart_disease,input_avg_glucose_level,input_bmi,input_smoking_status = input_gender,input_age_group,input_hypertension,input_heart_disease,input_avg_glucose_level,input_bmi,input_smoking_status series = {'gender': [gender_select(input_gender)], 'age_band': [age_group_selector(input_age_group)], 'hypertension': [hptension(input_hypertension)], 'heart_disease': [ht_dis(input_heart_disease)], 'avg_glucose_level': [input_avg_glucose_level /272], 'bmi': [input_bmi/49], 'smoking_status': [smoker_cat(input_smoking_status)], } vector = pd.DataFrame(series) result = loaded_model.predict(vector) if result[0] == 1: return "Risk of having stroke is high" else: return "Risk of having stroke is low" css = """ footer {display:none !important} .output-markdown{display:none !important} footer {visibility: hidden} .gr-button-lg { z-index: 14; width: 113px; height: 30px; left: 0px; top: 0px; padding: 0px; cursor: pointer !important; background: none rgb(17, 20, 45) !important; border: none !important; text-align: center !important; font-size: 14px !important; font-weight: 500 !important; color: rgb(255, 255, 255) !important; line-height: 1 !important; border-radius: 6px !important; transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important; box-shadow: none !important; } .gr-button-lg:hover{ z-index: 14; width: 113px; height: 30px; left: 0px; top: 0px; padding: 0px; cursor: pointer !important; background: none rgb(66, 133, 244) !important; border: none !important; text-align: center !important; font-size: 14px !important; font-weight: 500 !important; color: rgb(255, 255, 255) !important; line-height: 1 !important; border-radius: 6px !important; transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important; box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important; } """ with gr.Blocks(title="Brain Stroke Prediction | Data Science Dojo", css = css) as demo: with gr.Row(): input_gender = gr.Radio(["male", "female"],label="Gender") input_hypertension = gr.Radio(["yes", "no"],label="Hypertension") input_heart_disease = gr.Radio(["yes", "no"],label="Heart disease") with gr.Row(): input_age_group = gr.Dropdown(['0-16','17-32','33-48','49-64','64+'],label='Age Group') input_smoking_status = gr.Dropdown(['formerly smoked', 'never smoked', 'smokes', 'Prefer not to say'],label='Smoker') with gr.Row(): input_avg_glucose_level = gr.Slider(0, 270,label='Average Glucose Level') with gr.Row(): input_bmi = gr.Slider(0, 45,label='BMI Range') with gr.Row(): stroke = gr.Textbox(label='Chances of stroke') btn_ins = gr.Button(value="Submit") btn_ins.click(fn=predict_insurance, inputs=[input_gender,input_age_group,input_hypertension,input_heart_disease, input_avg_glucose_level,input_bmi,input_smoking_status], outputs=[stroke]) demo.launch()