Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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
|
| 16 |
def deteksi_serangan(dst_port, packet_size, duration, protocol_name):
|
| 17 |
if model is None:
|
| 18 |
-
return "⚠️ ERROR: File
|
| 19 |
|
| 20 |
-
# Konversi
|
| 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)
|
| 24 |
|
| 25 |
-
# Susun
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
|
|
|
| 34 |
if hasil == 0:
|
| 35 |
-
|
| 36 |
else:
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
interface = gr.Interface(
|
| 41 |
-
fn=deteksi_serangan,
|
| 42 |
inputs=[
|
| 43 |
-
gr.Number(label="Destination Port (
|
| 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 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|