File size: 3,090 Bytes
04c273a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import tensorflow as tf
import gradio as gr
from huggingface_hub import from_pretrained_keras

# download the already pushed model
model = from_pretrained_keras("buio/structured-data-classification")

def convert_and_predict(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):

    # some conversions from the gradio interface are needed
    sample_converted = {
    "age": age,
    "sex": sex,
    "cp": cp+1,
    "trestbps": trestbps,
    "chol": chol,
    "fbs": 0 if fbs<=120 else 1,
    "restecg": restecg,
    "thalach": thalach,
    "exang": exang,
    "oldpeak": oldpeak,
    "slope": slope+1,
    "ca": ca,
    "thal": thal,
}

    input_dict = {name: tf.convert_to_tensor([value]) for name, value in sample_converted.items()}
    predictions = model.predict(input_dict)

    return f'{predictions[0][0]:.2%}'

## gradio interface elements
inputs = [
          gr.Slider(minimum=1, maximum=120, step=1, label='age', value=60),
          gr.Radio(choices=['female','male'], label='sex', type='index',value='male'),
          gr.Radio(choices=['typical angina',
                            'atypical angina',
                            'non-anginal pain',
                            'asymptomatic'], 
                   type='index', label=f'chest pain type', value='typical angina'),
          gr.Number(label='blood pressure in mmHg', value=145),
          gr.Number(label='serum cholestoral in mg/dl', value=233),
          gr.Number(label='fasting blood sugar in mg/dl', value=150),
          gr.Radio(choices=['normal','T-T wave abnormality','probable or definite left ventricular hypertrophy'], 
                   label='resting ecg', type='index',value='probable or definite left ventricular hypertrophy'),
          gr.Number(label='maximum heart rate achieved', value=150),
          gr.Radio(choices=['no','yes',], type='index', label='exercise induced angina',value='no'),
          gr.Number(label='ST depression induced by exercise relative to rest', value=2.3),
          gr.Radio(choices=['psloping','flat','downsloping'], label='slope of the peak exercise ST segment', type='index', value='downsloping'),
          gr.Number(minimum=0, maximum=3, label ='number of major vessels (0-3) colored by flourosopy',value=0),
          gr.Radio(['normal','fixed','reversable'],label ='thal', value='fixed')
          ]


# the app outputs text
output = gr.Textbox(label='Probability of having a heart disease, as evaluated by our model:')
# it's good practice to pass examples, description and a title to guide users
examples = [] 
title = "Heart Disease Classification"
description = "Play with the clinical values or select examples below"
examples = [[63, 1, 1, 145, 233, 1, 2, 150, 0, 2.3, 3, 0, 'fixed'],
            [67, 1, 4, 160, 286, 0, 2, 108, 1, 1.5, 2, 3, 'normal'],
            [67, 1, 4, 120, 229, 0, 2, 129, 1, 2.6, 2, 2, 'reversible']]

gr.Interface(convert_and_predict, inputs, output, examples= examples, allow_flagging=False, analytics_enabled=False,
  title=title, description=description).launch()