datasciencedojo commited on
Commit
7960d3a
1 Parent(s): e5832d4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ from sklearn import preprocessing
4
+ import pandas as pd
5
+
6
+ filename = 'knn_model.sav'
7
+
8
+ loaded_model = pickle.load(open(filename, 'rb'))
9
+
10
+
11
+
12
+ def hptension(hp):
13
+ if hp == 'yes':
14
+ return 1
15
+ else:
16
+ return 0
17
+
18
+ def ht_dis(ht):
19
+ if ht == 'yes':
20
+ return 1
21
+ else:
22
+ return 0
23
+
24
+
25
+ def gender_select(gen):
26
+ if gen == 'male':
27
+ return 1
28
+ else:
29
+ return 0
30
+
31
+ def age_group_selector(age_grp):
32
+ if age_grp == '0-16': return 0
33
+ elif age_grp =='17-32': return 1
34
+ elif age_grp =='33-48': return 2
35
+ elif age_grp =='49-64': return 3
36
+ else: return 4
37
+
38
+
39
+ def smoker_cat(smoke):
40
+ if smoke == 'formerly smoked': return 0
41
+ elif smoke =='never smoked': return 1
42
+ elif smoke =='smokes': return 2
43
+ else: return 3
44
+
45
+
46
+ def predict_insurance(input_gender,input_age_group,input_hypertension,input_heart_disease,input_avg_glucose_level,input_bmi,input_smoking_status):
47
+
48
+ 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
49
+
50
+ series = {'gender': [gender_select(input_gender)],
51
+ 'age_band': [age_group_selector(input_age_group)],
52
+ 'hypertension': [hptension(input_hypertension)],
53
+ 'heart_disease': [ht_dis(input_heart_disease)],
54
+ 'avg_glucose_level': [input_avg_glucose_level /272],
55
+ 'bmi': [input_bmi/49],
56
+ 'smoking_status': [smoker_cat(input_smoking_status)],
57
+ }
58
+
59
+ vector = pd.DataFrame(series)
60
+
61
+ result = loaded_model.predict(vector)
62
+ if result[0] == 1:
63
+ return "Risk of having stroke is high"
64
+ else:
65
+ return "Risk of having stroke is low"
66
+
67
+ with gr.Blocks() as demo:
68
+ with gr.Row():
69
+ input_gender = gr.Radio(["male", "female"],label="Gender")
70
+ input_hypertension = gr.Radio(["yes", "no"],label="Hypertension")
71
+ input_heart_disease = gr.Radio(["yes", "no"],label="Heart disease")
72
+ with gr.Row():
73
+ input_age_group = gr.Dropdown(['0-16','17-32','33-48','49-64','64+'],label='Age Group')
74
+ input_smoking_status = gr.Dropdown(['formerly smoked', 'never smoked', 'smokes', 'Prefer not to say'],label='Smoker')
75
+ with gr.Row():
76
+ input_avg_glucose_level = gr.Slider(0, 270,label='Average Glucose Level')
77
+ with gr.Row():
78
+ input_bmi = gr.Slider(0, 45,label='BMI Range')
79
+ with gr.Row():
80
+ stroke = gr.Textbox(label='Chances of stroke')
81
+ btn_ins = gr.Button(value="Submit")
82
+ btn_ins.click(fn=predict_insurance, inputs=[input_gender,input_age_group,input_hypertension,input_heart_disease,
83
+ input_avg_glucose_level,input_bmi,input_smoking_status], outputs=[stroke])
84
+
85
+ demo.launch()