File size: 4,975 Bytes
119c847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586a69a
 
119c847
 
 
 
 
 
 
 
 
 
 
 
 
 
586a69a
 
119c847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
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