Sanjayraju30 commited on
Commit
115c399
·
verified ·
1 Parent(s): bd95f45

Update modules/ai_engine.py

Browse files
Files changed (1) hide show
  1. modules/ai_engine.py +7 -7
modules/ai_engine.py CHANGED
@@ -5,8 +5,8 @@ from transformers import TimeSeriesTransformerModel, TimeSeriesTransformerConfig
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
  )
@@ -14,14 +14,14 @@ class AIEngine:
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
 
5
  class AIEngine:
6
  def __init__(self):
7
  config = TimeSeriesTransformerConfig(
8
+ input_size=4,
9
+ output_size=1,
10
  context_length=10,
11
  prediction_length=1
12
  )
 
14
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
  self.model.to(self.device)
16
 
17
+ def preprocess(self, df: pd.DataFrame) -> torch.Tensor:
18
+ data = df[["SolarGen(kWh)", "WindGen(kWh)", "Tilt(°)", "Vibration(g)"]].values
19
+ return torch.tensor(data, dtype=torch.float32).unsqueeze(0).to(self.device)
20
 
21
  def predict_health(self, df: pd.DataFrame) -> pd.DataFrame:
22
+ input_tensor = self.preprocess(df)
23
  with torch.no_grad():
24
+ predictions = self.model(input_tensor).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