File size: 3,765 Bytes
07efaef
 
 
 
 
 
 
 
 
303ce01
07efaef
 
 
 
 
4993fb9
6a83a6f
 
4c7da12
07efaef
 
 
 
 
 
 
 
 
 
 
 
 
3a706e8
04faa3a
07efaef
 
94d26ca
 
07efaef
 
 
 
 
 
 
 
 
 
 
 
0fdc94e
 
7333766
0fdc94e
2f241fe
6825639
04faa3a
6825639
 
04faa3a
6825639
04faa3a
07efaef
 
 
 
 
 
 
 
 
37f7c13
94d26ca
07efaef
 
 
4c7da12
07efaef
 
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
71
72
73
74
75
76
77
78
79
import pickle
import pandas as pd
import shap
from shap.plots._force_matplotlib import draw_additive_plot
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt

# load the model from disk
loaded_model = pickle.load(open("age_xgb.pkl", 'rb'))

# Setup SHAP
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.

# Create the main function for server
def main_func(SEQN, RIDAGEYR, RIAGENDR, PAQ605, BMXBMI, LBXGLU, DIQ010, LBXGLT,LBXIN):
    new_row = pd.DataFrame.from_dict({'SEQN':SEQN,'RIDAGEYR':RIDAGEYR,
              'RIAGENDR':RIAGENDR,'PAQ605':PAQ605,'BMXBMI':BMXBMI,
              'LBXGLU':LBXGLU, 'DIQ010':DIQ010,'LBXGLT':LBXGLT,'LBXIN':LBXIN}, 
                                     orient = 'index').transpose()
    
    prob = loaded_model.predict_proba(new_row)
    
    shap_values = explainer(new_row)
    # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
    # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
    plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)

    plt.tight_layout()
    local_plot = plt.gcf()
    plt.close()
    
    #return {"Lower Predicted Age": float(prob[0][0]), "Higher Predicted Age": 1-float(prob[0][0])}, local_plot
    return {"Lower Predicted Age": float(prob[0][0]), "Higher Predicted Age": 1-float(prob[0][0])}, local_plot

# Create the UI
title = "**National Health and Nutritional Health Assessment** πŸͺ"
description1 = """This app takes info from subjects of various health and nutritional status and predicts their age. Do not use for medical diagnosis."""

description2 = """
To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🀞
""" 

with gr.Blocks(title=title) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description1)
    gr.Markdown("""---""")
    gr.Markdown(description2)
    gr.Markdown("""---""")
    
    SEQN = gr.Number(label="Respondent Sequence Number Score", value=40)
    RIDAGEYR = gr.Slider(label="Respondent's Age Score", minimum=12.0, maximum=80.0, value=12, step=1)
    RIAGENDR = gr.Slider(label="Respondent's Gender Score", minimum=1.01, maximum=2.0, value=1.01, step=0.01)
    PAQ605 = gr.Slider(label="If the respondent engages in moderate or vigorous-intensity sports, fitness, or recreational activities in the typical week Score", minimum=1.0, maximum=7.0, value=1, step=1) 
    BMXBMI = gr.Slider(label="Respondent's Body Mass Index Score", minimum=14.5, maximum=70.1, value=14.5, step=1.01)
    LBXGLU = gr.Slider(label="Respondent's Blood Glucose after fasting Score", minimum=63.0, maximum=405.0, value=63, step=1)

    DIQ010 = gr.Slider(label="If the Respondent is diabetic Score", minimum=1, maximum=3, value=1, step=1)
    LBXGLT = gr.Slider(label="Respondent's Oral Score", minimum=40, maximum=604, value=40, step=1)
   
    LBXIN = gr.Slider(label="Respondent's Blood Insulin Levels Score", minimum=.14, maximum=102.29, value=.14, step=1)

    
    submit_btn = gr.Button("Analyze")

    with gr.Column(visible=True) as output_col:
        label = gr.Label(label = "Predicted Label")
        local_plot = gr.Plot(label = 'Shap:')

    submit_btn.click(
        main_func,
        [SEQN, RIDAGEYR, RIAGENDR, PAQ605, BMXBMI, LBXGLU, DIQ010, LBXGLT,LBXIN],
        [label,local_plot], api_name="Age_Predictor"
    )
    
    gr.Markdown("### Click on any of the examples below to see how it works:")
    gr.Examples([[74000.0,10.0,2.0,1.0,15.0,65.0,1.0,42.0,.16], [82000.0,8.0,4.0,4,67.0,400.0,2.0,580.0,99.0]], [SEQN, RIDAGEYR, RIAGENDR, PAQ605, BMXBMI, LBXGLU, DIQ010, LBXGLT,LBXIN], [label,local_plot], main_func, cache_examples=True)

demo.launch()