MaheshP98 commited on
Commit
cfdd494
·
verified ·
1 Parent(s): b210736

Create models/anomaly.py

Browse files
Files changed (1) hide show
  1. models/anomaly.py +27 -0
models/anomaly.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```
2
+ import pandas as pd
3
+ from transformers import pipeline
4
+ import logging
5
+
6
+ logger = logging.getLogger(__name__)
7
+
8
+ def detect_anomalies(df):
9
+ """Detect anomalies in log data using a Hugging Face model."""
10
+ logger.info("Detecting anomalies...")
11
+ try:
12
+ detector = pipeline(
13
+ "text-classification",
14
+ model="prajjwal1/bert-tiny",
15
+ tokenizer="prajjwal1/bert-tiny",
16
+ clean_up_tokenization_spaces=True
17
+ )
18
+ df["text"] = df["status"] + " Usage:" + df["usage_count"].astype(str)
19
+ results = detector(df["text"].tolist())
20
+ df["anomaly"] = [r["label"] for r in results]
21
+ anomalies = df[df["anomaly"] == "POSITIVE"]
22
+ logger.info(f"Detected {len(anomalies)} anomalies.")
23
+ return anomalies
24
+ except Exception as e:
25
+ logger.error(f"Failed to detect anomalies: {e}")
26
+ raise
27
+ ```