File size: 2,140 Bytes
68ebb5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
import joblib
import requests
import os

from sklearn.ensemble import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier


# Load the saved models
rf_model = joblib.load('rf_model.pkl')
dt_model = joblib.load('dt_model.pkl')
bagging_model = joblib.load('bagging_model.pkl')
ada_model = joblib.load('ada_model.pkl')

class_labels = {
    0: "normal",
    1: "backdoor",
    2: "ddos",
    3: "dos",
    4: "injection",
    5: "password",
    6: "ransomware",
    7: "scanning",
    8: "xss",
    9: "mitm"
}

def detect_intrusion(features, model_choice="Random Forest"):
    # Convert the input string (comma-separated values) into a list of floats
    features = [list(map(float, features.split(",")))]
    
    # Choose the model based on user selection
    if model_choice == "Random Forest":
        model = rf_model
    elif model_choice == "Decision Tree":
        model = dt_model
    elif model_choice == "Bagging Classifier":
        model = bagging_model
    elif model_choice == "AdaBoost Classifier":
        model = ada_model
    else:
        return "Invalid model choice!"
    
    # Predict the class (multi-class classification)
    prediction = model.predict(features)
    predicted_class = prediction[0]  # Get the predicted class (an integer between 0-8)
    
    # Return the human-readable class description
    if predicted_class == 0:
        return "No Intrusion Detected"
    else:
        return f"Intrusion Detected: {class_labels.get(predicted_class, 'Unknown Attack')}"

# Create a Gradio interface
iface = gr.Interface(fn=detect_intrusion, 
                     inputs=[gr.Textbox(label="Input Features (comma-separated)"), 
                             gr.Dropdown(choices=["Random Forest", "Decision Tree", "Bagging Classifier", "AdaBoost Classifier"], label="Select Model")], 
                     outputs="text", 
                     title="Intrusion Detection System",
                     description="Enter features in the format: feature1, feature2, feature3...")

# Launch the interface locally for testing
iface.launch()