Spaces:
Runtime error
Runtime error
Upload 10 files
Browse files- app.py +671 -0
- decision_tree_model.pkl +3 -0
- features.pkl +3 -0
- logistic_regression_model.pkl +3 -0
- metrics.pkl +3 -0
- metrics_summary.json +23 -0
- model.pkl +3 -0
- requirements.txt +10 -0
- scaler.pkl +3 -0
- svm_model.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,671 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import datetime
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
import matplotlib
|
| 9 |
+
matplotlib.use("Agg")
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import matplotlib.patches as mpatches
|
| 12 |
+
from matplotlib.gridspec import GridSpec
|
| 13 |
+
|
| 14 |
+
# ββ Load artifacts βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
+
dt_model = joblib.load("models/decision_tree_model.pkl")
|
| 16 |
+
lr_model = joblib.load("models/logistic_regression_model.pkl")
|
| 17 |
+
svm_model = joblib.load("models/svm_model.pkl")
|
| 18 |
+
scaler = joblib.load("models/scaler.pkl")
|
| 19 |
+
features = joblib.load("models/features.pkl")
|
| 20 |
+
|
| 21 |
+
MODELS = {
|
| 22 |
+
"π³ Decision Tree": (dt_model, False),
|
| 23 |
+
"π Logistic Regression": (lr_model, True),
|
| 24 |
+
"β‘ SVM (RBF Kernel)": (svm_model, True),
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Load pre-computed training metrics if available
|
| 28 |
+
TRAIN_METRICS = {}
|
| 29 |
+
_mp = "models/metrics_summary.json"
|
| 30 |
+
if os.path.exists(_mp):
|
| 31 |
+
with open(_mp) as f:
|
| 32 |
+
TRAIN_METRICS = json.load(f)
|
| 33 |
+
|
| 34 |
+
# ββ Session state ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
session_log = []
|
| 36 |
+
total_scanned = 0
|
| 37 |
+
total_attacks = 0
|
| 38 |
+
attack_types = {"DoS": 0, "Probe": 0, "R2L": 0, "U2R": 0, "Normal": 0}
|
| 39 |
+
|
| 40 |
+
# ββ Feature metadata βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
+
FEATURE_INFO = {
|
| 42 |
+
"serror_rate": "SYN error rate β high = DoS/SYN-flood",
|
| 43 |
+
"srv_serror_rate": "SYN error rate for same service",
|
| 44 |
+
"dst_host_serror_rate": "SYN error rate at destination host",
|
| 45 |
+
"dst_host_srv_serror_rate": "SYN error rate for dest-host service",
|
| 46 |
+
"same_srv_rate": "% connections to same service",
|
| 47 |
+
"diff_srv_rate": "% connections to diff services (scan indicator)",
|
| 48 |
+
"dst_host_same_srv_rate": "Rate of same-service connections at dest host",
|
| 49 |
+
"dst_host_srv_count": "# connections to same service on dest host",
|
| 50 |
+
"count": "# connections to same host (last 2 s)",
|
| 51 |
+
"srv_count": "# connections to same service (last 2 s)",
|
| 52 |
+
"dst_host_count": "# connections to dest host",
|
| 53 |
+
"logged_in": "1 = login successful, 0 = not logged in",
|
| 54 |
+
"flag_sf": "SF = normal successful connection",
|
| 55 |
+
"flag_s0": "S0 = incomplete connection (suspicious)",
|
| 56 |
+
"service_http": "1 = HTTP/web service traffic",
|
| 57 |
+
"src_bytes": "Bytes sent from source to destination",
|
| 58 |
+
"dst_bytes": "Bytes sent from destination to source",
|
| 59 |
+
"duration": "Connection duration in seconds",
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
ATTACK_TIPS = {
|
| 63 |
+
"CRITICAL": "β‘ Immediate action β block source IP and alert SOC team.",
|
| 64 |
+
"HIGH": "π΄ High-risk β investigate source, log for forensic review.",
|
| 65 |
+
"MEDIUM": "π‘ Suspicious pattern β monitor closely, review connection logs.",
|
| 66 |
+
"LOW": "π’ Low-confidence β continue passive monitoring.",
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# ββ Colors βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
+
DARK_BG = "#0a0e1a"
|
| 71 |
+
PANEL_BG = "#0d1526"
|
| 72 |
+
CARD_BG = "#111d35"
|
| 73 |
+
CYAN = "#00d4ff"
|
| 74 |
+
RED = "#ff3c6e"
|
| 75 |
+
GREEN = "#39ff14"
|
| 76 |
+
YELLOW = "#f5a623"
|
| 77 |
+
PURPLE = "#c084fc"
|
| 78 |
+
TEXT = "#c8e6ff"
|
| 79 |
+
GRID_COL = "#1e3a5a"
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# ββ Attack type inference ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 83 |
+
def infer_attack_type(data: dict) -> tuple:
|
| 84 |
+
serror = float(data.get("serror_rate", 0))
|
| 85 |
+
srv_serr = float(data.get("srv_serror_rate", 0))
|
| 86 |
+
dh_serr = float(data.get("dst_host_serror_rate", 0))
|
| 87 |
+
diff_srv = float(data.get("diff_srv_rate", 0))
|
| 88 |
+
cnt = float(data.get("count", 0))
|
| 89 |
+
srv_cnt = float(data.get("srv_count", 0))
|
| 90 |
+
logged = float(data.get("logged_in", 0))
|
| 91 |
+
src_b = float(data.get("src_bytes", 0))
|
| 92 |
+
dst_b = float(data.get("dst_bytes", 0))
|
| 93 |
+
flag_s0 = float(data.get("flag_s0", 0))
|
| 94 |
+
|
| 95 |
+
if (serror > 0.5 or srv_serr > 0.5 or dh_serr > 0.5 or flag_s0 == 1) and cnt > 50:
|
| 96 |
+
return ("DoS",
|
| 97 |
+
"High SYN/connection error rate with large connection count β "
|
| 98 |
+
"classic Denial-of-Service pattern (neptune, smurf, pod).")
|
| 99 |
+
|
| 100 |
+
if diff_srv > 0.5 and cnt > 30 and serror < 0.3:
|
| 101 |
+
return ("Probe",
|
| 102 |
+
"High proportion of connections to different services β "
|
| 103 |
+
"network scanning / probing detected (portsweep, nmap).")
|
| 104 |
+
|
| 105 |
+
if logged == 1 and src_b > 0 and dst_b < src_b * 0.1 and cnt < 10:
|
| 106 |
+
return ("R2L",
|
| 107 |
+
"Authenticated session with unusual byte asymmetry β "
|
| 108 |
+
"possible remote-to-local exploit (ftp_write, guess_passwd).")
|
| 109 |
+
|
| 110 |
+
if logged == 1 and cnt < 5 and srv_cnt < 5 and src_b < 500:
|
| 111 |
+
return ("U2R",
|
| 112 |
+
"Very low traffic volume with successful login β "
|
| 113 |
+
"possible privilege escalation (buffer_overflow).")
|
| 114 |
+
|
| 115 |
+
return ("Unknown Attack",
|
| 116 |
+
"Does not clearly match DoS, Probe, R2L, or U2R β "
|
| 117 |
+
"could be a novel or combined attack vector.")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# ββ Chart helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 121 |
+
def _dark(fig, axes):
|
| 122 |
+
fig.patch.set_facecolor(DARK_BG)
|
| 123 |
+
for ax in axes:
|
| 124 |
+
ax.set_facecolor(PANEL_BG)
|
| 125 |
+
ax.tick_params(colors=TEXT, labelsize=8)
|
| 126 |
+
ax.xaxis.label.set_color(TEXT)
|
| 127 |
+
ax.yaxis.label.set_color(TEXT)
|
| 128 |
+
ax.title.set_color(CYAN)
|
| 129 |
+
for sp in ax.spines.values():
|
| 130 |
+
sp.set_edgecolor(GRID_COL)
|
| 131 |
+
ax.grid(color=GRID_COL, linewidth=0.5, alpha=0.6)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def radar_chart(values, feat_names, title):
|
| 135 |
+
N = len(values)
|
| 136 |
+
angles = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist()
|
| 137 |
+
vals = values + [values[0]]
|
| 138 |
+
angles += [angles[0]]
|
| 139 |
+
|
| 140 |
+
fig, ax = plt.subplots(figsize=(4.5, 4.5), subplot_kw=dict(polar=True))
|
| 141 |
+
fig.patch.set_facecolor(DARK_BG)
|
| 142 |
+
ax.set_facecolor(PANEL_BG)
|
| 143 |
+
ax.plot(angles, vals, color=CYAN, linewidth=2)
|
| 144 |
+
ax.fill(angles, vals, color=CYAN, alpha=0.18)
|
| 145 |
+
ax.set_xticks(angles[:-1])
|
| 146 |
+
short = [f.replace("dst_host_", "dh_").replace("serror", "serr")
|
| 147 |
+
.replace("_rate", "_r") for f in feat_names]
|
| 148 |
+
ax.set_xticklabels(short, color=TEXT, size=7)
|
| 149 |
+
ax.set_yticklabels([], color=TEXT)
|
| 150 |
+
ax.tick_params(colors=TEXT)
|
| 151 |
+
ax.spines["polar"].set_color(GRID_COL)
|
| 152 |
+
ax.grid(color=GRID_COL, linewidth=0.5)
|
| 153 |
+
ax.set_title(title, color=CYAN, pad=14, fontsize=10, fontweight="bold")
|
| 154 |
+
plt.tight_layout()
|
| 155 |
+
return fig
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def confidence_chart(vote_results):
|
| 159 |
+
names = list(vote_results.keys())
|
| 160 |
+
confs = [vote_results[n]["confidence"] for n in names]
|
| 161 |
+
colors = [RED if vote_results[n]["is_attack"] else GREEN for n in names]
|
| 162 |
+
|
| 163 |
+
fig, ax = plt.subplots(figsize=(5, 2.6))
|
| 164 |
+
bars = ax.barh(names, confs, color=colors, height=0.45, edgecolor=GRID_COL)
|
| 165 |
+
ax.set_xlim(0, 110)
|
| 166 |
+
ax.set_xlabel("Confidence (%)")
|
| 167 |
+
ax.set_title("Model Confidence Comparison", fontsize=10, fontweight="bold")
|
| 168 |
+
for bar, val in zip(bars, confs):
|
| 169 |
+
ax.text(val + 1, bar.get_y() + bar.get_height() / 2,
|
| 170 |
+
f"{val:.1f}%", va="center", color=TEXT, fontsize=9)
|
| 171 |
+
_dark(fig, [ax])
|
| 172 |
+
plt.tight_layout()
|
| 173 |
+
return fig
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def session_chart():
|
| 177 |
+
fig = plt.figure(figsize=(10, 4))
|
| 178 |
+
gs = GridSpec(1, 2, figure=fig, wspace=0.38)
|
| 179 |
+
|
| 180 |
+
# Donut: attack type breakdown
|
| 181 |
+
ax1 = fig.add_subplot(gs[0])
|
| 182 |
+
keys = [k for k, v in attack_types.items() if v > 0]
|
| 183 |
+
vals = [attack_types[k] for k in keys]
|
| 184 |
+
if vals:
|
| 185 |
+
pal = [RED, YELLOW, PURPLE, CYAN, GREEN]
|
| 186 |
+
colors = pal[:len(keys)]
|
| 187 |
+
wedges, texts, autotexts = ax1.pie(
|
| 188 |
+
vals, labels=keys, autopct="%1.0f%%", colors=colors,
|
| 189 |
+
startangle=90,
|
| 190 |
+
wedgeprops=dict(width=0.55, edgecolor=DARK_BG, linewidth=1.5),
|
| 191 |
+
textprops=dict(color=TEXT, fontsize=8))
|
| 192 |
+
for at in autotexts:
|
| 193 |
+
at.set_color(DARK_BG); at.set_fontsize(7)
|
| 194 |
+
else:
|
| 195 |
+
ax1.text(0.5, 0.5, "No scans yet", ha="center", va="center",
|
| 196 |
+
color=TEXT, fontsize=9, transform=ax1.transAxes)
|
| 197 |
+
ax1.set_title("Traffic Classification", color=CYAN, fontsize=10, fontweight="bold")
|
| 198 |
+
ax1.set_facecolor(PANEL_BG)
|
| 199 |
+
fig.patch.set_facecolor(DARK_BG)
|
| 200 |
+
|
| 201 |
+
# Bar: scan history
|
| 202 |
+
ax2 = fig.add_subplot(gs[1])
|
| 203 |
+
recent = session_log[-15:]
|
| 204 |
+
if recent:
|
| 205 |
+
idxs = list(range(1, len(recent) + 1))
|
| 206 |
+
clrs = [RED if e["result"] == "ATTACK" else GREEN for e in recent]
|
| 207 |
+
confs = [float(e["confidence"].rstrip("%")) for e in recent]
|
| 208 |
+
ax2.bar(idxs, confs, color=clrs, edgecolor=DARK_BG, linewidth=0.8)
|
| 209 |
+
ax2.set_ylim(0, 108)
|
| 210 |
+
ax2.set_xlabel("Scan #"); ax2.set_ylabel("Confidence %")
|
| 211 |
+
ax2.set_title("Scan History (last 15)", fontsize=10, fontweight="bold")
|
| 212 |
+
ax2.legend(handles=[
|
| 213 |
+
mpatches.Patch(color=RED, label="Attack"),
|
| 214 |
+
mpatches.Patch(color=GREEN, label="Normal")],
|
| 215 |
+
fontsize=7, facecolor=CARD_BG, edgecolor=GRID_COL, labelcolor=TEXT)
|
| 216 |
+
else:
|
| 217 |
+
ax2.text(0.5, 0.5, "No scans yet", ha="center", va="center",
|
| 218 |
+
color=TEXT, fontsize=9, transform=ax2.transAxes)
|
| 219 |
+
ax2.set_title("Scan History (last 15)", fontsize=10, fontweight="bold")
|
| 220 |
+
_dark(fig, [ax2])
|
| 221 |
+
ax2.set_facecolor(PANEL_BG)
|
| 222 |
+
plt.tight_layout()
|
| 223 |
+
return fig
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def metrics_chart():
|
| 227 |
+
if not TRAIN_METRICS:
|
| 228 |
+
fig, ax = plt.subplots(figsize=(7, 3))
|
| 229 |
+
ax.text(0.5, 0.5, "Run train_models.py first to generate metrics_summary.json",
|
| 230 |
+
ha="center", va="center", color=TEXT, fontsize=9,
|
| 231 |
+
transform=ax.transAxes, wrap=True)
|
| 232 |
+
_dark(fig, [ax])
|
| 233 |
+
return fig
|
| 234 |
+
|
| 235 |
+
keys = ["accuracy", "precision", "recall", "f1", "roc_auc"]
|
| 236 |
+
labels = ["Accuracy", "Precision", "Recall", "F1", "ROC-AUC"]
|
| 237 |
+
mnames = list(TRAIN_METRICS.keys())
|
| 238 |
+
palette = [CYAN, YELLOW, RED]
|
| 239 |
+
x = np.arange(len(labels)); w = 0.22
|
| 240 |
+
|
| 241 |
+
fig, ax = plt.subplots(figsize=(8.5, 4))
|
| 242 |
+
for i, (mname, color) in enumerate(zip(mnames, palette)):
|
| 243 |
+
vals = [TRAIN_METRICS[mname].get(k, 0) for k in keys]
|
| 244 |
+
bars = ax.bar(x + i * w, vals, w, label=mname,
|
| 245 |
+
color=color, edgecolor=DARK_BG, linewidth=0.8, alpha=0.88)
|
| 246 |
+
for bar, val in zip(bars, vals):
|
| 247 |
+
ax.text(bar.get_x() + bar.get_width() / 2,
|
| 248 |
+
bar.get_height() + 0.008, f"{val:.3f}",
|
| 249 |
+
ha="center", va="bottom", color=TEXT, fontsize=6.5)
|
| 250 |
+
|
| 251 |
+
ax.set_xticks(x + w); ax.set_xticklabels(labels)
|
| 252 |
+
ax.set_ylim(0, 1.14); ax.set_ylabel("Score")
|
| 253 |
+
ax.set_title("Model Performance Comparison (Training Evaluation)",
|
| 254 |
+
fontsize=11, fontweight="bold")
|
| 255 |
+
ax.legend(facecolor=CARD_BG, edgecolor=GRID_COL, labelcolor=TEXT, fontsize=8)
|
| 256 |
+
_dark(fig, [ax])
|
| 257 |
+
plt.tight_layout()
|
| 258 |
+
return fig
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
# ββ Core prediction ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 262 |
+
def predict(selected_model_name, *args):
|
| 263 |
+
global total_scanned, total_attacks
|
| 264 |
+
|
| 265 |
+
data = dict(zip(features, args))
|
| 266 |
+
df_in = pd.DataFrame([data])[features]
|
| 267 |
+
model, needs_scale = MODELS[selected_model_name]
|
| 268 |
+
X = scaler.transform(df_in) if needs_scale else df_in.values
|
| 269 |
+
|
| 270 |
+
pred = model.predict(X)[0]
|
| 271 |
+
proba = model.predict_proba(X)[0]
|
| 272 |
+
prob_attack = proba[1]
|
| 273 |
+
prob_normal = proba[0]
|
| 274 |
+
is_attack = pred == 1
|
| 275 |
+
confidence = prob_attack * 100 if is_attack else prob_normal * 100
|
| 276 |
+
|
| 277 |
+
severity = ("NONE" if not is_attack else
|
| 278 |
+
"CRITICAL" if prob_attack >= 0.90 else
|
| 279 |
+
"HIGH" if prob_attack >= 0.70 else
|
| 280 |
+
"MEDIUM" if prob_attack >= 0.50 else "LOW")
|
| 281 |
+
|
| 282 |
+
attack_type, attack_explanation = (
|
| 283 |
+
infer_attack_type(data) if is_attack else
|
| 284 |
+
("Normal", "Traffic behaves within expected norms.")
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
# Feature importance / weight
|
| 288 |
+
if hasattr(model, "feature_importances_"):
|
| 289 |
+
imps = model.feature_importances_
|
| 290 |
+
elif hasattr(model, "coef_"):
|
| 291 |
+
imps = np.abs(model.coef_[0])
|
| 292 |
+
else:
|
| 293 |
+
imps = np.ones(len(features))
|
| 294 |
+
|
| 295 |
+
contribs = sorted(zip(features, imps, list(args)),
|
| 296 |
+
key=lambda x: abs(x[1]), reverse=True)[:3]
|
| 297 |
+
top3_text = "\n".join(
|
| 298 |
+
f" β’ {f:<36} val={v:.3f} wt={w:.4f}"
|
| 299 |
+
for f, w, v in contribs)
|
| 300 |
+
|
| 301 |
+
# All-model vote
|
| 302 |
+
vote_results = {}
|
| 303 |
+
for mname, (m, scaled) in MODELS.items():
|
| 304 |
+
Xv = scaler.transform(df_in) if scaled else df_in.values
|
| 305 |
+
p = m.predict(Xv)[0]
|
| 306 |
+
pr = m.predict_proba(Xv)[0]
|
| 307 |
+
atk = p == 1
|
| 308 |
+
cf = pr[1] * 100 if atk else pr[0] * 100
|
| 309 |
+
short = mname.split(" ", 1)[1].split("(")[0].strip()
|
| 310 |
+
vote_results[short] = {"is_attack": atk, "confidence": cf}
|
| 311 |
+
|
| 312 |
+
# Session update
|
| 313 |
+
total_scanned += 1
|
| 314 |
+
if is_attack:
|
| 315 |
+
total_attacks += 1
|
| 316 |
+
attack_types[attack_type] = attack_types.get(attack_type, 0) + 1
|
| 317 |
+
else:
|
| 318 |
+
attack_types["Normal"] += 1
|
| 319 |
+
|
| 320 |
+
ts = datetime.datetime.now().strftime("%H:%M:%S")
|
| 321 |
+
session_log.append({
|
| 322 |
+
"time": ts,
|
| 323 |
+
"result": "ATTACK" if is_attack else "NORMAL",
|
| 324 |
+
"severity": severity,
|
| 325 |
+
"confidence": f"{confidence:.1f}%",
|
| 326 |
+
"type": attack_type,
|
| 327 |
+
})
|
| 328 |
+
|
| 329 |
+
# Format result
|
| 330 |
+
border = "β" * 54 if is_attack else "β" * 54
|
| 331 |
+
cb = "β" * int(confidence / 5) + "β" * (20 - int(confidence / 5))
|
| 332 |
+
tip = ATTACK_TIPS.get(severity, "")
|
| 333 |
+
status = f"π¨ ATTACK DETECTED Β· {severity}" if is_attack else "β
NORMAL TRAFFIC"
|
| 334 |
+
vote_lines = "\n".join(
|
| 335 |
+
f" {'βοΈ' if v['is_attack'] else 'β
'} {n:<28} {v['confidence']:.1f}%"
|
| 336 |
+
for n, v in vote_results.items())
|
| 337 |
+
|
| 338 |
+
result_text = (
|
| 339 |
+
f"{border}\n {status}\n{border}\n\n"
|
| 340 |
+
f" Confidence : [{cb}] {confidence:.2f}%\n"
|
| 341 |
+
f" Model Used : {selected_model_name}\n"
|
| 342 |
+
f" Timestamp : {ts}\n\n"
|
| 343 |
+
)
|
| 344 |
+
if is_attack:
|
| 345 |
+
result_text += (
|
| 346 |
+
f" Attack Type : {attack_type}\n"
|
| 347 |
+
f" Explanation : {attack_explanation}\n\n"
|
| 348 |
+
f" Severity : {severity}\n"
|
| 349 |
+
f" Advice : {tip}\n\n"
|
| 350 |
+
)
|
| 351 |
+
result_text += (
|
| 352 |
+
f" Top Contributing Features:\n{top3_text}\n\n"
|
| 353 |
+
f" ββ All-Model Consensus βββββββββββββββββββββββββββββ\n"
|
| 354 |
+
f"{vote_lines}\n{border}"
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
# Stats
|
| 358 |
+
rate = (total_attacks / total_scanned * 100) if total_scanned else 0
|
| 359 |
+
stats_text = (
|
| 360 |
+
f"π SESSION STATISTICS\n{'β'*32}\n"
|
| 361 |
+
f" Total Scanned : {total_scanned}\n"
|
| 362 |
+
f" Attacks Found : {total_attacks}\n"
|
| 363 |
+
f" Normal Traffic : {total_scanned - total_attacks}\n"
|
| 364 |
+
f" Attack Rate : {rate:.1f}%\n\n"
|
| 365 |
+
f" Attack Types Seen:\n" +
|
| 366 |
+
"".join(f" {k:<18} {v}\n" for k, v in attack_types.items() if v > 0)
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
+
# History
|
| 370 |
+
recent = session_log[-8:][::-1]
|
| 371 |
+
hist = ["π RECENT PREDICTIONS\n" + "β" * 46] + [
|
| 372 |
+
f" {'π΄' if e['result']=='ATTACK' else 'π’'} {e['time']} "
|
| 373 |
+
f"{e['result']:<7} {e.get('type','β'):<18} {e['confidence']}"
|
| 374 |
+
for e in recent]
|
| 375 |
+
history_text = "\n".join(hist)
|
| 376 |
+
|
| 377 |
+
# Normalise feature values β [0,1] for radar
|
| 378 |
+
norm = []
|
| 379 |
+
for feat, val in zip(features, args):
|
| 380 |
+
fv = float(val)
|
| 381 |
+
if "rate" in feat or feat.startswith("flag_") or feat in ["logged_in","service_http"]:
|
| 382 |
+
norm.append(min(fv, 1.0))
|
| 383 |
+
elif "count" in feat:
|
| 384 |
+
norm.append(min(fv / 255.0, 1.0))
|
| 385 |
+
else:
|
| 386 |
+
norm.append(min(fv / max(fv, 10000.0), 1.0))
|
| 387 |
+
|
| 388 |
+
return (result_text, stats_text, history_text,
|
| 389 |
+
radar_chart(norm, features, "Input Feature Profile"),
|
| 390 |
+
confidence_chart(vote_results),
|
| 391 |
+
session_chart())
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
def reset_session():
|
| 395 |
+
global session_log, total_scanned, total_attacks, attack_types
|
| 396 |
+
session_log = []
|
| 397 |
+
total_scanned = 0
|
| 398 |
+
total_attacks = 0
|
| 399 |
+
attack_types = {"DoS": 0, "Probe": 0, "R2L": 0, "U2R": 0, "Normal": 0}
|
| 400 |
+
return (
|
| 401 |
+
"β"*54 + "\n Session cleared. Ready for new scan.\n" + "β"*54,
|
| 402 |
+
"π SESSION STATISTICS\n" + "β"*32 + "\n No data yet.",
|
| 403 |
+
"π RECENT PREDICTIONS\n" + "β"*46 + "\n No predictions yet.",
|
| 404 |
+
None, None, session_chart()
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
# ββ Build input widgets ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 409 |
+
inputs = []
|
| 410 |
+
for feature in features:
|
| 411 |
+
info = FEATURE_INFO.get(feature, "Network traffic feature")
|
| 412 |
+
if "rate" in feature:
|
| 413 |
+
inputs.append(gr.Slider(0, 1, value=0, step=0.01, label=feature, info=info))
|
| 414 |
+
elif feature.startswith("flag_") or feature in ["logged_in", "service_http"]:
|
| 415 |
+
inputs.append(gr.Radio([0, 1], value=0, label=feature, info=info))
|
| 416 |
+
elif "count" in feature:
|
| 417 |
+
inputs.append(gr.Slider(0, 255, value=0, step=1, label=feature, info=info))
|
| 418 |
+
else:
|
| 419 |
+
inputs.append(gr.Number(value=0, label=feature, info=info))
|
| 420 |
+
|
| 421 |
+
flag_inputs, rate_inputs, count_inputs, other_inputs = [], [], [], []
|
| 422 |
+
for i, feature in enumerate(features):
|
| 423 |
+
if feature.startswith("flag_") or feature in ["logged_in", "service_http"]:
|
| 424 |
+
flag_inputs.append((i, inputs[i]))
|
| 425 |
+
elif "rate" in feature:
|
| 426 |
+
rate_inputs.append((i, inputs[i]))
|
| 427 |
+
elif "count" in feature:
|
| 428 |
+
count_inputs.append((i, inputs[i]))
|
| 429 |
+
else:
|
| 430 |
+
other_inputs.append((i, inputs[i]))
|
| 431 |
+
|
| 432 |
+
all_inputs = [inp for _, inp in flag_inputs + rate_inputs + count_inputs + other_inputs]
|
| 433 |
+
|
| 434 |
+
# ββ CSS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 435 |
+
CSS = """
|
| 436 |
+
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&family=Rajdhani:wght@400;600;700&display=swap');
|
| 437 |
+
:root{--bg:#0a0e1a;--panel:#0d1526;--card:#111d35;--cyan:#00d4ff;--red:#ff3c6e;
|
| 438 |
+
--green:#39ff14;--yellow:#f5a623;--text:#c8e6ff;--muted:#5a8aaa;--border:#1e3a5a;
|
| 439 |
+
--glow:0 0 14px rgba(0,212,255,0.45);}
|
| 440 |
+
body,.gradio-container{background:var(--bg)!important;font-family:'Rajdhani',sans-serif!important;color:var(--text)!important;}
|
| 441 |
+
.gradio-container h1{font-family:'Rajdhani',sans-serif!important;font-weight:700!important;font-size:2rem!important;
|
| 442 |
+
color:var(--cyan)!important;text-shadow:var(--glow)!important;letter-spacing:2px!important;}
|
| 443 |
+
.gr-block,.gr-box,.gradio-group,.gr-form,div[data-testid="block"]{
|
| 444 |
+
background:var(--panel)!important;border:1px solid var(--border)!important;border-radius:8px!important;}
|
| 445 |
+
label span,.gr-label,.label-wrap span{font-family:'Share Tech Mono',monospace!important;
|
| 446 |
+
font-size:0.73rem!important;color:var(--cyan)!important;letter-spacing:1px!important;text-transform:uppercase!important;}
|
| 447 |
+
.gr-info{color:var(--muted)!important;font-size:0.68rem!important;}
|
| 448 |
+
input[type=range]{accent-color:var(--cyan)!important;}
|
| 449 |
+
input[type=number]{background:var(--card)!important;border:1px solid var(--border)!important;
|
| 450 |
+
color:var(--cyan)!important;font-family:'Share Tech Mono',monospace!important;border-radius:4px!important;}
|
| 451 |
+
textarea{background:var(--card)!important;border:1px solid var(--border)!important;
|
| 452 |
+
color:var(--green)!important;font-family:'Share Tech Mono',monospace!important;
|
| 453 |
+
font-size:0.8rem!important;line-height:1.65!important;border-radius:6px!important;}
|
| 454 |
+
button.primary{background:linear-gradient(135deg,#003c6e,#006aaa)!important;
|
| 455 |
+
border:1px solid var(--cyan)!important;color:var(--cyan)!important;
|
| 456 |
+
font-family:'Rajdhani',sans-serif!important;font-weight:700!important;
|
| 457 |
+
font-size:1.05rem!important;letter-spacing:3px!important;text-transform:uppercase!important;
|
| 458 |
+
border-radius:6px!important;box-shadow:var(--glow)!important;}
|
| 459 |
+
button.primary:hover{background:linear-gradient(135deg,#005090,#0088cc)!important;
|
| 460 |
+
box-shadow:0 0 22px rgba(0,212,255,0.7)!important;}
|
| 461 |
+
button.secondary{background:#1a0a14!important;border:1px solid var(--red)!important;
|
| 462 |
+
color:var(--red)!important;font-family:'Rajdhani',sans-serif!important;
|
| 463 |
+
font-weight:600!important;letter-spacing:2px!important;border-radius:6px!important;}
|
| 464 |
+
.tab-nav button{font-family:'Rajdhani',sans-serif!important;font-weight:600!important;
|
| 465 |
+
color:var(--muted)!important;background:var(--panel)!important;
|
| 466 |
+
border:1px solid var(--border)!important;letter-spacing:1px!important;}
|
| 467 |
+
.tab-nav button.selected{color:var(--cyan)!important;
|
| 468 |
+
border-bottom:2px solid var(--cyan)!important;box-shadow:var(--glow)!important;}
|
| 469 |
+
select,select *{background:var(--card)!important;border:1px solid var(--border)!important;
|
| 470 |
+
color:var(--cyan)!important;font-family:'Share Tech Mono',monospace!important;}
|
| 471 |
+
.gr-accordion summary{color:var(--cyan)!important;font-family:'Rajdhani',sans-serif!important;
|
| 472 |
+
font-weight:600!important;letter-spacing:1px!important;}
|
| 473 |
+
::-webkit-scrollbar{width:5px;}::-webkit-scrollbar-track{background:var(--bg);}
|
| 474 |
+
::-webkit-scrollbar-thumb{background:var(--border);border-radius:3px;}
|
| 475 |
+
"""
|
| 476 |
+
|
| 477 |
+
# ββ Interface ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 478 |
+
with gr.Blocks(css=CSS, title="π‘οΈ IDS",
|
| 479 |
+
theme=gr.themes.Base(primary_hue="cyan", secondary_hue="pink",
|
| 480 |
+
neutral_hue="slate")) as app:
|
| 481 |
+
|
| 482 |
+
gr.HTML("""
|
| 483 |
+
<div style="text-align:center;padding:18px 0 6px;">
|
| 484 |
+
<div style="font-size:2.8rem;line-height:1;">π‘οΈ</div>
|
| 485 |
+
<h1 style="font-family:'Rajdhani',sans-serif;font-size:2rem;color:#00d4ff;
|
| 486 |
+
letter-spacing:3px;margin:8px 0 4px;
|
| 487 |
+
text-shadow:0 0 16px rgba(0,212,255,0.6);">
|
| 488 |
+
INTRUSION DETECTION SYSTEM
|
| 489 |
+
</h1>
|
| 490 |
+
<p style="font-family:'Share Tech Mono',monospace;color:#5a8aaa;
|
| 491 |
+
font-size:0.74rem;letter-spacing:2px;margin:0;">
|
| 492 |
+
DECISION TREE Β· LOGISTIC REGRESSION Β· SVM Β· NSL-KDD Β· CHI-SQUARE FEATURES
|
| 493 |
+
</p>
|
| 494 |
+
<div style="height:2px;background:linear-gradient(90deg,transparent,#00d4ff,transparent);
|
| 495 |
+
margin:12px auto;width:55%;"></div>
|
| 496 |
+
</div>""")
|
| 497 |
+
|
| 498 |
+
with gr.Tabs():
|
| 499 |
+
|
| 500 |
+
# ββ Tab 1: Live Scanner βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 501 |
+
with gr.Tab("π LIVE SCANNER"):
|
| 502 |
+
model_selector = gr.Dropdown(
|
| 503 |
+
choices=list(MODELS.keys()), value=list(MODELS.keys())[0],
|
| 504 |
+
label="SELECT MODEL",
|
| 505 |
+
info="Choose which trained model performs the classification")
|
| 506 |
+
|
| 507 |
+
with gr.Row():
|
| 508 |
+
with gr.Column(scale=3):
|
| 509 |
+
gr.HTML('<p style="font-family:\'Share Tech Mono\',monospace;color:#5a8aaa;'
|
| 510 |
+
'font-size:0.7rem;letter-spacing:1px;margin-bottom:6px;">'
|
| 511 |
+
'βΈ CONFIGURE NETWORK TRAFFIC PARAMETERS</p>')
|
| 512 |
+
with gr.Accordion("β FLAG & BINARY FEATURES", open=True):
|
| 513 |
+
for _, inp in flag_inputs: inp.render()
|
| 514 |
+
with gr.Accordion("π RATE FEATURES", open=True):
|
| 515 |
+
for _, inp in rate_inputs: inp.render()
|
| 516 |
+
with gr.Accordion("π’ COUNT FEATURES", open=False):
|
| 517 |
+
for _, inp in count_inputs: inp.render()
|
| 518 |
+
if other_inputs:
|
| 519 |
+
with gr.Accordion("π§ OTHER FEATURES", open=False):
|
| 520 |
+
for _, inp in other_inputs: inp.render()
|
| 521 |
+
|
| 522 |
+
with gr.Column(scale=2):
|
| 523 |
+
gr.HTML('<p style="font-family:\'Share Tech Mono\',monospace;color:#5a8aaa;'
|
| 524 |
+
'font-size:0.7rem;letter-spacing:1px;margin-bottom:6px;">'
|
| 525 |
+
'βΈ ANALYSIS OUTPUT</p>')
|
| 526 |
+
result_out = gr.Textbox(label="π DETECTION RESULT", lines=18, interactive=False)
|
| 527 |
+
stats_out = gr.Textbox(label="π SESSION STATS", lines=9, interactive=False)
|
| 528 |
+
history_out = gr.Textbox(label="π SCAN HISTORY", lines=10, interactive=False)
|
| 529 |
+
with gr.Row():
|
| 530 |
+
scan_btn = gr.Button("β‘ SCAN TRAFFIC", variant="primary")
|
| 531 |
+
reset_btn = gr.Button("π RESET SESSION", variant="secondary")
|
| 532 |
+
|
| 533 |
+
gr.HTML('<div style="height:1px;background:#1e3a5a;margin:18px 0 10px;"></div>'
|
| 534 |
+
'<p style="font-family:\'Share Tech Mono\',monospace;color:#5a8aaa;'
|
| 535 |
+
'font-size:0.7rem;letter-spacing:1px;margin-bottom:6px;">βΈ VISUAL ANALYSIS</p>')
|
| 536 |
+
with gr.Row():
|
| 537 |
+
radar_plot = gr.Plot(label="Feature Profile (Radar)")
|
| 538 |
+
conf_plot = gr.Plot(label="Model Confidence Comparison")
|
| 539 |
+
session_plot = gr.Plot(label="Session Dashboard")
|
| 540 |
+
|
| 541 |
+
scan_btn.click(fn=predict,
|
| 542 |
+
inputs=[model_selector] + all_inputs,
|
| 543 |
+
outputs=[result_out, stats_out, history_out,
|
| 544 |
+
radar_plot, conf_plot, session_plot])
|
| 545 |
+
reset_btn.click(fn=reset_session, inputs=[],
|
| 546 |
+
outputs=[result_out, stats_out, history_out,
|
| 547 |
+
radar_plot, conf_plot, session_plot])
|
| 548 |
+
|
| 549 |
+
# ββ Tab 2: Model Comparison ββββββββββββββββββββββββββββββββββββββββββββ
|
| 550 |
+
with gr.Tab("π MODEL COMPARISON"):
|
| 551 |
+
gr.HTML('<div style="font-family:\'Share Tech Mono\',monospace;color:#5a8aaa;'
|
| 552 |
+
'font-size:0.72rem;letter-spacing:1px;padding:8px 0 14px;">'
|
| 553 |
+
'βΈ TRAINING PERFORMANCE METRICS ACROSS ALL THREE MODELS</div>')
|
| 554 |
+
metrics_plot = gr.Plot(label="Model Metrics")
|
| 555 |
+
gr.Button("π LOAD METRICS CHART", variant="primary").click(
|
| 556 |
+
fn=metrics_chart, inputs=[], outputs=[metrics_plot])
|
| 557 |
+
|
| 558 |
+
if TRAIN_METRICS:
|
| 559 |
+
rows = [{"Model": m,
|
| 560 |
+
"Accuracy": f"{v['accuracy']:.4f}",
|
| 561 |
+
"Precision": f"{v['precision']:.4f}",
|
| 562 |
+
"Recall": f"{v['recall']:.4f}",
|
| 563 |
+
"F1": f"{v['f1']:.4f}",
|
| 564 |
+
"ROC-AUC": f"{v['roc_auc']:.4f}"}
|
| 565 |
+
for m, v in TRAIN_METRICS.items()]
|
| 566 |
+
gr.Dataframe(pd.DataFrame(rows), label="Metrics Table", interactive=False)
|
| 567 |
+
|
| 568 |
+
# ββ Tab 3: Preset Scenarios ββββββββββββββββββββββββββββββββββββββββββββ
|
| 569 |
+
with gr.Tab("π― PRESET SCENARIOS"):
|
| 570 |
+
gr.HTML('<div style="font-family:\'Share Tech Mono\',monospace;color:#5a8aaa;'
|
| 571 |
+
'font-size:0.72rem;letter-spacing:1px;padding:8px 0 14px;">'
|
| 572 |
+
'βΈ LOAD A KNOWN SCENARIO β SEE EXPECTED VALUES, THEN TEST IN SCANNER</div>')
|
| 573 |
+
scenario_out = gr.Textbox(label="Scenario Description", lines=18, interactive=False)
|
| 574 |
+
|
| 575 |
+
def make_scenario(name, expected, rules):
|
| 576 |
+
vals = []
|
| 577 |
+
for f in features:
|
| 578 |
+
matched = any(k in f and (vals.append(v) or True)
|
| 579 |
+
for k, v in rules.items())
|
| 580 |
+
if not matched:
|
| 581 |
+
vals.append(
|
| 582 |
+
1 if f in ["logged_in","flag_sf","service_http"] else 0)
|
| 583 |
+
lines = [f"SCENARIO : {name}", f"EXPECTED : {expected}", "β"*44]
|
| 584 |
+
lines += [f" {f:<40} = {v}" for f, v in zip(features, vals)]
|
| 585 |
+
lines += ["β"*44, "β€ Set values in LIVE SCANNER tab and click SCAN."]
|
| 586 |
+
return "\n".join(lines)
|
| 587 |
+
|
| 588 |
+
with gr.Row():
|
| 589 |
+
gr.Button("π₯ DoS Attack").click(
|
| 590 |
+
fn=lambda: make_scenario("Denial-of-Service (DoS)",
|
| 591 |
+
"π¨ ATTACK β CRITICAL | Type: DoS",
|
| 592 |
+
{"serror":0.95,"count":200,"srv_count":200,
|
| 593 |
+
"flag_s0":1,"flag_sf":0,"same_srv":0.95,
|
| 594 |
+
"diff_srv":0.05,"logged_in":0}),
|
| 595 |
+
outputs=scenario_out)
|
| 596 |
+
gr.Button("β
Normal Session").click(
|
| 597 |
+
fn=lambda: make_scenario("Normal HTTP Web Session",
|
| 598 |
+
"β
NORMAL TRAFFIC",
|
| 599 |
+
{"serror":0.0,"count":5,"srv_count":5,"flag_sf":1,
|
| 600 |
+
"flag_s0":0,"logged_in":1,"same_srv":0.95,
|
| 601 |
+
"diff_srv":0.0,"service_http":1,
|
| 602 |
+
"src_bytes":2000,"dst_bytes":8000}),
|
| 603 |
+
outputs=scenario_out)
|
| 604 |
+
gr.Button("π Port Scan").click(
|
| 605 |
+
fn=lambda: make_scenario("Network Port Scan (Probe)",
|
| 606 |
+
"π¨ ATTACK β MEDIUM/HIGH | Type: Probe",
|
| 607 |
+
{"diff_srv":0.85,"same_srv":0.10,"count":120,
|
| 608 |
+
"serror":0.1,"logged_in":0,"flag_sf":0}),
|
| 609 |
+
outputs=scenario_out)
|
| 610 |
+
gr.Button("π R2L / Brute-Force").click(
|
| 611 |
+
fn=lambda: make_scenario("Remote-to-Local (R2L) Attempt",
|
| 612 |
+
"π¨ ATTACK β HIGH | Type: R2L",
|
| 613 |
+
{"logged_in":1,"src_bytes":500,"dst_bytes":20,
|
| 614 |
+
"count":3,"serror":0.0,"flag_sf":1,"diff_srv":0.1}),
|
| 615 |
+
outputs=scenario_out)
|
| 616 |
+
|
| 617 |
+
# ββ Tab 4: Feature Reference βββββββββββββββββββββββββββββββββββββββββββ
|
| 618 |
+
with gr.Tab("π FEATURE REFERENCE"):
|
| 619 |
+
rows = [{"Feature": f,
|
| 620 |
+
"Type": ("Binary 0/1" if f.startswith("flag_") or f in ["logged_in","service_http"]
|
| 621 |
+
else "Rate [0β1]" if "rate" in f
|
| 622 |
+
else "Count [0β255]" if "count" in f
|
| 623 |
+
else "Numeric"),
|
| 624 |
+
"Description": FEATURE_INFO.get(f, "Network traffic feature")}
|
| 625 |
+
for f in features]
|
| 626 |
+
gr.Dataframe(pd.DataFrame(rows), label="Selected Features",
|
| 627 |
+
interactive=False, wrap=True)
|
| 628 |
+
gr.HTML('<div style="font-family:\'Share Tech Mono\',monospace;color:#5a8aaa;'
|
| 629 |
+
'font-size:0.7rem;letter-spacing:1px;margin-top:14px;padding:10px 14px;'
|
| 630 |
+
'border:1px solid #1e3a5a;border-radius:6px;">'
|
| 631 |
+
'Pipeline: Pearson Correlation (top-25) β Chi-Square SelectKBest (final 12).<br>'
|
| 632 |
+
'Trained on NSL-KDD 20,000 rows Β· 80/20 split Β· class_weight=balanced</div>')
|
| 633 |
+
|
| 634 |
+
# ββ Tab 5: About βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 635 |
+
with gr.Tab("βΉοΈ ABOUT"):
|
| 636 |
+
gr.HTML("""
|
| 637 |
+
<div style="font-family:'Rajdhani',sans-serif;max-width:720px;
|
| 638 |
+
margin:0 auto;padding:20px 0;line-height:1.8;">
|
| 639 |
+
<h2 style="color:#00d4ff;letter-spacing:2px;border-bottom:1px solid #1e3a5a;
|
| 640 |
+
padding-bottom:8px;">ML-Based Intrusion Detection System</h2>
|
| 641 |
+
<p style="color:#c8e6ff;font-size:0.95rem;">
|
| 642 |
+
Classifies network connections as <b style="color:#39ff14;">Normal</b> or
|
| 643 |
+
<b style="color:#ff3c6e;">Attack</b> using three ML models. Attack type is
|
| 644 |
+
inferred via rule-based heuristics on top of binary classification.
|
| 645 |
+
</p>
|
| 646 |
+
<h3 style="color:#00d4ff;margin-top:18px;">Three Models</h3>
|
| 647 |
+
<ul style="color:#c8e6ff;font-size:0.92rem;">
|
| 648 |
+
<li><b style="color:#00d4ff;">π³ Decision Tree</b> β Interpretable tree splits.
|
| 649 |
+
max_depth=10, balanced weights.</li>
|
| 650 |
+
<li><b style="color:#f5a623;">π Logistic Regression</b> β Linear probabilistic.
|
| 651 |
+
Scaled input, lbfgs, max_iter=1000.</li>
|
| 652 |
+
<li><b style="color:#ff3c6e;">β‘ SVM (RBF)</b> β Non-linear kernel SVM.
|
| 653 |
+
C=1.0, gamma=scale, probability=True.</li>
|
| 654 |
+
</ul>
|
| 655 |
+
<h3 style="color:#00d4ff;margin-top:18px;">Attack Categories</h3>
|
| 656 |
+
<ul style="color:#c8e6ff;font-size:0.92rem;">
|
| 657 |
+
<li><b style="color:#ff3c6e;">DoS</b> β High error rate + large count (neptune, smurf)</li>
|
| 658 |
+
<li><b style="color:#f5a623;">Probe</b> β Many services scanned (portsweep, nmap)</li>
|
| 659 |
+
<li><b style="color:#c084fc;">R2L</b> β Asymmetric bytes after login (ftp_write)</li>
|
| 660 |
+
<li><b style="color:#00d4ff;">U2R</b> β Low-volume logged-in session (buffer_overflow)</li>
|
| 661 |
+
</ul>
|
| 662 |
+
<div style="margin-top:20px;padding:10px 14px;background:#0d1526;
|
| 663 |
+
border:1px solid #1e3a5a;border-radius:6px;
|
| 664 |
+
font-family:'Share Tech Mono',monospace;font-size:0.72rem;
|
| 665 |
+
color:#5a8aaa;letter-spacing:1px;">
|
| 666 |
+
Dataset: Mireu-Lab/NSL-KDD (Hugging Face) |
|
| 667 |
+
Models: sklearn | UI: Gradio
|
| 668 |
+
</div>
|
| 669 |
+
</div>""")
|
| 670 |
+
|
| 671 |
+
app.launch()
|
decision_tree_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:19a4ad919aa8e47074dcf61826427c6fd43f7e13a81466ec261b13da082ea2f2
|
| 3 |
+
size 25337
|
features.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:154049b42d99136d6ef8a8965b74813f78b42e37e57b38726a33fcd02ae130eb
|
| 3 |
+
size 216
|
logistic_regression_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bcf74cdfc6563b629fb1d4e47a7f15274d11d394f301144efe69eeb9d3fdcccd
|
| 3 |
+
size 959
|
metrics.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:00eb75341463a7e4c868b2fbecc6c6987c0b2c39837046a7076b61a738be90da
|
| 3 |
+
size 455
|
metrics_summary.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"Decision Tree": {
|
| 3 |
+
"accuracy": 0.9688,
|
| 4 |
+
"precision": 0.9777,
|
| 5 |
+
"recall": 0.9554,
|
| 6 |
+
"f1": 0.9664,
|
| 7 |
+
"roc_auc": 0.9904
|
| 8 |
+
},
|
| 9 |
+
"Logistic Regression": {
|
| 10 |
+
"accuracy": 0.9212,
|
| 11 |
+
"precision": 0.9443,
|
| 12 |
+
"recall": 0.8847,
|
| 13 |
+
"f1": 0.9135,
|
| 14 |
+
"roc_auc": 0.9727
|
| 15 |
+
},
|
| 16 |
+
"SVM": {
|
| 17 |
+
"accuracy": 0.9537,
|
| 18 |
+
"precision": 0.9831,
|
| 19 |
+
"recall": 0.9173,
|
| 20 |
+
"f1": 0.9491,
|
| 21 |
+
"roc_auc": 0.9866
|
| 22 |
+
}
|
| 23 |
+
}
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9d49913a91a52a8ecc4fc2faecc6ac7f09d2bf3238e3528b2e620ae0846d59e2
|
| 3 |
+
size 6654233
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
scipy
|
| 6 |
+
matplotlib
|
| 7 |
+
seaborn
|
| 8 |
+
gradio
|
| 9 |
+
joblib
|
| 10 |
+
python-docx
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e2f42e4f14f41edee9a8242b2a6fc51dd029517cfc939a1043ceeb7ca78428fe
|
| 3 |
+
size 1319
|
svm_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:285521c98b9baa7d07e164fd35cfb38cd2725afa03adce01b24839008e9887b2
|
| 3 |
+
size 228011
|