File size: 3,510 Bytes
11a414e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""PCB Analyzer Orchestrator Entry Point for Collector Lite & Recycler Portal."""
from __future__ import annotations

from typing import Any
from .model_client import BaseModelClient, MockModelClient, Detection
from .purity_assay import assay_pcb, assess_bis_discrepancy, AssayedBoard
from .vernacular_voice import generate_vernacular_voice_alert, VoiceAlert


class PCBAnalyzer:
    """Single unified orchestrator for PCB micro-component purity assaying."""

    def __init__(self, model_client: BaseModelClient | None = None):
        self.model_client = model_client or MockModelClient()

    def analyze_board(
        self,
        front_image: Any,
        back_image: Any | None = None,
        base_price: float = 100.0,
        form_factor: str = "desktop_atx",
        lang: str = "hi",
    ) -> dict[str, Any]:
        """Perform 2-photo workflow PCB component purity assaying."""

        # Stage 1: Front side detection
        detections_front = self.model_client.predict(front_image)

        # Stage 2: Back side detection (if provided)
        detections_back = []
        if back_image is not None:
            detections_back = self.model_client.predict(back_image)

        all_detections = detections_front + detections_back

        # Run Board Integrity Score assay
        assayed: AssayedBoard = assay_pcb(
            detections=all_detections,
            base_price=base_price,
            form_factor=form_factor,
        )

        # Generate vernacular voice alert
        voice_alert: VoiceAlert = generate_vernacular_voice_alert(
            bis_band=assayed.status_band,
            adjusted_price=assayed.adjusted_price,
            lang=lang,
        )

        return {
            "form_factor": assayed.form_factor,
            "board_integrity_score": assayed.board_integrity_score,
            "base_price": assayed.base_price,
            "adjusted_price": assayed.adjusted_price,
            "price_floor": assayed.price_floor,
            "status_band": assayed.status_band,
            "detected_counts": assayed.detected_counts,
            "expected_counts": assayed.expected_counts,
            "fraud_desoldered_pads": assayed.fraud_desoldered_pads,
            "factory_unpopulated_pads": assayed.factory_unpopulated_pads,
            "occluded_regions": assayed.occluded_regions,
            "voice_alert": {
                "lang": voice_alert.lang,
                "transcript": voice_alert.transcript,
                "audio_fallback_path": voice_alert.audio_fallback_path,
            },
        }

    def verify_intake_discrepancy(
        self,
        collector_bis: float,
        recycler_front_image: Any,
        recycler_back_image: Any | None = None,
        form_factor: str = "desktop_atx",
    ) -> dict[str, Any]:
        """Verify intake lot discrepancy at Recycler Portal against collector BIS."""
        recycler_analysis = self.analyze_board(
            front_image=recycler_front_image,
            back_image=recycler_back_image,
            form_factor=form_factor,
        )
        recycler_bis = recycler_analysis["board_integrity_score"]
        status = assess_bis_discrepancy(collector_bis, recycler_bis)

        return {
            "collector_bis": collector_bis,
            "recycler_bis": recycler_bis,
            "bis_delta": round(collector_bis - recycler_bis, 1),
            "discrepancy_status": status,  # "pass", "advisory_flag", "critical_fraud_alert"
            "recycler_assay": recycler_analysis,
        }