CodebaseAi commited on
Commit
70a72e6
·
1 Parent(s): 935d233
Files changed (1) hide show
  1. routes/offline_detection.py +40 -15
routes/offline_detection.py CHANGED
@@ -24,8 +24,21 @@ ALLOWED_EXT = {"csv", "pcap"}
24
 
25
  # --- FEATURE DEFINITIONS (As per your Model Logs) ---
26
  BCC_FEATURES = [
27
- "proto", "src_port", "dst_port", "flow_duration", "total_fwd_pkts", "total_bwd_pkts",
28
- "flags_numeric", "payload_len", "header_len", "rate", "iat", "syn", "ack", "rst", "fin"
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  ]
30
 
31
  CICIDS_FEATURES = [
@@ -82,24 +95,36 @@ def offline_predict():
82
 
83
  # 2. Flexible Feature Mapping & Flag Extraction
84
  # Renames common CSV headers to the specific technical names the model expects
 
85
  mapping = {
86
- 'Protocol': 'proto', 'proto': 'proto',
87
- 'Source Port': 'src_port', 'src_port': 'src_port',
88
- 'Destination Port': 'dst_port', 'dst_port': 'dst_port',
89
- 'Flow Duration': 'flow_duration',
90
- 'Total Fwd Packets': 'total_fwd_pkts',
91
- 'Total Bwd Packets': 'total_bwd_pkts',
92
- 'Payload Len': 'payload_len',
93
- 'Header Len': 'header_len',
94
- 'IAT': 'iat', 'Rate': 'rate'
95
  }
96
  df = df.rename(columns=mapping)
97
 
98
- # Smart Flag Extraction: If 'syn', 'ack', etc. are missing, try to get them from a 'flags' string
99
  if 'flags' in df.columns:
100
- for f in ['syn', 'ack', 'rst', 'fin']:
101
- if f not in df.columns:
102
- df[f] = df['flags'].astype(str).str.lower().apply(lambda x: 1 if f in x else 0)
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  # 3. Model Loading & Feature Alignment
105
  try:
 
24
 
25
  # --- FEATURE DEFINITIONS (As per your Model Logs) ---
26
  BCC_FEATURES = [
27
+ "protocol", # instead of proto
28
+ "src_port",
29
+ "dst_port",
30
+ "duration", # instead of flow_duration
31
+ "fwd_packets_count", # instead of total_fwd_pkts
32
+ "bwd_packets_count", # instead of total_bwd_pkts
33
+ "flags", # instead of flags_numeric
34
+ "payload_len",
35
+ "header_len",
36
+ "bytes_rate", # instead of rate
37
+ "iat",
38
+ "syn_flag_counts", # instead of syn
39
+ "ack_flag_counts", # instead of ack
40
+ "rst_flag_counts", # instead of rst
41
+ "fin_flag_counts" # instead of fin
42
  ]
43
 
44
  CICIDS_FEATURES = [
 
95
 
96
  # 2. Flexible Feature Mapping & Flag Extraction
97
  # Renames common CSV headers to the specific technical names the model expects
98
+ # 2. Flexible Feature Mapping (Translate to EXACT fit-time names)
99
  mapping = {
100
+ 'Protocol': 'protocol', 'proto': 'protocol',
101
+ 'Source Port': 'src_port',
102
+ 'Destination Port': 'dst_port',
103
+ 'Flow Duration': 'duration', 'flow_duration': 'duration',
104
+ 'Total Fwd Packets': 'fwd_packets_count', 'total_fwd_pkts': 'fwd_packets_count',
105
+ 'Total Bwd Packets': 'bwd_packets_count', 'total_bwd_pkts': 'bwd_packets_count',
106
+ 'Rate': 'bytes_rate', 'rate': 'bytes_rate',
107
+ 'syn': 'syn_flag_counts', 'ack': 'ack_flag_counts',
108
+ 'rst': 'rst_flag_counts', 'fin': 'fin_flag_counts'
109
  }
110
  df = df.rename(columns=mapping)
111
 
112
+ # 3. Updated Flag Extraction for the new names
113
  if 'flags' in df.columns:
114
+ # Check for numeric flags if the model expects them
115
+ df['flags'] = pd.to_numeric(df['flags'], errors='coerce').fillna(0)
116
+
117
+ # Extract individual flag counts if missing
118
+ flag_map = {
119
+ 'syn_flag_counts': 'syn',
120
+ 'ack_flag_counts': 'ack',
121
+ 'rst_flag_counts': 'rst',
122
+ 'fin_flag_counts': 'fin'
123
+ }
124
+ for model_name, csv_name in flag_map.items():
125
+ if model_name not in df.columns:
126
+ # If we have a 'flags' string, try to find the flag name in it
127
+ df[model_name] = df['flags'].astype(str).str.lower().apply(lambda x: 1 if csv_name in x else 0)
128
 
129
  # 3. Model Loading & Feature Alignment
130
  try: