CodebaseAi commited on
Commit
c2e4dd0
·
1 Parent(s): 10d6e33

Detection2

Browse files
Files changed (1) hide show
  1. routes/offline_detection.py +14 -10
routes/offline_detection.py CHANGED
@@ -154,25 +154,29 @@ def offline_predict():
154
 
155
  except Exception as e:
156
  return jsonify(success=False, message=f"Model Initialization Error: {str(e)}"), 500
 
157
 
158
  # 4. Prediction Logic
159
  try:
160
- # Reorder columns to match the EXACT training order
 
 
 
 
161
  input_data = df[expected]
162
-
163
  if model_type == "bcc":
164
  scaler = model_data.get('scaler')
165
  encoder = model_data.get('encoder')
166
-
167
- # Scale features (Critical for BCC/MLP models)
168
- scaled_data = scaler.transform(input_data.values)
 
 
 
169
  preds = model.predict(scaled_data)
170
-
171
- # Convert numeric 0/1 to "Normal"/"DDoS"
172
  labels = encoder.inverse_transform(preds)
173
- else:
174
- # CICIDS (usually Random Forest) doesn't always need scaling
175
- labels = model.predict(input_data)
176
 
177
  # 5. Result Formatting for React Frontend
178
  df["prediction"] = labels
 
154
 
155
  except Exception as e:
156
  return jsonify(success=False, message=f"Model Initialization Error: {str(e)}"), 500
157
+
158
 
159
  # 4. Prediction Logic
160
  try:
161
+ # 1. Map protocols first!
162
+ proto_map = {'TCP': 6, 'UDP': 17, 'ICMP': 1, 'tcp': 6, 'udp': 17, 'icmp': 1}
163
+ df['protocol'] = df['protocol'].apply(lambda x: proto_map.get(x, x) if isinstance(x, str) else x)
164
+
165
+ # 2. Reorder columns
166
  input_data = df[expected]
167
+
168
  if model_type == "bcc":
169
  scaler = model_data.get('scaler')
170
  encoder = model_data.get('encoder')
171
+
172
+ # Ensure all columns are numeric before scaling
173
+ numeric_input = input_data.apply(pd.to_numeric, errors='coerce').fillna(0)
174
+
175
+ # 3. Scale features
176
+ scaled_data = scaler.transform(numeric_input.values) # Now it's all floats!
177
  preds = model.predict(scaled_data)
178
+
 
179
  labels = encoder.inverse_transform(preds)
 
 
 
180
 
181
  # 5. Result Formatting for React Frontend
182
  df["prediction"] = labels