SilverDragon9 commited on
Commit
898548b
·
verified ·
1 Parent(s): b3e393a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import requests
4
+ import os
5
+
6
+ from sklearn.ensemble import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier
7
+ from sklearn.tree import DecisionTreeClassifier
8
+
9
+
10
+ # Load the saved models
11
+ rf_model = joblib.load('rf_model.pkl')
12
+ dt_model = joblib.load('decision_tree_model.pkl')
13
+ bagging_model = joblib.load('model_bagging.pkl')
14
+ ada_model = joblib.load('model_adaboost.pkl')
15
+
16
+ class_labels = {
17
+ 0: "normal",
18
+ 1: "backdoor",
19
+ 2: "ddos",
20
+ 3: "dos",
21
+ 4: "injection",
22
+ 5: "password",
23
+ 6: "ransomware",
24
+ 7: "scanning",
25
+ 8: "xss",
26
+ 9: "mitm"
27
+ }
28
+
29
+ def detect_intrusion(features, model_choice="Random Forest"):
30
+ # Convert the input string (comma-separated values) into a list of floats
31
+ features = [list(map(float, features.split(",")))]
32
+
33
+ # Choose the model based on user selection
34
+ if model_choice == "Random Forest":
35
+ model = rf_model
36
+ elif model_choice == "Decision Tree":
37
+ model = decision_tree_model
38
+ elif model_choice == "Bagging Classifier":
39
+ model = model_bagging
40
+ elif model_choice == "AdaBoost Classifier":
41
+ model = model_adaboost
42
+ else:
43
+ return "Invalid model choice!"
44
+
45
+ # Predict the class (multi-class classification)
46
+ prediction = model.predict(features)
47
+ predicted_class = prediction[0] # Get the predicted class (an integer between 0-8)
48
+
49
+ # Return the human-readable class description
50
+ if predicted_class == 0:
51
+ return "No Intrusion Detected"
52
+ else:
53
+ return f"Intrusion Detected: {class_labels.get(predicted_class, 'Unknown Attack')}"
54
+
55
+ # Create a Gradio interface
56
+ iface = gr.Interface(fn=detect_intrusion,
57
+ inputs=[gr.Textbox(label="Input Features (comma-separated)"),
58
+ gr.Dropdown(choices=["Random Forest", "Decision Tree", "Bagging Classifier", "AdaBoost Classifier"], label="Select Model")],
59
+ outputs="text",
60
+ title="Intrusion Detection System",
61
+ description="Enter features in the format: feature1, feature2, feature3...")
62
+
63
+ # Launch the interface locally for testing
64
+ iface.launch()