SilverDragon9 commited on
Commit
022be5b
·
1 Parent(s): 46396b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -38,17 +38,16 @@ class_labels = {
38
  9: "mitm"
39
  }
40
 
41
- def detect_intrusion(feature_string, model_choice="Random Forest"):
42
- # Split input string into key-value pairs and map them to the feature values
43
- features = dict(item.split(" : ") for item in feature_string.split(", "))
44
-
45
- # Ensure all feature names are in the input
46
- feature_values = []
47
- for feature_name in feature_names:
48
- if feature_name in features:
49
- feature_values.append(float(features[feature_name]))
50
- else:
51
- return f"Missing feature: {feature_name}"
52
 
53
  # Choose the model based on user selection
54
  if model_choice == "Random Forest":
@@ -72,18 +71,20 @@ def detect_intrusion(feature_string, model_choice="Random Forest"):
72
  else:
73
  return f"Intrusion Detected: {class_labels.get(predicted_class, 'Unknown Attack')}"
74
 
75
- # Create a Gradio interface
76
- iface = gr.Interface(fn=detect_intrusion,
77
- inputs=[gr.Textbox(label="Input Features (e.g., src_ip : 16 , src_port : 49321 , ...)"),
78
- gr.Dropdown(choices=["Random Forest", "Decision Tree", "Bagging Classifier", "AdaBoost Classifier"], label="Select Model")],
79
- outputs="text",
80
- title="Intrusion Detection System",
81
- description="""
82
- Enter features as key-value pairs in the format:
83
- feature_name1:value1, feature_name2:value2, ...
84
- Example: src_ip:16, src_port:49321, dst_ip:210, dst_port:8080, proto:1, service:5, ...
85
- Note: Ensure all required features are provided and take extra care for spaces around the colons and the commas a space before and after both characters.
86
- """)
 
 
87
 
88
  # Launch the interface locally for testing
89
  iface.launch()
 
38
  9: "mitm"
39
  }
40
 
41
+ def detect_intrusion(feature_values, model_choice="Random Forest"):
42
+ # Ensure the length of feature_values matches feature_names
43
+ if len(feature_values) != len(feature_names):
44
+ return "Please fill in all the required feature values."
45
+
46
+ # Convert the input values to floats and match them with feature names
47
+ try:
48
+ feature_values = [float(value) for value in feature_values]
49
+ except ValueError:
50
+ return "Please enter valid numerical values for all fields."
 
51
 
52
  # Choose the model based on user selection
53
  if model_choice == "Random Forest":
 
71
  else:
72
  return f"Intrusion Detected: {class_labels.get(predicted_class, 'Unknown Attack')}"
73
 
74
+ # Create Gradio input fields for each feature
75
+ inputs = [gr.Textbox(label=feature_name) for feature_name in feature_names[:-1]] # Exclude "label" field from inputs
76
+
77
+ # Add model choice dropdown
78
+ inputs.append(gr.Dropdown(choices=["Random Forest", "Decision Tree", "Bagging Classifier", "AdaBoost Classifier"], label="Select Model"))
79
+
80
+ # Create the Gradio interface
81
+ iface = gr.Interface(
82
+ fn=detect_intrusion,
83
+ inputs=inputs,
84
+ outputs="text",
85
+ title="Intrusion Detection System",
86
+ description="Fill in the blank fields for the network traffic features, and choose the model to detect intrusions."
87
+ )
88
 
89
  # Launch the interface locally for testing
90
  iface.launch()