Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,11 @@ model_path = hf_hub_download(
|
|
| 9 |
filename="ids_model.pkl"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# Load model
|
| 13 |
with open(model_path, "rb") as f:
|
| 14 |
model = pickle.load(f)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
columns = [
|
| 18 |
"duration","protocol_type","service","flag","src_bytes","dst_bytes","land",
|
| 19 |
"wrong_fragment","urgent","hot","num_failed_logins","logged_in",
|
|
@@ -24,29 +24,30 @@ columns = [
|
|
| 24 |
"diff_srv_rate","srv_diff_host_rate","dst_host_count","dst_host_srv_count",
|
| 25 |
"dst_host_same_srv_rate","dst_host_diff_srv_rate",
|
| 26 |
"dst_host_same_src_port_rate","dst_host_srv_diff_host_rate",
|
| 27 |
-
"dst_host_serror_rate","dst_host_srv_serror_rate",
|
| 28 |
-
"dst_host_srv_rerror_rate"
|
| 29 |
]
|
| 30 |
|
| 31 |
def predict_intrusion(*inputs):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
pred = model.predict(data)[0]
|
| 36 |
if pred > 0.5:
|
| 37 |
return "🔴 ATTACK DETECTED"
|
| 38 |
else:
|
| 39 |
return "🟢 NORMAL TRAFFIC"
|
| 40 |
|
| 41 |
-
#
|
| 42 |
inputs_ui = [gr.Number(label=col) for col in columns]
|
| 43 |
|
| 44 |
app = gr.Interface(
|
| 45 |
fn=predict_intrusion,
|
| 46 |
inputs=inputs_ui,
|
| 47 |
outputs="text",
|
| 48 |
-
title="CyberSecurity
|
| 49 |
-
description="
|
| 50 |
)
|
| 51 |
|
| 52 |
app.launch()
|
|
|
|
| 9 |
filename="ids_model.pkl"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# Load the model
|
| 13 |
with open(model_path, "rb") as f:
|
| 14 |
model = pickle.load(f)
|
| 15 |
|
| 16 |
+
# Feature names (41 features of NSL-KDD)
|
| 17 |
columns = [
|
| 18 |
"duration","protocol_type","service","flag","src_bytes","dst_bytes","land",
|
| 19 |
"wrong_fragment","urgent","hot","num_failed_logins","logged_in",
|
|
|
|
| 24 |
"diff_srv_rate","srv_diff_host_rate","dst_host_count","dst_host_srv_count",
|
| 25 |
"dst_host_same_srv_rate","dst_host_diff_srv_rate",
|
| 26 |
"dst_host_same_src_port_rate","dst_host_srv_diff_host_rate",
|
| 27 |
+
"dst_host_serror_rate","dst_host_srv_serror_rate",
|
| 28 |
+
"dst_host_rerror_rate","dst_host_srv_rerror_rate"
|
| 29 |
]
|
| 30 |
|
| 31 |
def predict_intrusion(*inputs):
|
| 32 |
+
df = pd.DataFrame([inputs], columns=columns)
|
| 33 |
+
df = df.apply(pd.to_numeric, errors='coerce').fillna(0)
|
| 34 |
+
|
| 35 |
+
pred = model.predict(df)[0] # LightGBM outputs probability
|
| 36 |
|
|
|
|
| 37 |
if pred > 0.5:
|
| 38 |
return "🔴 ATTACK DETECTED"
|
| 39 |
else:
|
| 40 |
return "🟢 NORMAL TRAFFIC"
|
| 41 |
|
| 42 |
+
# Dynamic UI (41 numeric inputs)
|
| 43 |
inputs_ui = [gr.Number(label=col) for col in columns]
|
| 44 |
|
| 45 |
app = gr.Interface(
|
| 46 |
fn=predict_intrusion,
|
| 47 |
inputs=inputs_ui,
|
| 48 |
outputs="text",
|
| 49 |
+
title="CyberSecurity Intrusion Detection System",
|
| 50 |
+
description="Upload network feature values (NSL-KDD dataset) to detect malicious intrusions using LightGBM."
|
| 51 |
)
|
| 52 |
|
| 53 |
app.launch()
|