Spaces:
Runtime error
Runtime error
File size: 4,138 Bytes
7960d3a 563a02c bfa51bf feb5242 bfa51bf 563a02c 7960d3a |
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 |
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() |