Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -140,17 +140,24 @@ def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples
|
|
| 140 |
|
| 141 |
|
| 142 |
# Function for anomaly examples (Optional feature row)
|
| 143 |
-
def
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
# Gradio Interface
|
|
@@ -206,15 +213,21 @@ with gr.Blocks() as demo:
|
|
| 206 |
outputs=scatter_plot,
|
| 207 |
)
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
)
|
| 219 |
|
| 220 |
demo.launch(debug=True)
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
# Function for anomaly examples (Optional feature row)
|
| 143 |
+
def get_anomaly_samples():
|
| 144 |
+
"""Returns formatted top, middle, and bottom 10 records based on anomaly score."""
|
| 145 |
+
sorted_df = df.sort_values("Anomaly_Score", ascending=False)
|
| 146 |
+
|
| 147 |
+
# Top 10 anomalies
|
| 148 |
+
top_10 = sorted_df[sorted_df["Anomaly_Label"] == "Anomaly"].head(10)
|
| 149 |
+
|
| 150 |
+
# Middle 10 (mix of anomalies and normal)
|
| 151 |
+
mid_start = len(sorted_df) // 2 - 50 # Get a broader middle slice
|
| 152 |
+
middle_section = sorted_df.iloc[mid_start: mid_start + 100] # Consider a larger middle slice
|
| 153 |
+
middle_anomalies = middle_section[middle_section["Anomaly_Label"] == "Anomaly"].sample(n=5, random_state=42)
|
| 154 |
+
middle_normals = middle_section[middle_section["Anomaly_Label"] == "Normal"].sample(n=5, random_state=42)
|
| 155 |
+
middle_10 = pd.concat([middle_anomalies, middle_normals]).sort_values("Anomaly_Score", ascending=False)
|
| 156 |
+
|
| 157 |
+
# Bottom 10 normal records
|
| 158 |
+
bottom_10 = sorted_df[sorted_df["Anomaly_Label"] == "Normal"].tail(10)
|
| 159 |
+
|
| 160 |
+
return top_10, middle_10, bottom_10
|
| 161 |
|
| 162 |
|
| 163 |
# Gradio Interface
|
|
|
|
| 213 |
outputs=scatter_plot,
|
| 214 |
)
|
| 215 |
|
| 216 |
+
with gr.Tab("Anomaly Samples"):
|
| 217 |
+
gr.HTML("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Top 10 Records (Anomalies)</h3>")
|
| 218 |
+
top_table = gr.Dataframe(label="Top 10 Records")
|
| 219 |
+
|
| 220 |
+
gr.HTML("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Middle 10 Records (Mixed)</h3>")
|
| 221 |
+
middle_table = gr.Dataframe(label="Middle 10 Records")
|
| 222 |
+
|
| 223 |
+
gr.HTML("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Bottom 10 Records (Normal)</h3>")
|
| 224 |
+
bottom_table = gr.Dataframe(label="Bottom 10 Records")
|
| 225 |
+
|
| 226 |
+
anomaly_samples_button = gr.Button("Show Anomaly Samples")
|
| 227 |
+
anomaly_samples_button.click(
|
| 228 |
+
get_anomaly_samples,
|
| 229 |
+
outputs=[top_table, middle_table, bottom_table]
|
| 230 |
+
)
|
| 231 |
)
|
| 232 |
|
| 233 |
demo.launch(debug=True)
|