dev1461 commited on
Commit
caf7068
Β·
verified Β·
1 Parent(s): 5e372db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -15
app.py CHANGED
@@ -1,25 +1,85 @@
1
  import gradio as gr
2
  import numpy as np
3
 
4
- def predict(*inputs):
5
- data = np.array(inputs)
 
 
 
 
6
 
7
- # Simple logic (stable)
8
- result = 1 if np.sum(data) > 5 else 0
 
 
 
 
9
 
 
 
 
 
 
10
  if result == 1:
11
- return "⚠️ Attack Detected"
 
 
 
 
 
 
 
 
 
12
  else:
13
- return "βœ… Normal Traffic"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- inputs = [gr.Number(label=f"Feature {i+1}") for i in range(20)]
16
 
17
- interface = gr.Interface(
18
- fn=predict,
19
- inputs=inputs,
20
- outputs="text",
21
- title="Intrusion Detection System",
22
- description="Enter feature values to detect attack or normal traffic"
23
- )
 
 
24
 
25
- interface.launch()
 
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()