Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -155,42 +155,63 @@ def plot_anomaly_examples(input_data, n_samples, outliers_fraction):
|
|
| 155 |
|
| 156 |
# Gradio Interface
|
| 157 |
with gr.Blocks() as demo:
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
input_data = gr.Radio(
|
| 162 |
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
| 163 |
value="Moons",
|
| 164 |
label="Dataset"
|
| 165 |
)
|
| 166 |
n_samples = gr.Slider(
|
| 167 |
-
minimum=
|
| 168 |
)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
| 172 |
with gr.Row():
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
scatter_plot = gr.Plot(label="Interactive Feature Scatter Plot")
|
| 178 |
scatter_plot_button.click(
|
| 179 |
fn=plot_interactive_feature_scatter,
|
| 180 |
inputs=[input_data, feature_x, feature_y, n_samples],
|
| 181 |
outputs=scatter_plot,
|
| 182 |
)
|
| 183 |
-
|
| 184 |
-
#
|
| 185 |
-
gr.Markdown("### Anomaly Examples")
|
| 186 |
-
|
| 187 |
-
outliers_fraction = gr.Slider(
|
| 188 |
-
minimum=0.01, maximum=0.99, step=0.01, value=0.2, label="Fraction of Outliers"
|
| 189 |
-
)
|
| 190 |
-
anomaly_plot_button = gr.Button("Generate Anomaly Examples")
|
| 191 |
-
|
| 192 |
anomaly_plot = gr.Plot(label="Anomaly Examples")
|
| 193 |
-
|
|
|
|
| 194 |
fn=plot_anomaly_examples,
|
| 195 |
inputs=[input_data, n_samples, outliers_fraction],
|
| 196 |
outputs=anomaly_plot,
|
|
|
|
| 155 |
|
| 156 |
# Gradio Interface
|
| 157 |
with gr.Blocks() as demo:
|
| 158 |
+
# App Title and Description
|
| 159 |
+
gr.Markdown("## 🕵️♀️ Anomaly Detection App 🕵️♂️")
|
| 160 |
+
gr.Markdown("Explore anomaly detection models, feature interactions, and anomaly examples.")
|
| 161 |
+
|
| 162 |
+
# Anomaly Detection Comparison
|
| 163 |
+
gr.Markdown("### 1. Compare Anomaly Detection Algorithms")
|
| 164 |
input_data = gr.Radio(
|
| 165 |
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
| 166 |
value="Moons",
|
| 167 |
label="Dataset"
|
| 168 |
)
|
| 169 |
n_samples = gr.Slider(
|
| 170 |
+
minimum=10, maximum=10000, step=25, value=500, label="Number of Samples"
|
| 171 |
)
|
| 172 |
+
outliers_fraction = gr.Slider(
|
| 173 |
+
minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers"
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
|
| 177 |
+
plots = []
|
| 178 |
with gr.Row():
|
| 179 |
+
for model_name in input_models:
|
| 180 |
+
plot = gr.Plot(label=model_name)
|
| 181 |
+
plots.append((model_name, plot))
|
| 182 |
+
|
| 183 |
+
def update_anomaly_comparison(input_data, outliers_fraction, n_samples):
|
| 184 |
+
results = []
|
| 185 |
+
for clf_name, plot in plots:
|
| 186 |
+
fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
|
| 187 |
+
results.append(fig)
|
| 188 |
+
return results
|
| 189 |
+
|
| 190 |
+
anomaly_inputs = [input_data, outliers_fraction, n_samples]
|
| 191 |
+
anomaly_outputs = [plot for _, plot in plots]
|
| 192 |
+
input_data.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 193 |
+
n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 194 |
+
outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 195 |
+
|
| 196 |
+
# Interactive Feature Scatter Plot
|
| 197 |
+
gr.Markdown("### 2. Interactive Feature Scatter Plot")
|
| 198 |
+
feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
|
| 199 |
+
feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
|
| 200 |
+
scatter_plot_button = gr.Button("Generate Scatter Plot")
|
| 201 |
+
scatter_plot = gr.Plot(label="Feature Scatter Plot")
|
| 202 |
|
|
|
|
| 203 |
scatter_plot_button.click(
|
| 204 |
fn=plot_interactive_feature_scatter,
|
| 205 |
inputs=[input_data, feature_x, feature_y, n_samples],
|
| 206 |
outputs=scatter_plot,
|
| 207 |
)
|
| 208 |
+
|
| 209 |
+
# Anomaly Examples Visualization
|
| 210 |
+
gr.Markdown("### 3. Anomaly Examples Visualization")
|
| 211 |
+
anomaly_examples_button = gr.Button("Generate Anomaly Examples")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
anomaly_plot = gr.Plot(label="Anomaly Examples")
|
| 213 |
+
|
| 214 |
+
anomaly_examples_button.click(
|
| 215 |
fn=plot_anomaly_examples,
|
| 216 |
inputs=[input_data, n_samples, outliers_fraction],
|
| 217 |
outputs=anomaly_plot,
|