freddyaboulton HF staff commited on
Commit
2fae970
1 Parent(s): 277b36e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("keras-io/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
+
34
+ # the app uses slider and number fields for numerical inputs
35
+ # while radio buttons for the categoricals
36
+ inputs = [
37
+ gr.Slider(minimum=1, maximum=120, step=1, label='age', value=60),
38
+ gr.Radio(choices=['female','male'], label='sex', type='index',value='male'),
39
+ gr.Radio(choices=['typical angina',
40
+ 'atypical angina',
41
+ 'non-anginal pain',
42
+ 'asymptomatic'],
43
+ type='index', label=f'chest pain type', value='typical angina'),
44
+ gr.Number(label='blood pressure in mmHg', value=145),
45
+ gr.Number(label='serum cholestoral in mg/dl', value=233),
46
+ gr.Number(label='fasting blood sugar in mg/dl', value=150),
47
+ gr.Radio(choices=['normal','T-T wave abnormality','probable or definite left ventricular hypertrophy'],
48
+ label='resting ecg', type='index',value='probable or definite left ventricular hypertrophy'),
49
+ gr.Number(label='maximum heart rate achieved', value=150),
50
+ gr.Radio(choices=['no','yes',], type='index', label='exercise induced angina',value='no'),
51
+ gr.Number(label='ST depression induced by exercise relative to rest', value=2.3),
52
+ gr.Radio(choices=['psloping','flat','downsloping'], label='slope of the peak exercise ST segment', type='index', value='downsloping'),
53
+ gr.Number(label ='number of major vessels (0-3) colored by flourosopy',value=0),
54
+ gr.Radio(['normal','fixed','reversable'],label ='thal', value='fixed')
55
+ ]
56
+
57
+
58
+ # the app outputs text
59
+ output = gr.Textbox(label='Probability of having a heart disease, as evaluated by our model:')
60
+ # it's good practice to pass examples, description and a title to guide users
61
+ title = "Structured Data Classification 🧮"
62
+ description = "Binary classification of structured data including numerical and categorical features for Heart Disease prediction."
63
+
64
+ 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> "
65
+
66
+ examples = [[41, 'female', 'atypical angina', 130, 204, 100, 'normal', 150, 'yes', 1.4, 'psloping', 2, 'reversible'],
67
+ [63, 'male', 'typical angina', 145, 233, 150, 'T-T wave abnormality', 150, 'no', 2.3, 'flat', 0, 'fixed']]
68
+
69
+ gr.Interface(convert_and_predict, inputs, output, examples= examples, allow_flagging='never',
70
+ title=title, description=description, article=article, live=True).launch()