|
|
import base64 |
|
|
import io |
|
|
from PIL import Image |
|
|
from autogluon.multimodal import MultiModalPredictor |
|
|
|
|
|
class EndpointHandler: |
|
|
def __init__(self, path=""): |
|
|
|
|
|
self.predictor = MultiModalPredictor.load(path) |
|
|
|
|
|
def __call__(self, data): |
|
|
|
|
|
image_data = data.get("inputs") |
|
|
if not image_data: |
|
|
return {"error": "No input image provided."} |
|
|
|
|
|
try: |
|
|
|
|
|
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)}"} |
|
|
|
|
|
|
|
|
result = self.predictor.predict({"image": image}) |
|
|
probabilities = self.predictor.predict_proba({"image": image}) |
|
|
|
|
|
|
|
|
top_class = result.iloc[0] |
|
|
confidence = probabilities.iloc[0][top_class] |
|
|
|
|
|
return { |
|
|
"label": top_class, |
|
|
"confidence": round(confidence, 4) |
|
|
} |