Spaces:
Sleeping
Sleeping
Update modules/ai_engine.py
Browse files- 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,
|
9 |
-
output_size=1,
|
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
|
18 |
-
|
19 |
-
return torch.tensor(
|
20 |
|
21 |
def predict_health(self, df: pd.DataFrame) -> pd.DataFrame:
|
22 |
-
|
23 |
with torch.no_grad():
|
24 |
-
predictions = self.model(
|
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
|