Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
import gradio as gr
|
4 |
+
from huggingface_hub import from_pretrained_keras
|
5 |
+
|
6 |
+
# download the already pushed model
|
7 |
+
model = from_pretrained_keras("buio/structured-data-classification")
|
8 |
+
|
9 |
+
def convert_and_predict(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
10 |
+
|
11 |
+
# some conversions from the gradio interface are needed
|
12 |
+
sample_converted = {
|
13 |
+
"age": age,
|
14 |
+
"sex": sex,
|
15 |
+
"cp": cp+1,
|
16 |
+
"trestbps": trestbps,
|
17 |
+
"chol": chol,
|
18 |
+
"fbs": 0 if fbs<=120 else 1,
|
19 |
+
"restecg": restecg,
|
20 |
+
"thalach": thalach,
|
21 |
+
"exang": exang,
|
22 |
+
"oldpeak": oldpeak,
|
23 |
+
"slope": slope+1,
|
24 |
+
"ca": ca,
|
25 |
+
"thal": thal,
|
26 |
+
}
|
27 |
+
|
28 |
+
input_dict = {name: tf.convert_to_tensor([value]) for name, value in sample_converted.items()}
|
29 |
+
predictions = model.predict(input_dict)
|
30 |
+
|
31 |
+
return f'{predictions[0][0]:.2%}'
|
32 |
+
|
33 |
+
## gradio interface elements
|
34 |
+
inputs = [
|
35 |
+
gr.Slider(minimum=1, maximum=120, step=1, label='age', value=60),
|
36 |
+
gr.Radio(choices=['female','male'], label='sex', type='index',value='male'),
|
37 |
+
gr.Radio(choices=['typical angina',
|
38 |
+
'atypical angina',
|
39 |
+
'non-anginal pain',
|
40 |
+
'asymptomatic'],
|
41 |
+
type='index', label=f'chest pain type', value='typical angina'),
|
42 |
+
gr.Number(label='blood pressure in mmHg', value=145),
|
43 |
+
gr.Number(label='serum cholestoral in mg/dl', value=233),
|
44 |
+
gr.Number(label='fasting blood sugar in mg/dl', value=150),
|
45 |
+
gr.Radio(choices=['normal','T-T wave abnormality','probable or definite left ventricular hypertrophy'],
|
46 |
+
label='resting ecg', type='index',value='probable or definite left ventricular hypertrophy'),
|
47 |
+
gr.Number(label='maximum heart rate achieved', value=150),
|
48 |
+
gr.Radio(choices=['no','yes',], type='index', label='exercise induced angina',value='no'),
|
49 |
+
gr.Number(label='ST depression induced by exercise relative to rest', value=2.3),
|
50 |
+
gr.Radio(choices=['psloping','flat','downsloping'], label='slope of the peak exercise ST segment', type='index', value='downsloping'),
|
51 |
+
gr.Number(minimum=0, maximum=3, label ='number of major vessels (0-3) colored by flourosopy',value=0),
|
52 |
+
gr.Radio(['normal','fixed','reversable'],label ='thal', value='fixed')
|
53 |
+
]
|
54 |
+
|
55 |
+
|
56 |
+
# the app outputs text
|
57 |
+
output = gr.Textbox(label='Probability of having a heart disease, as evaluated by our model:')
|
58 |
+
# it's good practice to pass examples, description and a title to guide users
|
59 |
+
examples = []
|
60 |
+
title = "Heart Disease Classification"
|
61 |
+
description = "Play with the clinical values or select examples below"
|
62 |
+
examples = [[63, 1, 1, 145, 233, 1, 2, 150, 0, 2.3, 3, 0, 'fixed'],
|
63 |
+
[67, 1, 4, 160, 286, 0, 2, 108, 1, 1.5, 2, 3, 'normal'],
|
64 |
+
[67, 1, 4, 120, 229, 0, 2, 129, 1, 2.6, 2, 2, 'reversible']]
|
65 |
+
|
66 |
+
gr.Interface(convert_and_predict, inputs, output, examples= examples, allow_flagging=False, analytics_enabled=False,
|
67 |
+
title=title, description=description).launch()
|