Download handler.py from Swarmed/bee-swarm-id: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/Swarmed/bee-swarm-id/resolve/main/handler.py
- Command line
-
hf download hf://Swarmed/bee-swarm-id/handler.py
-
curl -L -o handler.py https://huggingface.co/Swarmed/bee-swarm-id/resolve/main/handler.py
1.15 kB
| 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) | |
| } |