Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,14 +8,13 @@ from sklearn.linear_model import SGDOneClassSVM
|
|
| 8 |
from sklearn.kernel_approximation import Nystroem
|
| 9 |
from sklearn.pipeline import make_pipeline
|
| 10 |
from sklearn.datasets import make_blobs, make_moons
|
| 11 |
-
import gradio as gr
|
| 12 |
-
import tensorflow as tf
|
| 13 |
from tensorflow.keras.models import Sequential
|
| 14 |
-
from tensorflow.keras.layers import Dense
|
| 15 |
from tensorflow.keras.optimizers import Adam
|
|
|
|
| 16 |
import time
|
| 17 |
|
| 18 |
-
#
|
| 19 |
def prepare_data(input_data, n_samples, outliers_fraction=0.01):
|
| 20 |
n_outliers = max(int(outliers_fraction * n_samples), 1)
|
| 21 |
n_inliers = n_samples - n_outliers
|
|
@@ -33,7 +32,7 @@ def prepare_data(input_data, n_samples, outliers_fraction=0.01):
|
|
| 33 |
X = np.concatenate([X, outliers], axis=0)
|
| 34 |
return X
|
| 35 |
|
| 36 |
-
#
|
| 37 |
def build_autoencoder(input_dim):
|
| 38 |
model = Sequential([
|
| 39 |
Dense(64, activation='relu', input_dim=input_dim),
|
|
@@ -53,7 +52,18 @@ def autoencoder_anomaly_detection(X, outliers_fraction=0.01, epochs=50):
|
|
| 53 |
y_pred = (reconstruction_error > threshold).astype(int)
|
| 54 |
return y_pred
|
| 55 |
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 58 |
X = prepare_data(input_data, n_samples, outliers_fraction)
|
| 59 |
NAME_CLF_MAPPING = {
|
|
@@ -67,34 +77,57 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 67 |
"Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction),
|
| 68 |
"Autoencoders": autoencoder_anomaly_detection(X, outliers_fraction)
|
| 69 |
}
|
|
|
|
| 70 |
if clf_name == "Autoencoders":
|
| 71 |
-
y_pred =
|
| 72 |
else:
|
| 73 |
-
clf = NAME_CLF_MAPPING[clf_name]
|
| 74 |
if clf_name == "Local Outlier Factor":
|
| 75 |
y_pred = clf.fit_predict(X)
|
| 76 |
else:
|
| 77 |
clf.fit(X)
|
| 78 |
y_pred = clf.predict(X)
|
|
|
|
|
|
|
| 79 |
plt.figure(figsize=(5, 5))
|
| 80 |
colors = np.array(["#377eb8", "#ff7f00"])
|
| 81 |
plt.scatter(X[:, 0], X[:, 1], c=colors[(y_pred + 1) // 2], s=20)
|
| 82 |
plt.title(clf_name)
|
| 83 |
return plt.gcf()
|
| 84 |
|
| 85 |
-
#
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("## Anomaly Detection Comparison App")
|
|
|
|
|
|
|
|
|
|
| 88 |
input_data = gr.Radio(choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"], value="Moons", label="Dataset")
|
|
|
|
|
|
|
| 89 |
n_samples = gr.Slider(minimum=50, maximum=2000, step=50, value=500, label="Number of Samples")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
outliers_fraction = gr.Slider(minimum=0.01, maximum=0.5, step=0.01, value=0.05, label="Fraction of Outliers")
|
| 91 |
input_models = gr.Radio(
|
| 92 |
choices=["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor", "Autoencoders"],
|
| 93 |
value="Isolation Forest",
|
| 94 |
label="Select Model"
|
| 95 |
)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
demo.launch()
|
|
|
|
| 8 |
from sklearn.kernel_approximation import Nystroem
|
| 9 |
from sklearn.pipeline import make_pipeline
|
| 10 |
from sklearn.datasets import make_blobs, make_moons
|
|
|
|
|
|
|
| 11 |
from tensorflow.keras.models import Sequential
|
| 12 |
+
from tensorflow.keras.layers import Dense
|
| 13 |
from tensorflow.keras.optimizers import Adam
|
| 14 |
+
import gradio as gr
|
| 15 |
import time
|
| 16 |
|
| 17 |
+
# Helper function: Prepare data
|
| 18 |
def prepare_data(input_data, n_samples, outliers_fraction=0.01):
|
| 19 |
n_outliers = max(int(outliers_fraction * n_samples), 1)
|
| 20 |
n_inliers = n_samples - n_outliers
|
|
|
|
| 32 |
X = np.concatenate([X, outliers], axis=0)
|
| 33 |
return X
|
| 34 |
|
| 35 |
+
# Autoencoder Anomaly Detection
|
| 36 |
def build_autoencoder(input_dim):
|
| 37 |
model = Sequential([
|
| 38 |
Dense(64, activation='relu', input_dim=input_dim),
|
|
|
|
| 52 |
y_pred = (reconstruction_error > threshold).astype(int)
|
| 53 |
return y_pred
|
| 54 |
|
| 55 |
+
# Function to generate scatter plots
|
| 56 |
+
def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples):
|
| 57 |
+
X = prepare_data(input_data, n_samples)
|
| 58 |
+
plt.figure(figsize=(6, 6))
|
| 59 |
+
plt.scatter(X[:, 0], X[:, 1], alpha=0.8, c="blue", s=20, label="Data Points")
|
| 60 |
+
plt.title(f"Feature Interaction Scatter Plot - {feature_x} vs {feature_y}")
|
| 61 |
+
plt.xlabel(feature_x)
|
| 62 |
+
plt.ylabel(feature_y)
|
| 63 |
+
plt.legend()
|
| 64 |
+
return plt.gcf()
|
| 65 |
+
|
| 66 |
+
# Function to train models and generate comparison plots
|
| 67 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 68 |
X = prepare_data(input_data, n_samples, outliers_fraction)
|
| 69 |
NAME_CLF_MAPPING = {
|
|
|
|
| 77 |
"Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction),
|
| 78 |
"Autoencoders": autoencoder_anomaly_detection(X, outliers_fraction)
|
| 79 |
}
|
| 80 |
+
clf = NAME_CLF_MAPPING.get(clf_name, None)
|
| 81 |
if clf_name == "Autoencoders":
|
| 82 |
+
y_pred = clf
|
| 83 |
else:
|
|
|
|
| 84 |
if clf_name == "Local Outlier Factor":
|
| 85 |
y_pred = clf.fit_predict(X)
|
| 86 |
else:
|
| 87 |
clf.fit(X)
|
| 88 |
y_pred = clf.predict(X)
|
| 89 |
+
|
| 90 |
+
# Plot results
|
| 91 |
plt.figure(figsize=(5, 5))
|
| 92 |
colors = np.array(["#377eb8", "#ff7f00"])
|
| 93 |
plt.scatter(X[:, 0], X[:, 1], c=colors[(y_pred + 1) // 2], s=20)
|
| 94 |
plt.title(clf_name)
|
| 95 |
return plt.gcf()
|
| 96 |
|
| 97 |
+
# Gradio Interface
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
gr.Markdown("## Anomaly Detection Comparison App")
|
| 100 |
+
|
| 101 |
+
# Interactive Scatter Plot
|
| 102 |
+
gr.Markdown("### Interactive Feature Scatter Plot")
|
| 103 |
input_data = gr.Radio(choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"], value="Moons", label="Dataset")
|
| 104 |
+
feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
|
| 105 |
+
feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
|
| 106 |
n_samples = gr.Slider(minimum=50, maximum=2000, step=50, value=500, label="Number of Samples")
|
| 107 |
+
scatter_plot_button = gr.Button("Generate Scatter Plot")
|
| 108 |
+
scatter_plot = gr.Plot(label="Feature Scatter Plot")
|
| 109 |
+
|
| 110 |
+
scatter_plot_button.click(
|
| 111 |
+
fn=plot_interactive_feature_scatter,
|
| 112 |
+
inputs=[input_data, feature_x, feature_y, n_samples],
|
| 113 |
+
outputs=scatter_plot,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Compare Anomaly Detection Algorithms
|
| 117 |
+
gr.Markdown("### Compare Anomaly Detection Algorithms")
|
| 118 |
outliers_fraction = gr.Slider(minimum=0.01, maximum=0.5, step=0.01, value=0.05, label="Fraction of Outliers")
|
| 119 |
input_models = gr.Radio(
|
| 120 |
choices=["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor", "Autoencoders"],
|
| 121 |
value="Isolation Forest",
|
| 122 |
label="Select Model"
|
| 123 |
)
|
| 124 |
+
comparison_plot = gr.Plot(label="Model Comparison Results")
|
| 125 |
+
generate_comparison_plot = gr.Button("Generate Comparison Plot")
|
| 126 |
+
|
| 127 |
+
generate_comparison_plot.click(
|
| 128 |
+
fn=train_models,
|
| 129 |
+
inputs=[input_data, outliers_fraction, n_samples, input_models],
|
| 130 |
+
outputs=comparison_plot,
|
| 131 |
+
)
|
| 132 |
|
| 133 |
demo.launch()
|