Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,84 +46,33 @@ shap_values = explainer(df[columns])
|
|
| 46 |
|
| 47 |
# Define functions for Gradio
|
| 48 |
|
| 49 |
-
def get_shap_summary():
|
| 50 |
-
"""Generates SHAP summary plot."""
|
| 51 |
-
plt.figure()
|
| 52 |
-
shap.summary_plot(shap_values, df[columns], feature_names=columns, show=False)
|
| 53 |
-
plt.savefig("shap_summary.png")
|
| 54 |
-
return "shap_summary.png"
|
| 55 |
-
|
| 56 |
-
def get_shap_waterfall(index):
|
| 57 |
-
"""Generates SHAP waterfall plot for a specific data point."""
|
| 58 |
-
specific_index = int(index)
|
| 59 |
-
plt.figure()
|
| 60 |
-
shap.waterfall_plot(
|
| 61 |
-
shap.Explanation(
|
| 62 |
-
values=shap_values.values[specific_index],
|
| 63 |
-
base_values=shap_values.base_values[specific_index],
|
| 64 |
-
data=df.iloc[specific_index],
|
| 65 |
-
feature_names=columns
|
| 66 |
-
)
|
| 67 |
-
)
|
| 68 |
-
plt.savefig("shap_waterfall.png")
|
| 69 |
-
return "shap_waterfall.png"
|
| 70 |
-
|
| 71 |
-
def get_scatter_plot(feature1, feature2):
|
| 72 |
-
"""Generates scatter plot for two features."""
|
| 73 |
-
plt.figure(figsize=(8, 6))
|
| 74 |
-
plt.scatter(
|
| 75 |
-
df[feature1],
|
| 76 |
-
df[feature2],
|
| 77 |
-
c=(df["Anomaly_Label"] == "Anomaly"),
|
| 78 |
-
cmap="coolwarm",
|
| 79 |
-
edgecolor="k",
|
| 80 |
-
alpha=0.7
|
| 81 |
-
)
|
| 82 |
-
plt.title(f"Isolation Forest - {feature1} vs {feature2}")
|
| 83 |
-
plt.xlabel(feature1)
|
| 84 |
-
plt.ylabel(feature2)
|
| 85 |
-
plt.savefig("scatter_plot.png")
|
| 86 |
-
return "scatter_plot.png"
|
| 87 |
-
|
| 88 |
def get_anomaly_samples():
|
| 89 |
"""Returns formatted top, middle, and bottom 10 records based on anomaly score."""
|
| 90 |
sorted_df = df.sort_values("Anomaly_Score", ascending=False)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return top_10, middle_10, bottom_10
|
| 95 |
|
| 96 |
# Create Gradio interface
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("# Isolation Forest Anomaly Detection")
|
| 99 |
|
| 100 |
-
with gr.Tab("SHAP Summary"):
|
| 101 |
-
gr.Markdown("### Global Explainability: SHAP Summary Plot")
|
| 102 |
-
shap_button = gr.Button("Generate SHAP Summary Plot")
|
| 103 |
-
shap_image = gr.Image()
|
| 104 |
-
shap_button.click(get_shap_summary, outputs=shap_image)
|
| 105 |
-
|
| 106 |
-
with gr.Tab("SHAP Waterfall"):
|
| 107 |
-
gr.Markdown("### Local Explainability: SHAP Waterfall Plot")
|
| 108 |
-
index_input = gr.Number(label="Data Point Index", value=0)
|
| 109 |
-
shap_waterfall_button = gr.Button("Generate SHAP Waterfall Plot")
|
| 110 |
-
shap_waterfall_image = gr.Image()
|
| 111 |
-
shap_waterfall_button.click(get_shap_waterfall, inputs=index_input, outputs=shap_waterfall_image)
|
| 112 |
-
|
| 113 |
-
with gr.Tab("Feature Scatter Plot"):
|
| 114 |
-
gr.Markdown("### Feature Interaction: Scatter Plot")
|
| 115 |
-
feature1_dropdown = gr.Dropdown(choices=columns, label="Feature 1")
|
| 116 |
-
feature2_dropdown = gr.Dropdown(choices=columns, label="Feature 2")
|
| 117 |
-
scatter_button = gr.Button("Generate Scatter Plot")
|
| 118 |
-
scatter_image = gr.Image()
|
| 119 |
-
scatter_button.click(get_scatter_plot, inputs=[feature1_dropdown, feature2_dropdown], outputs=scatter_image)
|
| 120 |
-
|
| 121 |
with gr.Tab("Anomaly Samples"):
|
| 122 |
-
gr.Markdown("
|
| 123 |
-
anomaly_samples_button = gr.Button("Show Anomaly Samples")
|
| 124 |
top_table = gr.Dataframe(label="Top 10 Records")
|
|
|
|
|
|
|
| 125 |
middle_table = gr.Dataframe(label="Middle 10 Records")
|
|
|
|
|
|
|
| 126 |
bottom_table = gr.Dataframe(label="Bottom 10 Records")
|
|
|
|
|
|
|
| 127 |
anomaly_samples_button.click(
|
| 128 |
get_anomaly_samples,
|
| 129 |
outputs=[top_table, middle_table, bottom_table]
|
|
|
|
| 46 |
|
| 47 |
# Define functions for Gradio
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def get_anomaly_samples():
|
| 50 |
"""Returns formatted top, middle, and bottom 10 records based on anomaly score."""
|
| 51 |
sorted_df = df.sort_values("Anomaly_Score", ascending=False)
|
| 52 |
+
# Top 10 anomalies
|
| 53 |
+
top_10 = sorted_df[sorted_df["Anomaly_Label"] == "Anomaly"].head(10)
|
| 54 |
+
# Middle 10 (mixed records)
|
| 55 |
+
mid_start = len(sorted_df) // 2 - 5
|
| 56 |
+
middle_10 = sorted_df.iloc[mid_start: mid_start + 10]
|
| 57 |
+
# Bottom 10 normal records
|
| 58 |
+
bottom_10 = sorted_df[sorted_df["Anomaly_Label"] == "Normal"].tail(10)
|
| 59 |
return top_10, middle_10, bottom_10
|
| 60 |
|
| 61 |
# Create Gradio interface
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
gr.Markdown("# Isolation Forest Anomaly Detection")
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Tab("Anomaly Samples"):
|
| 66 |
+
gr.Markdown("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Top 10 Records (Anomalies)</h3>", unsafe_allow_html=True)
|
|
|
|
| 67 |
top_table = gr.Dataframe(label="Top 10 Records")
|
| 68 |
+
|
| 69 |
+
gr.Markdown("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Middle 10 Records (Mixed)</h3>", unsafe_allow_html=True)
|
| 70 |
middle_table = gr.Dataframe(label="Middle 10 Records")
|
| 71 |
+
|
| 72 |
+
gr.Markdown("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Bottom 10 Records (Normal)</h3>", unsafe_allow_html=True)
|
| 73 |
bottom_table = gr.Dataframe(label="Bottom 10 Records")
|
| 74 |
+
|
| 75 |
+
anomaly_samples_button = gr.Button("Show Anomaly Samples")
|
| 76 |
anomaly_samples_button.click(
|
| 77 |
get_anomaly_samples,
|
| 78 |
outputs=[top_table, middle_table, bottom_table]
|