siedysioes commited on
Commit
403cc6c
·
verified ·
1 Parent(s): b725363

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -19
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import gradio as gr
2
  import joblib
3
  import numpy as np
 
4
 
5
- # 1. LOAD MODEL (OTAK AI)
6
  def load_model():
7
  try:
8
  model = joblib.load('otak_ai_security.pkl')
@@ -12,44 +13,74 @@ def load_model():
12
 
13
  model = load_model()
14
 
15
- # 2. FUNGSI PREDIKSI (INTI LOGIKA)
16
  def deteksi_serangan(dst_port, packet_size, duration, protocol_name):
17
  if model is None:
18
- return "⚠️ ERROR: File otak_ai_security.pkl belum diupload!"
19
 
20
- # Konversi nama protokol ke angka (sesuai training modul 2)
21
- # Mapping: TCP=6, UDP=17, ICMP=1
22
  protocol_map = {"TCP": 6, "UDP": 17, "ICMP": 1}
23
- proto_code = protocol_map.get(protocol_name, 6) # Default TCP
24
 
25
- # Susun data input
26
  data_input = np.array([[dst_port, packet_size, duration, proto_code]])
27
 
28
  # Prediksi
29
  hasil = model.predict(data_input)[0]
30
- probabilitas = model.predict_proba(data_input)
31
- confidence = probabilitas[0][hasil] * 100
 
 
 
32
 
33
- # Susun Kalimat Jawaban
 
34
  if hasil == 0:
35
- return f"✅ STATUS: AMAN (BENIGN)\n\nAI yakin {confidence:.1f}% bahwa ini traffic normal."
36
  else:
37
- return f"🚨 STATUS: BAHAYA (MALICIOUS ATTACK!)\n\nAI yakin {confidence:.1f}% ini adalah SERANGAN."
38
 
39
- # 3. MEMBUAT TAMPILAN WEB (INTERFACE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  interface = gr.Interface(
41
- fn=deteksi_serangan, # Fungsi yang dipanggil saat tombol ditekan
42
  inputs=[
43
- gr.Number(label="Destination Port (Contoh: 80, 443, 22)", value=80),
44
  gr.Number(label="Packet Size (Bytes)", value=500),
45
  gr.Number(label="Duration (ms)", value=100),
46
  gr.Dropdown(["TCP", "UDP", "ICMP"], label="Protocol Type", value="TCP"),
47
  ],
48
- outputs="text",
49
- title="🛡️ AI-Based Intrusion Detection System",
50
- description="Demo Keamanan Siber oleh Edy Qineos Academy. Masukkan parameter paket data untuk didiagnosa oleh AI.",
 
 
 
 
51
  theme="default"
52
  )
53
 
54
- # Jalankan Aplikasi
55
  interface.launch()
 
1
  import gradio as gr
2
  import joblib
3
  import numpy as np
4
+ import matplotlib.pyplot as plt
5
 
6
+ # 1. LOAD MODEL
7
  def load_model():
8
  try:
9
  model = joblib.load('otak_ai_security.pkl')
 
13
 
14
  model = load_model()
15
 
16
+ # 2. FUNGSI PREDIKSI + VISUALISASI
17
  def deteksi_serangan(dst_port, packet_size, duration, protocol_name):
18
  if model is None:
19
+ return "⚠️ ERROR: File .pkl belum diupload!", None
20
 
21
+ # Konversi Protocol
 
22
  protocol_map = {"TCP": 6, "UDP": 17, "ICMP": 1}
23
+ proto_code = protocol_map.get(protocol_name, 6)
24
 
25
+ # Susun Data
26
  data_input = np.array([[dst_port, packet_size, duration, proto_code]])
27
 
28
  # Prediksi
29
  hasil = model.predict(data_input)[0]
30
+ probabilitas = model.predict_proba(data_input)[0] # Contoh: [0.1, 0.9]
31
+
32
+ # Ambil nilai probabilitas
33
+ prob_aman = probabilitas[0]
34
+ prob_serangan = probabilitas[1]
35
 
36
+ # --- BAGIAN 1: TEKS OUTPUT ---
37
+ confidence = probabilitas[hasil] * 100
38
  if hasil == 0:
39
+ pesan = f"✅ STATUS: AMAN (BENIGN)\n\nAI yakin {confidence:.1f}% trafik ini normal."
40
  else:
41
+ pesan = f"🚨 STATUS: BAHAYA (MALICIOUS!)\n\nAI yakin {confidence:.1f}% ini SERANGAN."
42
 
43
+ # --- BAGIAN 2: MEMBUAT GRAFIK (VISUALISASI) ---
44
+ # Kita buat Donut Chart
45
+ fig, ax = plt.subplots(figsize=(6, 4))
46
+
47
+ labels = ['Aman', 'Serangan']
48
+ values = [prob_aman, prob_serangan]
49
+ colors = ['#4CAF50', '#F44336'] # Hijau & Merah
50
+ explode = (0.1, 0) if hasil == 0 else (0, 0.1) # Efek "keluar" sedikit pada yang terpilih
51
+
52
+ # Gambar Pie Chart
53
+ wedges, texts, autotexts = ax.pie(values, labels=labels, autopct='%1.1f%%',
54
+ startangle=90, colors=colors, explode=explode,
55
+ textprops=dict(color="black"))
56
+
57
+ # Bikin jadi Donut (Tengah bolong)
58
+ centre_circle = plt.Circle((0,0),0.70,fc='white')
59
+ fig.gca().add_artist(centre_circle)
60
+
61
+ ax.axis('equal')
62
+ plt.title("Tingkat Keyakinan AI", fontsize=14, fontweight='bold')
63
+ plt.close() # Tutup plot agar memori hemat
64
+
65
+ return pesan, fig
66
+
67
+ # 3. INTERFACE GRADIO (DENGAN PLOT)
68
  interface = gr.Interface(
69
+ fn=deteksi_serangan,
70
  inputs=[
71
+ gr.Number(label="Destination Port (Cth: 80, 443, 22)", value=80),
72
  gr.Number(label="Packet Size (Bytes)", value=500),
73
  gr.Number(label="Duration (ms)", value=100),
74
  gr.Dropdown(["TCP", "UDP", "ICMP"], label="Protocol Type", value="TCP"),
75
  ],
76
+ # OUTPUTNYA SEKARANG ADA 2: TEKS DAN GAMBAR (PLOT)
77
+ outputs=[
78
+ gr.Textbox(label="Hasil Analisa"),
79
+ gr.Plot(label="Visualisasi Confidence")
80
+ ],
81
+ title="🛡️ AI Security Dashboard (Visual)",
82
+ description="Demo IDS dengan Visualisasi Data. Masukkan parameter untuk melihat grafik deteksi.",
83
  theme="default"
84
  )
85
 
 
86
  interface.launch()