File size: 2,181 Bytes
1a91148
 
 
 
 
 
 
8da037b
 
1a91148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
HuggingFace Inference Endpoint Handler for LearningStudio Callout Detection.

This wrapper provides an EMCO-compatible API format for LearningStudio integration,
calling the AWS Lambda-based callout detection pipeline via API Gateway.
"""

from typing import Dict, Any
from inference import inference


class EndpointHandler:
    """
    HuggingFace Inference Endpoint Handler.

    This class provides the interface expected by HuggingFace Inference Endpoints.
    It wraps the callout detection pipeline and transforms outputs to EMCO format.
    """

    def __init__(self, path: str = ""):
        """
        Initialize the endpoint handler.

        Args:
            path: Model path (unused for this wrapper, but required by HF interface)
        """
        # No model to load - this is a wrapper for an external API
        pass

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Process an inference request.

        Args:
            data: Request data with format:
                {
                    "inputs": "image_url_or_base64",
                    "parameters": {...}  # Optional parameters
                }

        Returns:
            EMCO-compatible response:
            {
                "predictions": [
                    {
                        "id": 1,
                        "label": "callout",
                        "class_id": 0,
                        "confidence": 0.95,
                        "bbox": {"x1": 100, "y1": 200, "x2": 300, "y2": 400}
                    },
                    ...
                ],
                "total_detections": N,
                "image": "base64_encoded_image"
            }
        """
        # Extract input
        inputs = data.get("inputs")
        if inputs is None:
            return {
                "error": "Missing 'inputs' field",
                "predictions": [],
                "total_detections": 0,
                "image": ""
            }

        # Extract optional parameters
        parameters = data.get("parameters", {})

        # Call the inference function
        result = inference(inputs, parameters)

        return result