import gradio as gr import pandas as pd from sklearn.ensemble import IsolationForest def detect_anomalies(log_text): lines = log_text.strip().split("\n") df = pd.DataFrame({"log": lines}) df["length"] = df["log"].apply(len) df["digits"] = df["log"].apply(lambda x: sum(c.isdigit() for c in x)) df["specials"] = df["log"].apply(lambda x: sum(not c.isalnum() for c in x)) model = IsolationForest(contamination=0.1, random_state=42) preds = model.fit_predict(df[["length", "digits", "specials"]]) df["anomaly"] = preds df["status"] = df["anomaly"].map({1: "Normal", -1: "Anomaly"}) return df[["log", "status"]] demo = gr.Interface( fn=detect_anomalies, inputs=gr.Textbox(lines=20, placeholder="Paste your logs here..."), outputs=gr.Dataframe(), title="🧭 Log Anomaly Detection (AIOps)", description="Paste logs to detect anomalies using Isolation Forest. Lightweight, fast, and deploy-safe." ) demo.launch()