|
import gradio as gr |
|
import joblib |
|
|
|
|
|
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"): |
|
|
|
features = [list(map(float, features.split(",")))] |
|
|
|
|
|
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!" |
|
|
|
|
|
prediction = model.predict(features) |
|
predicted_class = prediction[0] |
|
|
|
|
|
if predicted_class == 0: |
|
return "No Intrusion Detected" |
|
else: |
|
return f"Intrusion Detected: {class_labels.get(predicted_class, 'Unknown Attack')}" |
|
|
|
|
|
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...") |
|
|
|
|
|
iface.launch() |