ML-003: Predictor mit Sensor-Validierung und Batch-Interface
This commit is contained in:
33
app/ml/predictor.py
Normal file
33
app/ml/predictor.py
Normal file
@@ -0,0 +1,33 @@
|
||||
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])
|
||||
Reference in New Issue
Block a user