Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from sklearn.datasets import make_classification
|
|
| 4 |
from sklearn.ensemble import IsolationForest
|
| 5 |
import shap
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
-
from itertools import combinations
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
# Generate synthetic data with 20 features
|
|
@@ -86,6 +85,14 @@ def get_scatter_plot(feature1, feature2):
|
|
| 86 |
plt.savefig("scatter_plot.png")
|
| 87 |
return "scatter_plot.png"
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# Create Gradio interface
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
gr.Markdown("# Isolation Forest Anomaly Detection")
|
|
@@ -111,5 +118,11 @@ with gr.Blocks() as demo:
|
|
| 111 |
scatter_image = gr.Image()
|
| 112 |
scatter_button.click(get_scatter_plot, inputs=[feature1_dropdown, feature2_dropdown], outputs=scatter_image)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# Launch the Gradio app
|
| 115 |
demo.launch()
|
|
|
|
| 4 |
from sklearn.ensemble import IsolationForest
|
| 5 |
import shap
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
# Generate synthetic data with 20 features
|
|
|
|
| 85 |
plt.savefig("scatter_plot.png")
|
| 86 |
return "scatter_plot.png"
|
| 87 |
|
| 88 |
+
def get_anomaly_samples():
|
| 89 |
+
"""Returns top, middle, and bottom 10 records based on anomaly score."""
|
| 90 |
+
sorted_df = df.sort_values("Anomaly_Score", ascending=False)
|
| 91 |
+
top_10 = sorted_df.head(10).to_dict(orient="records")
|
| 92 |
+
middle_10 = sorted_df.iloc[len(sorted_df) // 2 - 5: len(sorted_df) // 2 + 5].to_dict(orient="records")
|
| 93 |
+
bottom_10 = sorted_df.tail(10).to_dict(orient="records")
|
| 94 |
+
return {"Top 10 Anomalies": top_10, "Middle 10 Records": middle_10, "Bottom 10 Scores": bottom_10}
|
| 95 |
+
|
| 96 |
# Create Gradio interface
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("# Isolation Forest Anomaly Detection")
|
|
|
|
| 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("### View Anomaly Scores")
|
| 123 |
+
anomaly_samples_button = gr.Button("Get Anomaly Samples")
|
| 124 |
+
anomaly_samples_output = gr.JSON()
|
| 125 |
+
anomaly_samples_button.click(get_anomaly_samples, outputs=anomaly_samples_output)
|
| 126 |
+
|
| 127 |
# Launch the Gradio app
|
| 128 |
demo.launch()
|