saad177's picture
fix layout size
4aa5f86
raw
history blame
1.35 kB
import gradio as gr
import matplotlib.pyplot as plt
import shap
import hopsworks
import pandas as pd
import joblib
# Assuming you have your model and data defined elsewhere
project = hopsworks.login(
project="SonyaStern_Lab1",
api_key_value="c9StuuVQPoMUeXWe.jB2XeWcI8poKUN59W13MxAbMemzY7SChOnX151GtTFNhysBBUPMRuEp5IK7SE3i1",
)
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")
rf_model = model.steps[-1][1] # Load your model
df = pd.DataFrame(
[[20, 20, 30, 40]],
columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
)
def generate_plots():
# Create the first plot as before
fig1, ax1 = plt.subplots()
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title("Plot 1")
# Generate the SHAP waterfall plot for fig2
fig2 = shap.plots.waterfall(
shap.Explanation(
values=shap.Explainer(rf_model).shap_values(df)[1][0],
base_values=shap.Explainer(rf_model).expected_value[1],
)
)
return fig1, fig2
with gr.Blocks() as demo:
with gr.Row():
gr.Plot(generate_plots()[0]) # Display first plot in the first row
with gr.Row():
gr.Plot(generate_plots()[1]) # Display SHAP waterfall plot in the second row
demo.launch()