-
bert-log-anomaly-detection is a BERT-based NLP model fine-tuned for single SQL transaction log anomaly detection.
-
The model classifies each database transaction log as either Normal or Anomaly, with the goal of supporting AI-powered fraud detection and cybersecurity monitoring systems.
-
This model was developed as part of the Samsung × KBTG Digital Fraud Cybersecurity Hackathon (Thailand) under the AI-Powered Fraud Detection & Prevention track.
This model analyzes individual SQL database transaction logs and detects abnormal patterns that may indicate fraudulent, malicious, or suspicious behavior.
1import torch
2from transformers import BertForSequenceClassification, BertTokenizer
3
4MODEL_PATH = "AungMoonLord/bert-log-anomaly-detection"
5
6model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
7tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
8
9model.eval()
1# Perfom log preprocessing
2def add_prefix_token(text): # log data must pass this code before training/inferencing
3 # clean log
4 text = text.replace("\t", " ")
5 text = text.strip()
6 # add token
7 if text[0].isalpha() or text[3].isalpha():
8 return "[SQL]\n" + text
9 else:
10 return "[LOG]\n" + text
1def predict_log(log_text):
2 log_text = add_prefix_token(log_text)
3 inputs = tokenizer(
4 log_text,
5 return_tensors="pt",
6 truncation=True,
7 padding=True, # for cases when the inference contains more than 1 log, i.e., batch size > 1
8 max_length=128
9 )
10
11 with torch.no_grad():
12 logits = model(**inputs).logits
13 pred = torch.argmax(logits, dim=1).item()
14 prob = torch.softmax(logits, dim=-1).tolist()[0]
15
16 return "Normal" if pred == 1 else "Anomaly", prob
1# Example 1
2text1 = "SELECT * FROM users WHERE id = 1 OR 1=1"
3print(predict_log(text1))
4
5# Example 2
6text2 = "2025-01-06 14:23:45 | User: anonymous | IP: 203.154.89.102 | Duration: 0.05s SELECT * FROM users WHERE username = 'admin' OR '1'='1' -- ' AND password = 'x'"
7print(predict_log(text2))
8
9# Example 3
10text3 = "3051-06-22T07:20:02.296945Z 3 Query select e3mJKDCCY from 7Q8SpG8LLEWhrfpe4s5 where ph4d = 'a1S9hQa92uC1EAyJf2Y';"
11print(predict_log(text3))
-
Multi-log sequence anomaly detection
-
Non-textual anomaly detection
-
SQL database transaction logs (1,611 samples) synthetically generated by ChatGPT, Qwen, DeepSeek, Grok, Gemini, and Claude
-
Each log labeled as either Normal or Anomaly
-
Data prepared for single-log classification
The model demonstrates strong anomaly detection capability with high recall, making it suitable for fraud detection and cybersecurity use cases.