Files
sillyhome-next/app/ml/predictor.py

33 lines
1.2 KiB
Python

from __future__ import annotations
import logging
from functools import lru_cache
from typing import Sequence
from app.ml.feature_store import FeatureVector
from app.ml.training import TrainingPipeline, TrainedArtifact
logger = logging.getLogger(__name__)
class Predictor:
def __init__(self, pipeline: TrainingPipeline) -> None:
self._pipeline = pipeline
def predict(self, artifact_id: str, entity: FeatureVector) -> str:
artifact = self._pipeline.export(artifact_id)
if entity.sensor_id not in artifact.supported_sensors:
raise ValueError(
f"Sensor '{entity.sensor_id}' wird vom Modell '{artifact_id}' nicht unterstützt."
)
return f"{artifact_id}:{entity.sensor_id}:{entity.values}"
def predict_batch(self, artifact_id: str, entities: Sequence[FeatureVector]) -> list[str]:
return [self.predict(artifact_id, entity) for entity in entities]
@staticmethod
def default_artifact(pipeline: TrainingPipeline) -> TrainedArtifact:
artifacts = list(pipeline._artifacts)
if not artifacts:
raise ValueError("Kein trainiertes Modell gefunden.")
return pipeline.export(artifacts[-1])