|
from transformers import pipeline |
|
from PIL import Image |
|
import requests |
|
from io import BytesIO |
|
import base64 |
|
from typing import Dict, List, Any |
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
|
|
self.classifier = pipeline("zero-shot-image-classification", model="rroset/CLIP-ViT-B-32-laion2B-s34B-b79K") |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
|
|
image_base64 = data.get("inputs", None) |
|
parameters = data.get("parameters", None) |
|
|
|
if image_base64 is None or parameters is None: |
|
raise ValueError("Input data or parameters not provided") |
|
|
|
|
|
candidate_labels = parameters.get("candidate_labels", None) |
|
if candidate_labels is None: |
|
raise ValueError("Candidate labels not provided") |
|
|
|
|
|
image_data = base64.b64decode(image_base64) |
|
image = Image.open(BytesIO(image_data)) |
|
|
|
|
|
results = self.classifier(images=image, candidate_labels=candidate_labels) |
|
|
|
|
|
return results |
|
|