File size: 11,697 Bytes
42d88ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import json
import time
import cv2
import numpy as np
import google.generativeai as genai
from pathlib import Path
from typing import Optional
from uuid import UUID

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from Backend.core.config import settings
from Backend.core.events import event_bus, IssueClassified, IssueCreated
from Backend.core.logging import get_logger
from Backend.core.schemas import ClassificationResult, DetectionBox, CLASS_ID_TO_CATEGORY, IssueCategory
from Backend.database.models import Classification, Issue, IssueImage, IssueEvent
from Backend.orchestration.base import BaseAgent
from Backend.utils.fuzzy_match import auto_validate_issue
from Backend.utils.storage import save_bytes, download_from_supabase, get_upload_url

logger = get_logger(__name__, agent_name="VisionAgent")

if settings.gemini_api_key:
    genai.configure(api_key=settings.gemini_api_key)


class VisionAgent(BaseAgent):
    _model = None
    
    def __init__(self, db: Optional[AsyncSession] = None):
        super().__init__("VisionAgent")
        self.db = db
        if settings.gemini_api_key:
            self.gemini_model = genai.GenerativeModel('gemma-3-27b-it')
        else:
            self.gemini_model = None
    
    @classmethod
    def load_model(cls):
        if cls._model is None:
            from ultralytics import YOLO
            model_path = settings.model_path
            if not model_path.exists():
                raise FileNotFoundError(f"Model not found: {model_path}")
            cls._model = YOLO(str(model_path))
            logger.info(f"YOLO model loaded from {model_path}")
        return cls._model
    
    @classmethod
    def get_model(cls):
        if cls._model is None:
            cls.load_model()
        return cls._model
    
    async def download_image(self, remote_path: str) -> bytes:
        return await download_from_supabase(remote_path)
    
    async def save_annotated(self, results, original_path: str, subfolder: str) -> str:
        im_array = results[0].plot()
        
        original_name = Path(original_path).stem
        annotated_filename = f"annotated_{original_name}.jpg"
        
        _, buffer = cv2.imencode('.jpg', im_array, [cv2.IMWRITE_JPEG_QUALITY, 90])
        image_bytes = buffer.tobytes()
        
        remote_path = await save_bytes(image_bytes, annotated_filename, subfolder=subfolder)
        return remote_path
    
    async def run_inference(self, image_data: bytes) -> tuple[list, float]:
        model = self.get_model()
        
        nparr = np.frombuffer(image_data, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        if img is None:
            raise ValueError("Invalid image data")
        
        start_time = time.perf_counter()
        results = model.predict(
            source=img,
            conf=settings.model_confidence_threshold,
            imgsz=settings.model_input_size,
            verbose=False,
        )
        inference_time = (time.perf_counter() - start_time) * 1000
        
        return results, inference_time

    async def gemini_classify_image(
        self,
        image_data: bytes,
        description: Optional[str] = None
    ) -> tuple[Optional[IssueCategory], float, Optional[str]]:
        if not self.gemini_model:
            return None, 0.0, None

        allowed = [
            {"class_id": k, "class_name": v.value}
            for k, v in CLASS_ID_TO_CATEGORY.items()
        ]
        prompt = (
            "Classify the photo into exactly one of the allowed categories. "
            "Return ONLY valid JSON with keys: class_id (int), confidence (0.0-1.0), reasoning (max 80 chars).\n\n"
            f"Allowed categories: {json.dumps(allowed)}\n"
            f"User description: {(description or '')[:200]}"
        )

        try:
            response = self.gemini_model.generate_content(
                [
                    {"text": prompt},
                    {
                        "inline_data": {
                            "mime_type": "image/jpeg",
                            "data": image_data,
                        }
                    },
                ]
            )
            text = (response.text or "").replace("```json", "").replace("```", "").strip()
            data = json.loads(text)
            class_id = data.get("class_id")
            confidence = float(data.get("confidence", 0.0))
            reasoning = data.get("reasoning")
            if not isinstance(class_id, int):
                return None, 0.0, None
            category = CLASS_ID_TO_CATEGORY.get(class_id)
            if not category:
                return None, 0.0, None
            confidence = max(0.0, min(1.0, confidence))
            return category, confidence, reasoning
        except Exception as e:
            logger.error(f"Gemini vision classification failed: {e}")
            return None, 0.0, None
    
    def extract_detections(self, results) -> list[DetectionBox]:
        detections = []
        for result in results:
            boxes = result.boxes
            if boxes is not None:
                for i in range(len(boxes)):
                    class_id = int(boxes.cls[i].item())
                    confidence = float(boxes.conf[i].item())
                    bbox = tuple(boxes.xyxy[i].tolist())
                    
                    category = CLASS_ID_TO_CATEGORY.get(class_id)
                    if category:
                        detections.append(DetectionBox(
                            class_id=class_id,
                            class_name=category.value,
                            confidence=confidence,
                            bbox=bbox,
                        ))
        return detections
    
    async def classify_image(
        self,
        image_path: str,
        subfolder: str = "",
        description: Optional[str] = None
    ) -> tuple[list[DetectionBox], str, Optional[IssueCategory], float, Optional[str]]:
        image_data = await self.download_image(image_path)
        results, inference_time = await self.run_inference(image_data)
        annotated_path = await self.save_annotated(results, image_path, subfolder)
        detections = self.extract_detections(results)

        gemini_category = None
        gemini_confidence = 0.0
        gemini_reasoning = None
        if self.gemini_model and (not detections or max(d.confidence for d in detections) < 0.5):
            gemini_category, gemini_confidence, gemini_reasoning = await self.gemini_classify_image(
                image_data=image_data,
                description=description
            )
        
        logger.info(f"Inference completed in {inference_time:.2f}ms, {len(detections)} detections")
        return detections, annotated_path, gemini_category, gemini_confidence, gemini_reasoning
    
    async def process_issue(
        self,
        issue_id: UUID,
        image_paths: list[str],
        description: Optional[str] = None
    ) -> ClassificationResult:
        all_detections = []
        annotated_paths = []
        total_time = 0.0
        subfolder = str(issue_id)

        gemini_best_category = None
        gemini_best_confidence = 0.0
        gemini_best_reasoning = None
        
        for path in image_paths:
            start = time.perf_counter()
            detections, annotated_path, gemini_category, gemini_confidence, gemini_reasoning = await self.classify_image(
                path,
                subfolder=subfolder,
                description=description
            )
            total_time += (time.perf_counter() - start) * 1000
            all_detections.extend(detections)
            annotated_paths.append(annotated_path)

            if gemini_category and gemini_confidence > gemini_best_confidence:
                gemini_best_category = gemini_category
                gemini_best_confidence = gemini_confidence
                gemini_best_reasoning = gemini_reasoning
            
            if self.db:
                query = select(IssueImage).where(IssueImage.file_path == path)
                result = await self.db.execute(query)
                image_record = result.scalar_one_or_none()
                if image_record:
                    image_record.annotated_path = annotated_path
        
        result = ClassificationResult(
            issue_id=issue_id,
            detections=all_detections,
            annotated_urls=[get_upload_url(p) for p in annotated_paths],
            inference_time_ms=total_time,
        )

        if gemini_best_category and (not result.primary_category or result.primary_confidence < 0.5):
            result.primary_category = gemini_best_category
            result.primary_confidence = gemini_best_confidence
        
        detected_categories = list(set(d.class_name for d in all_detections))
        auto_validated, validation_reason = auto_validate_issue(description, detected_categories)
        
        validation_source = "auto" if auto_validated else "pending_manual"
        new_state = "validated" if auto_validated else "reported"
        
        self.log_decision(
            issue_id=issue_id,
            decision=f"Validation: {validation_source}",
            reasoning=validation_reason
        )
        
        if self.db:
            classification = Classification(
                issue_id=issue_id,
                primary_category=result.primary_category.value if result.primary_category else None,
                primary_confidence=result.primary_confidence,
                detections_json=json.dumps([d.model_dump() for d in all_detections]),
                inference_time_ms=total_time,
            )
            self.db.add(classification)
            
            issue = await self.db.get(Issue, issue_id)
            if issue:
                issue.state = new_state
                issue.validation_source = validation_source
                issue.validation_reason = validation_reason
            
            event_record = IssueEvent(
                issue_id=issue_id,
                event_type="classified",
                agent_name=self.name,
                event_data=json.dumps({
                    "category": result.primary_category.value if result.primary_category else None,
                    "confidence": result.primary_confidence,
                    "detections_count": len(all_detections),
                    "validation_source": validation_source,
                    "annotated_images": annotated_paths,
                    "gemini_category": gemini_best_category.value if gemini_best_category else None,
                    "gemini_confidence": gemini_best_confidence,
                    "gemini_reasoning": gemini_best_reasoning,
                })
            )
            self.db.add(event_record)
            await self.db.flush()
        
        if result.primary_category:
            event = IssueClassified(
                issue_id=issue_id,
                category=result.primary_category.value,
                confidence=result.primary_confidence,
                detections_count=len(all_detections),
                metadata={
                    "validation_source": validation_source,
                    "validation_reason": validation_reason,
                    "annotated_images": [get_upload_url(p) for p in annotated_paths],
                }
            )
            await event_bus.publish(event)
        
        return result
    
    async def handle(self, event: IssueCreated) -> None:
        await self.process_issue(
            event.issue_id,
            event.image_paths,
            event.description
        )