Spaces:
Sleeping
Sleeping
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() |