Sanjayraju30 commited on
Commit
72a5765
·
verified ·
1 Parent(s): 0bad9ea

Create modules/ai_engine.py

Browse files
Files changed (1) hide show
  1. modules/ai_engine.py +27 -0
modules/ai_engine.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import torch
3
+ from transformers import TimeSeriesTransformerModel, TimeSeriesTransformerConfig
4
+
5
+ class AIEngine:
6
+ def __init__(self):
7
+ config = TimeSeriesTransformerConfig(
8
+ input_size=4, # SolarGen, WindGen, Tilt, Vibration
9
+ output_size=1, # Health Score
10
+ context_length=10,
11
+ prediction_length=1
12
+ )
13
+ self.model = TimeSeriesTransformerModel(config)
14
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ self.model.to(self.device)
16
+
17
+ def preprocess_data(self, df: pd.DataFrame) -> torch.Tensor:
18
+ features = df[["SolarGen(kWh)", "WindGen(kWh)", "Tilt(°)", "Vibration(g)"]].values
19
+ return torch.tensor(features, dtype=torch.float32).unsqueeze(0).to(self.device)
20
+
21
+ def predict_health(self, df: pd.DataFrame) -> pd.DataFrame:
22
+ input_data = self.preprocess_data(df)
23
+ with torch.no_grad():
24
+ predictions = self.model(input_data).logits.squeeze().cpu().numpy()
25
+ df["HealthScore"] = predictions
26
+ df["ML_Anomaly"] = df["HealthScore"].apply(lambda x: "Risk" if x < 0.5 else "Normal")
27
+ return df