|
from typing import Dict, List, Any |
|
from transformers import AutoModelForImageClassification, AutoFeatureExtractor |
|
import torch |
|
from PIL import Image |
|
import io |
|
|
|
class EndpointHandler: |
|
def __init__(self): |
|
|
|
model_id = "alexionby/ainoai" |
|
self.model = AutoModelForImageClassification.from_pretrained(model_id) |
|
self.feature_extractor = AutoFeatureExtractor.from_pretrained(model_id) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
|
|
image = data.pop('image', data) |
|
image = Image.open(io.BytesIO(image)) |
|
|
|
|
|
inputs = self.feature_extractor(images=image, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = self.model(**inputs) |
|
|
|
|
|
logits = outputs.logits |
|
probabilities = torch.nn.functional.softmax(logits, dim=-1) |
|
predictions = probabilities.argmax(-1) |
|
|
|
|
|
return {"label": str(predictions.item())} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|