bee-swarm-id / handler.py
Swarmed's picture
Create handler.py
0a02a66 verified
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)
}