|
import gradio as gr |
|
import joblib |
|
import requests |
|
import os |
|
|
|
from sklearn.ensemble import RandomForestClassifier |
|
|
|
|
|
rf_model = joblib.load('rf_model.pkl') |
|
|
|
|
|
feature_names = [ |
|
"date", "time", "door_state", "sphone_signal", "label" |
|
] |
|
|
|
class_labels = { |
|
0: "normal", |
|
1: "backdoor", |
|
2: "ddos", |
|
3: "injection", |
|
4: "password", |
|
5: "ransomware", |
|
6: "scanning", |
|
7: "xss", |
|
} |
|
|
|
|
|
rf_model = None |
|
|
|
def detect_intrusion(file): |
|
|
|
try: |
|
log_data = pd.read_csv(file.name) |
|
except Exception as e: |
|
return f"Error reading file: {str(e)}" |
|
|
|
|
|
missing_features = [feature for feature in feature_names if feature not in log_data.columns] |
|
if missing_features: |
|
return f"Missing features in file: {', '.join(missing_features)}" |
|
|
|
|
|
feature_values = log_data[feature_names].astype(float).values |
|
|
|
|
|
predictions = rf_model.predict(feature_values) |
|
|
|
|
|
return log_data[['Prediction']].head().to_string() |
|
|
|
|
|
iface = gr.Interface( |
|
fn=detect_intrusion, |
|
inputs=[ |
|
gr.File(label="Upload Log File (CSV format)") |
|
], |
|
outputs="text", |
|
title="Intrusion Detection System", |
|
description=(""" |
|
Upload a CSV log file containing the following features: |
|
date, time, door_state, sphone_signal, label (without the 'type' column). |
|
Example file structure: |
|
date,time,door_state,sphone_signal,label |
|
2025-03-12,10:45:00,1,-85,normal |
|
""") |
|
) |
|
|
|
|
|
iface.launch() |