Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +77 -0
- label_encoder.pkl +3 -0
- requirements.txt +4 -0
- scaler.pkl +3 -0
- xgboost_model.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
from xgboost import XGBClassifier
|
| 6 |
+
|
| 7 |
+
# Load các mô hình và encoder
|
| 8 |
+
model = joblib.load("xgboost_model.pkl")
|
| 9 |
+
scaler = joblib.load("scaler.pkl")
|
| 10 |
+
label_encoder = joblib.load("label_encoder.pkl")
|
| 11 |
+
|
| 12 |
+
def predict_attack(file):
|
| 13 |
+
# Đọc file CSV
|
| 14 |
+
df = pd.read_csv(file.name)
|
| 15 |
+
|
| 16 |
+
# Tiền xử lý dữ liệu
|
| 17 |
+
X = scaler.transform(df)
|
| 18 |
+
|
| 19 |
+
# Dự đoán
|
| 20 |
+
predictions = model.predict(X)
|
| 21 |
+
prediction_probs = model.predict_proba(X)
|
| 22 |
+
confidences = np.max(prediction_probs, axis=1)
|
| 23 |
+
predicted_labels = label_encoder.inverse_transform(predictions)
|
| 24 |
+
|
| 25 |
+
# Thêm cột dự đoán và độ tin cậy
|
| 26 |
+
df["Predicted Label"] = predicted_labels
|
| 27 |
+
df["Confidence"] = confidences.round(4)
|
| 28 |
+
|
| 29 |
+
# Tóm tắt kết quả
|
| 30 |
+
summary = df["Predicted Label"].value_counts().reset_index()
|
| 31 |
+
summary.columns = ["Class", "Count"]
|
| 32 |
+
summary["Percentage"] = (summary["Count"] / len(df) * 100).round(2).astype(str) + "%"
|
| 33 |
+
summary["Avg Confidence"] = df.groupby("Predicted Label")["Confidence"].mean().round(4).values
|
| 34 |
+
|
| 35 |
+
# Confusion matrix giả định: không có nhãn thật → dùng cột dự đoán chính
|
| 36 |
+
labels = label_encoder.classes_
|
| 37 |
+
confusion_matrix_html = "<table border='1'><tr><th>Actual \\ Predicted</th>" + "".join(
|
| 38 |
+
f"<th>{label}</th>" for label in labels
|
| 39 |
+
) + "</tr>"
|
| 40 |
+
|
| 41 |
+
for actual in labels:
|
| 42 |
+
confusion_matrix_html += f"<tr><td>{actual}</td>"
|
| 43 |
+
for predicted in labels:
|
| 44 |
+
count = len(df[(df["Predicted Label"] == predicted) & (df["Predicted Label"] == actual)])
|
| 45 |
+
cell_color = " style='background-color: #ffcccc'" if count > 0 and actual != predicted else ""
|
| 46 |
+
confusion_matrix_html += f"<td{cell_color}>{count}</td>"
|
| 47 |
+
confusion_matrix_html += "</tr>"
|
| 48 |
+
confusion_matrix_html += "</table>"
|
| 49 |
+
|
| 50 |
+
# Hiển thị kết quả dạng bảng HTML
|
| 51 |
+
summary_html = summary.to_html(index=False)
|
| 52 |
+
prediction_sample = df.head(10).to_html(index=False)
|
| 53 |
+
|
| 54 |
+
html_output = f"""
|
| 55 |
+
<h3>Kết quả phân tích:</h3>
|
| 56 |
+
<h4>Summary of Detections:</h4>
|
| 57 |
+
{summary_html}
|
| 58 |
+
<h4>Actual vs Predicted Labels Confusion Matrix:</h4>
|
| 59 |
+
{confusion_matrix_html}
|
| 60 |
+
<h4>Prediction Results (Total: {len(df)} records)</h4>
|
| 61 |
+
<p>Top 10 records shown:</p>
|
| 62 |
+
{prediction_sample}
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
return html_output
|
| 66 |
+
|
| 67 |
+
# Tạo giao diện
|
| 68 |
+
iface = gr.Interface(
|
| 69 |
+
fn=predict_attack,
|
| 70 |
+
inputs=gr.File(label="Tải file CSV đã xử lý"),
|
| 71 |
+
outputs=gr.HTML(),
|
| 72 |
+
title="🔍 Dự đoán tấn công mạng bằng mô hình XGBoost",
|
| 73 |
+
description="Tải file CSV để hệ thống phân tích và dự đoán loại tấn công (nếu có).",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
iface.launch()
|
label_encoder.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f00da92b437e090ec8e6d476164a02a77c3192e2bcecd47e759e903a3080fcaf
|
| 3 |
+
size 619
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
xgboost
|
| 3 |
+
pandas
|
| 4 |
+
scikit-learn
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7fdd3414b15059fa232c0d0bf51f2764a44acb657f83d5476f3b9b69718bf4e9
|
| 3 |
+
size 2447
|
xgboost_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5895ddcdda7b262cd5986158e0f13008755f5ee5b56cc7c1c130772ac8f98b8e
|
| 3 |
+
size 2710360
|