File size: 3,623 Bytes
2fae970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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("keras-io/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%}'


# the app uses slider and number fields for numerical inputs
# while radio buttons for the categoricals
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(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
title = "Structured Data Classification 🧮"
description =  "Binary classification of structured data including numerical and categorical features for Heart Disease prediction."

article = "Author: <a href=\"https://huggingface.co/buio\">Marco Buiani</a>. Based on this <a href=\"https://keras.io/examples/structured_data/structured_data_classification_from_scratch/\">keras example</a> by <a href=\"https://twitter.com/fchollet\">François Chollet.</a> HuggingFace Model <a href=\"https://huggingface.co/buio/structured-data-classification\">here</a> "

examples = [[41, 'female', 'atypical angina', 130, 204, 100, 'normal', 150, 'yes', 1.4, 'psloping', 2, 'reversible'],
            [63, 'male', 'typical angina', 145, 233, 150, 'T-T wave abnormality', 150, 'no', 2.3, 'flat', 0, 'fixed']]
            
gr.Interface(convert_and_predict, inputs, output, examples= examples, allow_flagging='never',
  title=title, description=description, article=article, live=True).launch()