Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.ensemble import IsolationForest
|
| 4 |
+
|
| 5 |
+
def detect_anomalies(log_text):
|
| 6 |
+
lines = log_text.strip().split("\n")
|
| 7 |
+
df = pd.DataFrame({"log": lines})
|
| 8 |
+
df["length"] = df["log"].apply(len)
|
| 9 |
+
df["digits"] = df["log"].apply(lambda x: sum(c.isdigit() for c in x))
|
| 10 |
+
df["specials"] = df["log"].apply(lambda x: sum(not c.isalnum() for c in x))
|
| 11 |
+
|
| 12 |
+
model = IsolationForest(contamination=0.1, random_state=42)
|
| 13 |
+
preds = model.fit_predict(df[["length", "digits", "specials"]])
|
| 14 |
+
df["anomaly"] = preds
|
| 15 |
+
df["status"] = df["anomaly"].map({1: "Normal", -1: "Anomaly"})
|
| 16 |
+
|
| 17 |
+
return df[["log", "status"]]
|
| 18 |
+
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=detect_anomalies,
|
| 21 |
+
inputs=gr.Textbox(lines=20, placeholder="Paste your logs here..."),
|
| 22 |
+
outputs=gr.Dataframe(),
|
| 23 |
+
title="🧭 Log Anomaly Detection (AIOps)",
|
| 24 |
+
description="Paste logs to detect anomalies using Isolation Forest. Lightweight, fast, and deploy-safe."
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
demo.launch()
|