File size: 1,150 Bytes
0a02a66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import io
from PIL import Image
from autogluon.multimodal import MultiModalPredictor

class EndpointHandler:
    def __init__(self, path=""):
        # Load the pre-trained AutoGluon model
        self.predictor = MultiModalPredictor.load(path)

    def __call__(self, data):
        # Expecting base64-encoded image in 'inputs'
        image_data = data.get("inputs")
        if not image_data:
            return {"error": "No input image provided."}

        try:
            # Decode the base64 image
            image_bytes = base64.b64decode(image_data)
            image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
        except Exception as e:
            return {"error": f"Failed to process image: {str(e)}"}

        # Perform prediction
        result = self.predictor.predict({"image": image})
        probabilities = self.predictor.predict_proba({"image": image})

        # Extract top prediction and its confidence
        top_class = result.iloc[0]
        confidence = probabilities.iloc[0][top_class]

        return {
            "label": top_class,
            "confidence": round(confidence, 4)
        }