Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# --- 1. Load Model ---
|
| 5 |
model_path = "elftsdmr/malware-url-detect"
|
| 6 |
-
|
| 7 |
try:
|
| 8 |
-
# Load model
|
| 9 |
classifier = pipeline("text-classification", model=model_path, top_k=None)
|
| 10 |
except Exception as e:
|
| 11 |
classifier = None
|
| 12 |
print(f"Error loading model: {e}")
|
| 13 |
|
| 14 |
-
# --- 2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def analyze_url(target_url):
|
| 16 |
if classifier is None:
|
| 17 |
return "Model gagal dimuat.", {}
|
|
@@ -19,94 +33,92 @@ def analyze_url(target_url):
|
|
| 19 |
return "Mohon masukkan URL.", {}
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
#
|
| 23 |
-
results = classifier(target_url)[0]
|
| 24 |
|
| 25 |
-
# Format data
|
| 26 |
chart_data = {}
|
| 27 |
-
|
| 28 |
top_score = 0
|
| 29 |
|
| 30 |
for res in results:
|
| 31 |
label = res['label']
|
| 32 |
score = res['score']
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
else:
|
| 38 |
-
readable_label = "
|
| 39 |
|
| 40 |
chart_data[readable_label] = score
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
return text_output, chart_data
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
return f"Error: {e}", {}
|
| 68 |
|
| 69 |
-
# ---
|
| 70 |
-
# Kita hapus 'theme=...' tapi tetap pertahankan layout yang diminta
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
gr.Markdown(
|
| 73 |
"""
|
| 74 |
-
# 🛡️ AI Network Security
|
| 75 |
-
###
|
| 76 |
"""
|
| 77 |
)
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
-
# Kolom Input
|
| 81 |
with gr.Column(scale=1):
|
| 82 |
input_text = gr.Textbox(
|
| 83 |
-
label="
|
| 84 |
-
placeholder="
|
| 85 |
lines=3
|
| 86 |
)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
gr.Markdown("ℹ️ **Info:** Grafik di kanan menunjukkan tingkat keyakinan AI (Confidence Level).")
|
| 91 |
|
| 92 |
-
# Kolom Output
|
| 93 |
with gr.Column(scale=1):
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# Output Grafik
|
| 97 |
-
output_chart = gr.Label(label="Probabilitas AI", num_top_classes=2)
|
| 98 |
|
| 99 |
-
# Hubungkan tombol
|
| 100 |
submit_btn.click(
|
| 101 |
fn=analyze_url,
|
| 102 |
inputs=input_text,
|
| 103 |
outputs=[output_text, output_chart]
|
| 104 |
)
|
| 105 |
|
| 106 |
-
# Footer
|
| 107 |
gr.Markdown("---")
|
| 108 |
gr.Markdown("*Powered by Hugging Face Spaces*")
|
| 109 |
|
| 110 |
-
# --- 4. Launch ---
|
| 111 |
if __name__ == "__main__":
|
| 112 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# --- 1. Load Model AI ---
|
| 5 |
model_path = "elftsdmr/malware-url-detect"
|
|
|
|
| 6 |
try:
|
|
|
|
| 7 |
classifier = pipeline("text-classification", model=model_path, top_k=None)
|
| 8 |
except Exception as e:
|
| 9 |
classifier = None
|
| 10 |
print(f"Error loading model: {e}")
|
| 11 |
|
| 12 |
+
# --- 2. Definisi Rule-Based (Daftar Hitam Kata Kunci) ---
|
| 13 |
+
# Ini adalah jaring pengaman jika AI gagal mendeteksi
|
| 14 |
+
SUSPICIOUS_KEYWORDS = [
|
| 15 |
+
"bitcoin", "mining", "crypto", "wallet", "bonus", "claim",
|
| 16 |
+
"free-gift", "login-verify", "secure-account", "update-payment",
|
| 17 |
+
"paypal", "bank", "prize", "giveaway"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
def check_keywords(url):
|
| 21 |
+
"""Cek apakah URL mengandung kata kunci berbahaya"""
|
| 22 |
+
found_keywords = []
|
| 23 |
+
for keyword in SUSPICIOUS_KEYWORDS:
|
| 24 |
+
if keyword in url.lower():
|
| 25 |
+
found_keywords.append(keyword)
|
| 26 |
+
return found_keywords
|
| 27 |
+
|
| 28 |
+
# --- 3. Fungsi Logika Utama (Hybrid) ---
|
| 29 |
def analyze_url(target_url):
|
| 30 |
if classifier is None:
|
| 31 |
return "Model gagal dimuat.", {}
|
|
|
|
| 33 |
return "Mohon masukkan URL.", {}
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
# A. Analisis AI
|
| 37 |
+
results = classifier(target_url)[0]
|
| 38 |
|
| 39 |
+
# Format data untuk grafik
|
| 40 |
chart_data = {}
|
| 41 |
+
ai_verdict = "AMAN"
|
| 42 |
top_score = 0
|
| 43 |
|
| 44 |
for res in results:
|
| 45 |
label = res['label']
|
| 46 |
score = res['score']
|
| 47 |
|
| 48 |
+
if label == "LABEL_1" or "MAL" in label.upper():
|
| 49 |
+
readable_label = "AI: BERBAHAYA"
|
| 50 |
+
if score > 0.5: ai_verdict = "BAHAYA"
|
| 51 |
else:
|
| 52 |
+
readable_label = "AI: AMAN"
|
| 53 |
|
| 54 |
chart_data[readable_label] = score
|
| 55 |
+
if score > top_score: top_score = score
|
| 56 |
+
|
| 57 |
+
# B. Analisis Keyword (Rule-Based)
|
| 58 |
+
found_keywords = check_keywords(target_url)
|
| 59 |
|
| 60 |
+
# C. KEPUTUSAN AKHIR (Logika Gabungan)
|
| 61 |
+
|
| 62 |
+
# Skenario 1: AI bilang Bahaya
|
| 63 |
+
if ai_verdict == "BAHAYA":
|
| 64 |
+
final_status = "⚠️ BERBAHAYA (Terdeteksi AI)"
|
| 65 |
+
detail_msg = f"AI sangat yakin ({top_score:.1%}) ini berbahaya."
|
| 66 |
+
|
| 67 |
+
# Skenario 2: AI bilang Aman, TAPI ada Keyword mencurigakan (Kasus Bitcoin tadi)
|
| 68 |
+
elif ai_verdict == "AMAN" and found_keywords:
|
| 69 |
+
final_status = "⚠️ MENCURIGAKAN (Terdeteksi Keyword)"
|
| 70 |
+
detail_msg = (f"Meskipun AI memberi skor aman, sistem mendeteksi kata kunci sensitif: {found_keywords}.\n"
|
| 71 |
+
f"Sistem melakukan 'Override' demi keamanan.")
|
| 72 |
+
# Kita paksa grafik tampil seimbang agar user waspada
|
| 73 |
+
chart_data["Rule-Based Detection"] = 0.99
|
| 74 |
+
|
| 75 |
+
# Skenario 3: AI bilang Aman dan tidak ada Keyword
|
| 76 |
else:
|
| 77 |
+
final_status = "✅ AMAN"
|
| 78 |
+
detail_msg = f"Tidak ditemukan indikasi berbahaya oleh AI maupun sistem keyword."
|
| 79 |
+
|
| 80 |
+
# Format Teks Output
|
| 81 |
+
text_output = (f"STATUS: {final_status}\n"
|
| 82 |
+
f"--------------------------------\n"
|
| 83 |
+
f"{detail_msg}\n\n"
|
| 84 |
+
f"Confidence AI: {top_score:.1%}")
|
| 85 |
|
| 86 |
return text_output, chart_data
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
return f"Error: {e}", {}
|
| 90 |
|
| 91 |
+
# --- 4. UI Gradio ---
|
|
|
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
gr.Markdown(
|
| 94 |
"""
|
| 95 |
+
# 🛡️ Hybrid AI Network Security
|
| 96 |
+
### Kombinasi Deep Learning + Rule Based System
|
| 97 |
"""
|
| 98 |
)
|
| 99 |
|
| 100 |
with gr.Row():
|
|
|
|
| 101 |
with gr.Column(scale=1):
|
| 102 |
input_text = gr.Textbox(
|
| 103 |
+
label="URL Target",
|
| 104 |
+
placeholder="Masukkan URL...",
|
| 105 |
lines=3
|
| 106 |
)
|
| 107 |
+
submit_btn = gr.Button("🚀 Scan Hybrid", variant="primary")
|
| 108 |
+
gr.Markdown("ℹ️ **Info:** Sistem ini menggunakan 2 lapis keamanan (AI + Daftar Kata Kunci).")
|
|
|
|
|
|
|
| 109 |
|
|
|
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
+
output_text = gr.Textbox(label="Hasil Analisis", lines=8)
|
| 112 |
+
output_chart = gr.Label(label="Indikator Deteksi")
|
|
|
|
|
|
|
| 113 |
|
|
|
|
| 114 |
submit_btn.click(
|
| 115 |
fn=analyze_url,
|
| 116 |
inputs=input_text,
|
| 117 |
outputs=[output_text, output_chart]
|
| 118 |
)
|
| 119 |
|
|
|
|
| 120 |
gr.Markdown("---")
|
| 121 |
gr.Markdown("*Powered by Hugging Face Spaces*")
|
| 122 |
|
|
|
|
| 123 |
if __name__ == "__main__":
|
| 124 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|