Spaces:
Runtime error
Runtime error
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" | |
with gr.Blocks() 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() |