Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if result == 1:
|
| 11 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
else:
|
| 13 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
+
# =========================
|
| 5 |
+
# Prediction Function
|
| 6 |
+
# =========================
|
| 7 |
+
def predict(duration, src_bytes, dst_bytes, failed_logins,
|
| 8 |
+
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 9 |
+
host_count, login_attempts):
|
| 10 |
|
| 11 |
+
# Convert input into array
|
| 12 |
+
data = np.array([
|
| 13 |
+
duration, src_bytes, dst_bytes, failed_logins,
|
| 14 |
+
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 15 |
+
host_count, login_attempts
|
| 16 |
+
])
|
| 17 |
|
| 18 |
+
# Simple logic (for demo)
|
| 19 |
+
score = np.sum(data)
|
| 20 |
+
result = 1 if score > 50 else 0
|
| 21 |
+
|
| 22 |
+
# Interpretation
|
| 23 |
if result == 1:
|
| 24 |
+
return (
|
| 25 |
+
"β οΈ **Attack Detected!**\n\n"
|
| 26 |
+
f"Risk Score: {score:.2f}\n\n"
|
| 27 |
+
"The network behavior appears suspicious.\n"
|
| 28 |
+
"Possible reasons:\n"
|
| 29 |
+
"- High traffic volume\n"
|
| 30 |
+
"- Multiple login attempts\n"
|
| 31 |
+
"- Unusual connection patterns\n\n"
|
| 32 |
+
"π Recommended Action: Monitor or block this activity."
|
| 33 |
+
)
|
| 34 |
else:
|
| 35 |
+
return (
|
| 36 |
+
"β
**Normal Traffic**\n\n"
|
| 37 |
+
f"Risk Score: {score:.2f}\n\n"
|
| 38 |
+
"The network activity appears safe and normal.\n"
|
| 39 |
+
"No immediate threat detected."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# =========================
|
| 44 |
+
# UI Design
|
| 45 |
+
# =========================
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
|
| 48 |
+
gr.Markdown("# π Intrusion Detection System (IDS)")
|
| 49 |
+
gr.Markdown(
|
| 50 |
+
"This system analyzes network traffic features and predicts whether the activity is **Normal** or a potential **Cyber Attack**.\n\n"
|
| 51 |
+
"π Enter values below to simulate network behavior."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
duration = gr.Number(label="Connection Duration")
|
| 56 |
+
src_bytes = gr.Number(label="Source Bytes (Data Sent)")
|
| 57 |
+
dst_bytes = gr.Number(label="Destination Bytes (Data Received)")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
failed_logins = gr.Number(label="Failed Login Attempts")
|
| 61 |
+
login_attempts = gr.Number(label="Total Login Attempts")
|
| 62 |
+
count = gr.Number(label="Number of Connections")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
error_rate = gr.Number(label="Error Rate")
|
| 66 |
+
same_srv_rate = gr.Number(label="Same Service Rate")
|
| 67 |
+
diff_srv_rate = gr.Number(label="Different Service Rate")
|
| 68 |
+
|
| 69 |
+
host_count = gr.Number(label="Host Count")
|
| 70 |
+
|
| 71 |
+
predict_btn = gr.Button("π Analyze Traffic")
|
| 72 |
|
| 73 |
+
output = gr.Markdown()
|
| 74 |
|
| 75 |
+
predict_btn.click(
|
| 76 |
+
predict,
|
| 77 |
+
inputs=[
|
| 78 |
+
duration, src_bytes, dst_bytes, failed_logins,
|
| 79 |
+
count, error_rate, same_srv_rate, diff_srv_rate,
|
| 80 |
+
host_count, login_attempts
|
| 81 |
+
],
|
| 82 |
+
outputs=output
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
demo.launch()
|