""" Detection Service — Inference Controller (Enterprise Edition) Orchestrates inference pipelines with Redis caching, validation, and structured responses. Fully self-contained — no external service calls. """ from __future__ import annotations import json import time from typing import Any, Dict, Optional import structlog from app.core.config import get_settings from app.core.logging import get_logger from app.db.redis import get_redis from app.services.inference_router import InferenceRouter from app.services.preprocessing_service import PreprocessingService logger = get_logger(__name__) class InferenceController: """ Controls inference flow with Redis caching and enterprise responses. Thread-safe — delegates to singleton model services. """ def __init__(self) -> None: self._router = InferenceRouter() self._preprocessor = PreprocessingService() self._settings = get_settings() # ── Cache helpers ── async def _cache_get(self, key: str) -> Optional[Dict]: """Retrieve cached inference result.""" if not self._settings.cache_enabled: return None try: redis = await get_redis() if redis is None: return None data = await redis.get(key) if data: logger.info("cache_hit", key=key[:40]) return json.loads(data) except Exception as exc: logger.warning("cache_get_error", error=str(exc)) return None async def _cache_set(self, key: str, value: Dict, ttl: int | None = None) -> None: """Store inference result in cache with configurable TTL.""" if not self._settings.cache_enabled: return try: redis = await get_redis() if redis is None: return await redis.set( key, json.dumps(value, default=str), ex=ttl if ttl is not None else self._settings.cache_ttl, ) except Exception as exc: logger.warning("cache_set_error", error=str(exc)) def _make_cache_key(self, endpoint: str, image_hash: str, mode: str = "default") -> str: return f"{self._settings.cache_version}:{endpoint}:{image_hash}:{mode}" # ── Detection (YOLO only) ── async def handle_detect( self, image_bytes: bytes, mode: str = "balanced", request_id: str = "", confidence: Optional[float] = None, ) -> Dict[str, Any]: """POST /detect — YOLO-only with SHA256 cache.""" image_hash = self._preprocessor.compute_image_hash(image_bytes) cache_key = self._make_cache_key("detect", image_hash, mode) cached = await self._cache_get(cache_key) if cached: cached["cached"] = True return cached result = await self._router.detect(image_bytes, mode=mode, confidence=confidence) result["cached"] = False await self._cache_set(cache_key, result, ttl=self._settings.cache_ttl_detect) return result # ── Classification (CNN only) ── async def handle_classify( self, image_bytes: bytes, mode: str = "balanced", request_id: str = "", ) -> Dict[str, Any]: """POST /classify — CNN-only with SHA256 cache.""" image_hash = self._preprocessor.compute_image_hash(image_bytes) cache_key = self._make_cache_key("classify", image_hash, mode) cached = await self._cache_get(cache_key) if cached: cached["cached"] = True return cached result = await self._router.classify(image_bytes, mode=mode) result["cached"] = False await self._cache_set(cache_key, result, ttl=self._settings.cache_ttl_classify) return result # ── Analyze (YOLO → CNN → Decision Fusion) ── async def handle_analyze( self, image_bytes: bytes, mode: str = "balanced", cnn_mode: str = "balanced", request_id: str = "", ) -> Dict[str, Any]: """POST /analyze — Full enterprise pipeline with fusion.""" image_hash = self._preprocessor.compute_image_hash(image_bytes) cache_key = self._make_cache_key("analyze", image_hash, f"{mode}_{cnn_mode}") cached = await self._cache_get(cache_key) if cached: cached["cached"] = True if "metadata" in cached: cached["metadata"]["request_id"] = request_id return cached result = await self._router.analyze(image_bytes, mode=mode, cnn_mode=cnn_mode) result["cached"] = False # Inject request_id into metadata if "metadata" in result: result["metadata"]["request_id"] = request_id await self._cache_set(cache_key, result, ttl=self._settings.cache_ttl_analyze) return result