import gradio as gr
import hopsworks
import joblib
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import shap
project = hopsworks.login(project="SonyaStern_Lab1")
fs = project.get_feature_store()
print("trying to dl model")
mr = project.get_model_registry()
model = mr.get_model("diabetes_model", version=1)
model_dir = model.download()
model = joblib.load(model_dir + "/diabetes_model.pkl")
print("Model downloaded")
diabetes_fg = fs.get_feature_group(name="diabetes_gan", version=1)
query = diabetes_fg.select_all()
# feature_view = fs.get_or_create_feature_view(name="diabetes",
feature_view = fs.get_or_create_feature_view(
name="diabetes_gan",
version=1,
description="Read from Diabetes dataset",
labels=["diabetes"],
query=query,
)
diabetes_df = pd.DataFrame(diabetes_fg.read())
with gr.Blocks() as demo:
with gr.Row():
gr.HTML(value="
Diabetes prediction
")
with gr.Row():
with gr.Column():
age_input = gr.Number(label="age")
bmi_input = gr.Slider(10, 100, label="bmi", info="Body Mass Index")
hba1c_input = gr.Slider(
3.5, 9, label="hba1c_level", info="Glycated Haemoglobin"
)
blood_glucose_input = gr.Slider(
80, 300, label="blood_glucose_level", info="Blood Glucose Level"
)
existent_info_input = gr.Radio(
["yes", "no", "Don't know"],
label="Do you already know if you have diabetes? (This will not be used for the prediction)",
)
consent_input = gr.Checkbox(
info="I consent that my personal data will be saved and potentially be used for the model training",
label="accept",
)
btn = gr.Button("Submit")
with gr.Column():
output = gr.Text(label="Model prediction")
plot = gr.Plot()
with gr.Row():
with gr.Accordion("See model explanability", open=False):
waterfall_plot = gr.Plot()
def submit_inputs(
age_input,
bmi_input,
hba1c_input,
blood_glucose_input,
existent_info_input,
consent_input,
):
df = pd.DataFrame(
[[age_input, bmi_input, hba1c_input, blood_glucose_input]],
columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
)
res = model.predict(df)
mean_for_age = diabetes_df[
(diabetes_df["diabetes"] == 0) & (diabetes_df["age"] == age_input)
].mean()
print(
"your bmi is:", bmi_input, "the mean for ur age is :", mean_for_age["bmi"]
)
categories = ["BMI", "HbA1c", "Blood Level"]
fig, ax = plt.subplots()
bar_width = 0.35
indices = np.arange(len(categories))
ax.bar(
indices,
[
mean_for_age.bmi,
mean_for_age.hba1c_level,
mean_for_age.blood_glucose_level,
],
bar_width,
label="Reference",
color="b",
alpha=0.7,
)
ax.bar(
indices + bar_width,
[bmi_input, hba1c_input, blood_glucose_input],
bar_width,
label="User",
color="r",
alpha=0.7,
)
ax.legend()
ax.set_xlabel("Variables")
ax.set_ylabel("Values")
ax.set_title("Comparison with Mean values for your age")
ax.set_xticks(indices + bar_width / 2)
ax.set_xticklabels(categories)
## explainability plots
rf_model = model.steps[-1][1]
print("model: ", rf_model)
explainer = shap.TreeExplainer(rf_model, model_output="raw")
shap_values = explainer(df)
print(shap_values.shape) # should now show (1, 4)
shap_waterfall_plot = shap.plots.waterfall(shap_values[0])
## save user's data in hopsworks
if consent_input == True:
user_data_fg = fs.get_or_create_feature_group(
name="user_diabetes_data",
version=1,
primary_key=["age", "bmi", "hba1c_level", "blood_glucose_level"],
description="Submitted user data",
)
user_data_df = df.copy()
user_data_df["diabetes"] = existent_info_input
user_data_fg.insert(user_data_df)
print("inserted new user data to hopsworks", user_data_df)
return res, fig, shap_waterfall_plot
btn.click(
submit_inputs,
inputs=[
age_input,
bmi_input,
hba1c_input,
blood_glucose_input,
existent_info_input,
consent_input,
],
outputs=[output, plot, waterfall_plot],
)
demo.launch()