Spaces:
Sleeping
Sleeping
File size: 1,106 Bytes
72a5765 115c399 72a5765 115c399 72a5765 115c399 72a5765 115c399 72a5765 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import pandas as pd
import torch
from transformers import TimeSeriesTransformerModel, TimeSeriesTransformerConfig
class AIEngine:
def __init__(self):
config = TimeSeriesTransformerConfig(
input_size=4,
output_size=1,
context_length=10,
prediction_length=1
)
self.model = TimeSeriesTransformerModel(config)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model.to(self.device)
def preprocess(self, df: pd.DataFrame) -> torch.Tensor:
data = df[["SolarGen(kWh)", "WindGen(kWh)", "Tilt(°)", "Vibration(g)"]].values
return torch.tensor(data, dtype=torch.float32).unsqueeze(0).to(self.device)
def predict_health(self, df: pd.DataFrame) -> pd.DataFrame:
input_tensor = self.preprocess(df)
with torch.no_grad():
predictions = self.model(input_tensor).logits.squeeze().cpu().numpy()
df["HealthScore"] = predictions
df["ML_Anomaly"] = df["HealthScore"].apply(lambda x: "Risk" if x < 0.5 else "Normal")
return df
|