ML-003: Predictor mit Sensor-Validierung und Batch-Interface

This commit is contained in:
2026-06-11 00:38:45 +02:00
parent 24be7a4f11
commit 3cf9af3515
2 changed files with 81 additions and 0 deletions

33
app/ml/predictor.py Normal file
View 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])