sankath commited on
Commit
d0d5435
·
verified ·
1 Parent(s): cf08e63

Upload detect_anomalies.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. detect_anomalies.py +9 -11
detect_anomalies.py CHANGED
@@ -1,11 +1,9 @@
1
- # detect_anomalies.py
2
- # Standalone anomaly predictor: loads the trained LogSentry-LM and flags log
3
- # lines the model finds "surprising" (high next-token loss => likely anomalous).
4
- #
5
- # Usage:
6
  # python detect_anomalies.py "Failed password for root from 1.2.3.4 port 22 ssh2"
7
  # python detect_anomalies.py --file some_logs.txt
8
- # python detect_anomalies.py # runs a built-in demo set
9
  import os
10
  import sys
11
  import argparse
@@ -29,11 +27,11 @@ def pick_device():
29
 
30
  def load_model(device):
31
  if not os.path.exists(MODEL_FILE):
32
- sys.exit(f"Model checkpoint '{MODEL_FILE}' not found. "
33
  f"Run `python train_and_generate.py` first to train and save it.")
34
  ckpt = torch.load(MODEL_FILE, map_location=device)
35
- model = LogSentryLM(vocab_size=ckpt["vocab_size"],
36
- max_seq_len=ckpt["max_seq_len"]).to(device)
37
  model.load_state_dict(ckpt["model_state"])
38
  model.eval()
39
  tokenizer = build_or_load_tokenizer(LOG_FILE, TOKENIZER_FILE)
@@ -43,7 +41,7 @@ def load_model(device):
43
  def predict(model, tokenizer, lines, ckpt, device):
44
  threshold = ckpt["threshold"]
45
  max_seq_len = ckpt["max_seq_len"]
46
- print(f"Anomaly threshold (loss > {threshold:.3f} => ANOMALY)\n")
47
  for line in lines:
48
  result = score_line(model, tokenizer, line, device, max_seq_len=max_seq_len)
49
  if result is None:
@@ -69,7 +67,7 @@ if __name__ == "__main__":
69
  elif args.lines:
70
  lines = args.lines
71
  else:
72
- # Built-in demo: two genuine log lines + one clearly off-distribution line.
73
  lines = [
74
  "Failed password for root from 112.95.230.3 port 49204 ssh2",
75
  "[Sun Dec 04 04:47:44 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties",
 
1
+ # file: detect_anomalies.py
2
+ # Standalone predictor: load the trained model + saved threshold and flag lines
3
+ # the model finds surprising. Run after train_and_generate.py has saved a checkpoint.
 
 
4
  # python detect_anomalies.py "Failed password for root from 1.2.3.4 port 22 ssh2"
5
  # python detect_anomalies.py --file some_logs.txt
6
+ # python detect_anomalies.py # built-in demo lines
7
  import os
8
  import sys
9
  import argparse
 
27
 
28
  def load_model(device):
29
  if not os.path.exists(MODEL_FILE):
30
+ sys.exit(f"[ERROR] Model checkpoint '{MODEL_FILE}' not found. "
31
  f"Run `python train_and_generate.py` first to train and save it.")
32
  ckpt = torch.load(MODEL_FILE, map_location=device)
33
+ # Rebuild the exact architecture from the saved config, then load weights
34
+ model = LogSentryLM(vocab_size=ckpt["vocab_size"], max_seq_len=ckpt["max_seq_len"]).to(device)
35
  model.load_state_dict(ckpt["model_state"])
36
  model.eval()
37
  tokenizer = build_or_load_tokenizer(LOG_FILE, TOKENIZER_FILE)
 
41
  def predict(model, tokenizer, lines, ckpt, device):
42
  threshold = ckpt["threshold"]
43
  max_seq_len = ckpt["max_seq_len"]
44
+ print(f"[DETECT] Anomaly threshold (loss > {threshold:.3f} => ANOMALY)\n")
45
  for line in lines:
46
  result = score_line(model, tokenizer, line, device, max_seq_len=max_seq_len)
47
  if result is None:
 
67
  elif args.lines:
68
  lines = args.lines
69
  else:
70
+ # demo: two genuine lines + one clearly off-distribution line
71
  lines = [
72
  "Failed password for root from 112.95.230.3 port 49204 ssh2",
73
  "[Sun Dec 04 04:47:44 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties",