Spaces:
Runtime error
Runtime error
fix layout size
Browse files- app.py +3 -2
- test.py +48 -0
- testchat.py +50 -0
- testcorcel.py +79 -0
- testcorcel2.py +75 -0
app.py
CHANGED
@@ -62,7 +62,7 @@ with gr.Blocks() as demo:
|
|
62 |
with gr.Accordion("See model explanability", open=False):
|
63 |
with gr.Row():
|
64 |
with gr.Column():
|
65 |
-
waterfall_plot = gr.Plot()
|
66 |
with gr.Column():
|
67 |
ploott = gr.Plot()
|
68 |
with gr.Row():
|
@@ -150,7 +150,8 @@ with gr.Blocks() as demo:
|
|
150 |
feature_names=df.columns.tolist(),
|
151 |
)
|
152 |
|
153 |
-
fig2 = plt.figure(figsize=(
|
|
|
154 |
plt.title("SHAP Waterfall Plot") # Optionally set a title for the SHAP plot
|
155 |
plt.tight_layout()
|
156 |
shap.waterfall_plot(
|
|
|
62 |
with gr.Accordion("See model explanability", open=False):
|
63 |
with gr.Row():
|
64 |
with gr.Column():
|
65 |
+
waterfall_plot = gr.Plot(container=True)
|
66 |
with gr.Column():
|
67 |
ploott = gr.Plot()
|
68 |
with gr.Row():
|
|
|
150 |
feature_names=df.columns.tolist(),
|
151 |
)
|
152 |
|
153 |
+
fig2 = plt.figure(figsize=(8, 4)) # Create a new figure for SHAP plot
|
154 |
+
fig2.tight_layout()
|
155 |
plt.title("SHAP Waterfall Plot") # Optionally set a title for the SHAP plot
|
156 |
plt.tight_layout()
|
157 |
shap.waterfall_plot(
|
test.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import shap
|
4 |
+
import hopsworks
|
5 |
+
import pandas as pd
|
6 |
+
import joblib
|
7 |
+
|
8 |
+
# Assuming you have your model and data defined elsewhere
|
9 |
+
project = hopsworks.login(
|
10 |
+
project="SonyaStern_Lab1",
|
11 |
+
api_key_value="c9StuuVQPoMUeXWe.jB2XeWcI8poKUN59W13MxAbMemzY7SChOnX151GtTFNhysBBUPMRuEp5IK7SE3i1",
|
12 |
+
)
|
13 |
+
mr = project.get_model_registry()
|
14 |
+
model = mr.get_model("diabetes_model", version=1)
|
15 |
+
model_dir = model.download()
|
16 |
+
model = joblib.load(model_dir + "/diabetes_model.pkl")
|
17 |
+
rf_model = model.steps[-1][1] # Load your model
|
18 |
+
df = pd.DataFrame(
|
19 |
+
[[20, 20, 30, 40]],
|
20 |
+
columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
|
21 |
+
)
|
22 |
+
|
23 |
+
|
24 |
+
def generate_plots():
|
25 |
+
# Create the first plot as before
|
26 |
+
fig1, ax1 = plt.subplots()
|
27 |
+
ax1.plot([1, 2, 3], [4, 5, 6])
|
28 |
+
ax1.set_title("Plot 1")
|
29 |
+
|
30 |
+
# Generate the SHAP waterfall plot for fig2
|
31 |
+
fig2 = shap.plots.waterfall(
|
32 |
+
shap.Explanation(
|
33 |
+
values=shap.Explainer(rf_model).shap_values(df)[1][0],
|
34 |
+
base_values=shap.Explainer(rf_model).expected_value[1],
|
35 |
+
)
|
36 |
+
)
|
37 |
+
|
38 |
+
return fig1, fig2
|
39 |
+
|
40 |
+
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
with gr.Row():
|
43 |
+
gr.Plot(generate_plots()[0]) # Display first plot in the first row
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
gr.Plot(generate_plots()[1]) # Display SHAP waterfall plot in the second row
|
47 |
+
|
48 |
+
demo.launch()
|
testchat.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import shap
|
4 |
+
import hopsworks
|
5 |
+
import pandas as pd
|
6 |
+
import joblib
|
7 |
+
|
8 |
+
project = hopsworks.login(
|
9 |
+
project="SonyaStern_Lab1",
|
10 |
+
api_key_value="c9StuuVQPoMUeXWe.jB2XeWcI8poKUN59W13MxAbMemzY7SChOnX151GtTFNhysBBUPMRuEp5IK7SE3i1",
|
11 |
+
)
|
12 |
+
mr = project.get_model_registry()
|
13 |
+
model = mr.get_model("diabetes_model", version=1)
|
14 |
+
model_dir = model.download()
|
15 |
+
model = joblib.load(model_dir + "/diabetes_model.pkl")
|
16 |
+
rf_model = model.steps[-1][1] # Load your model
|
17 |
+
df = pd.DataFrame(
|
18 |
+
[[20, 20, 30, 40]],
|
19 |
+
columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
|
20 |
+
)
|
21 |
+
|
22 |
+
|
23 |
+
def generate_plots():
|
24 |
+
# Create the first plot as before
|
25 |
+
fig1, ax1 = plt.subplots()
|
26 |
+
ax1.plot([1, 2, 3], [4, 5, 6])
|
27 |
+
ax1.set_title("Plot 1")
|
28 |
+
|
29 |
+
# Generate the SHAP waterfall plot for fig2
|
30 |
+
explainer = shap.Explainer(rf_model)
|
31 |
+
shap_values = explainer.shap_values(df)[1] # Select SHAP values for class 1
|
32 |
+
shap_values_exp = shap.Explanation(
|
33 |
+
values=shap_values[0], base_values=explainer.expected_value[1]
|
34 |
+
)
|
35 |
+
ax2 = shap.plots.waterfall(
|
36 |
+
shap_values_exp, show=False
|
37 |
+
) # Get the axis for the waterfall plot
|
38 |
+
|
39 |
+
return fig1, ax2
|
40 |
+
|
41 |
+
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
with gr.Row():
|
44 |
+
gr.Plot(generate_plots()[0]) # Display first plot in the first row
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
_, ax2 = generate_plots()
|
48 |
+
gr.Plot(ax2) # Display SHAP waterfall plot in the second row
|
49 |
+
|
50 |
+
demo.launch()
|
testcorcel.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import shap
|
4 |
+
import hopsworks
|
5 |
+
import pandas as pd
|
6 |
+
import joblib
|
7 |
+
from sklearn.pipeline import make_pipeline
|
8 |
+
|
9 |
+
df = pd.DataFrame(
|
10 |
+
[[20, 20, 30, 40]],
|
11 |
+
columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
# Assuming the hopsworks login and model retrieval code works as expected
|
16 |
+
project = hopsworks.login(
|
17 |
+
project="SonyaStern_Lab1",
|
18 |
+
api_key_value="c9StuuVQPoMUeXWe.jB2XeWcI8poKUN59W13MxAbMemzY7SChOnX151GtTFNhysBBUPMRuEp5IK7SE3i1",
|
19 |
+
)
|
20 |
+
mr = project.get_model_registry()
|
21 |
+
model = mr.get_model("diabetes_model", version=1)
|
22 |
+
model_dir = model.download()
|
23 |
+
model = joblib.load(model_dir + "/diabetes_model.pkl")
|
24 |
+
print("printing model pipeline:", model)
|
25 |
+
|
26 |
+
rf_classifier = model.named_steps["randomforestclassifier"]
|
27 |
+
|
28 |
+
transformer_pipeline = make_pipeline(
|
29 |
+
*[
|
30 |
+
step
|
31 |
+
for name, step in model.named_steps.items()
|
32 |
+
if name != "randomforestclassifier"
|
33 |
+
]
|
34 |
+
)
|
35 |
+
|
36 |
+
transformed_df = transformer_pipeline.transform(df)
|
37 |
+
|
38 |
+
|
39 |
+
# rf_model = model.steps[-1][1] # Load your model
|
40 |
+
|
41 |
+
|
42 |
+
def generate_plots():
|
43 |
+
# Create the first plot as before
|
44 |
+
fig1, ax1 = plt.subplots()
|
45 |
+
ax1.plot([1, 2, 3], [4, 5, 6])
|
46 |
+
ax1.set_title("Plot 1")
|
47 |
+
|
48 |
+
# Generate the SHAP waterfall plot for fig2
|
49 |
+
explainer = shap.TreeExplainer(rf_classifier)
|
50 |
+
|
51 |
+
shap_values = explainer.shap_values(transformed_df)
|
52 |
+
predicted_class = rf_classifier.predict(transformed_df)[0]
|
53 |
+
shap_values_for_predicted_class = shap_values[predicted_class]
|
54 |
+
# base_value = explainer.expected_value[1]
|
55 |
+
|
56 |
+
fig2 = plt.figure() # Create a new figure for SHAP plot
|
57 |
+
shap_explanation = shap.Explanation(
|
58 |
+
values=shap_values_for_predicted_class[0],
|
59 |
+
base_values=explainer.expected_value[predicted_class],
|
60 |
+
data=transformed_df[0],
|
61 |
+
feature_names=df.columns.tolist(),
|
62 |
+
)
|
63 |
+
shap.waterfall_plot(shap_explanation)
|
64 |
+
plt.title("SHAP Waterfall Plot") # Optionally set a title for the SHAP plot
|
65 |
+
|
66 |
+
return fig1, fig2
|
67 |
+
|
68 |
+
|
69 |
+
# Generate plots once and store them
|
70 |
+
fig1, fig2 = generate_plots()
|
71 |
+
|
72 |
+
with gr.Blocks() as demo:
|
73 |
+
with gr.Row():
|
74 |
+
gr.Plot(fig1) # Display first plot in the first row
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
gr.Plot(fig2) # Display SHAP waterfall plot in the second row
|
78 |
+
|
79 |
+
demo.launch()
|
testcorcel2.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import shap
|
4 |
+
import hopsworks
|
5 |
+
import pandas as pd
|
6 |
+
import joblib
|
7 |
+
from sklearn.pipeline import make_pipeline
|
8 |
+
|
9 |
+
df = pd.DataFrame(
|
10 |
+
[[20, 20, 30, 40]],
|
11 |
+
columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
# Assuming the hopsworks login and model retrieval code works as expected
|
16 |
+
project = hopsworks.login(
|
17 |
+
project="SonyaStern_Lab1",
|
18 |
+
api_key_value="c9StuuVQPoMUeXWe.jB2XeWcI8poKUN59W13MxAbMemzY7SChOnX151GtTFNhysBBUPMRuEp5IK7SE3i1",
|
19 |
+
)
|
20 |
+
mr = project.get_model_registry()
|
21 |
+
model = mr.get_model("diabetes_gan_model", version=1)
|
22 |
+
model_dir = model.download()
|
23 |
+
model = joblib.load(model_dir + "/diabetes_gan_model.pkl")
|
24 |
+
print("printing model pipeline:", model)
|
25 |
+
|
26 |
+
rf_classifier = model.named_steps["randomforestclassifier"]
|
27 |
+
|
28 |
+
transformer_pipeline = make_pipeline(
|
29 |
+
*[
|
30 |
+
step
|
31 |
+
for name, step in model.named_steps.items()
|
32 |
+
if name != "randomforestclassifier"
|
33 |
+
]
|
34 |
+
)
|
35 |
+
|
36 |
+
transformed_df = transformer_pipeline.transform(df)
|
37 |
+
|
38 |
+
|
39 |
+
# rf_model = model.steps[-1][1] # Load your model
|
40 |
+
|
41 |
+
|
42 |
+
def generate_plots():
|
43 |
+
# Create the first plot as before
|
44 |
+
fig1, ax1 = plt.subplots()
|
45 |
+
ax1.plot([1, 2, 3], [4, 5, 6])
|
46 |
+
ax1.set_title("Plot 1")
|
47 |
+
|
48 |
+
# Generate the SHAP waterfall plot for fig2
|
49 |
+
explainer = shap.TreeExplainer(rf_classifier)
|
50 |
+
|
51 |
+
shap_values = explainer.shap_values(transformed_df)
|
52 |
+
predicted_class = rf_classifier.predict(transformed_df)[0]
|
53 |
+
shap_values_for_predicted_class = shap_values[predicted_class]
|
54 |
+
# base_value = explainer.expected_value[1]
|
55 |
+
|
56 |
+
fig2 = plt.figure() # Create a new figure for SHAP plot
|
57 |
+
shap.waterfall_plot(
|
58 |
+
explainer.expected_value[predicted_class], shap_values_for_predicted_class[0]
|
59 |
+
)
|
60 |
+
plt.title("SHAP Waterfall Plot") # Optionally set a title for the SHAP plot
|
61 |
+
|
62 |
+
return fig1, fig2
|
63 |
+
|
64 |
+
|
65 |
+
# Generate plots once and store them
|
66 |
+
fig1, fig2 = generate_plots()
|
67 |
+
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
with gr.Row():
|
70 |
+
gr.Plot(fig1) # Display first plot in the first row
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
gr.Plot(fig2) # Display SHAP waterfall plot in the second row
|
74 |
+
|
75 |
+
demo.launch()
|