Upload handler.py with huggingface_hub
Browse files- handler.py +43 -0
handler.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from transformers import AutoFeatureExtractor, EfficientNetForImageClassification
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
class EndpointHandler:
|
| 9 |
+
def __init__(self, path=""):
|
| 10 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Load feature extractor
|
| 13 |
+
self.feature_extractor = AutoFeatureExtractor.from_pretrained(path)
|
| 14 |
+
|
| 15 |
+
# Load model
|
| 16 |
+
self.model = EfficientNetForImageClassification.from_pretrained(path)
|
| 17 |
+
|
| 18 |
+
# Replace the classification head with a regression head
|
| 19 |
+
self.model.classifier = torch.nn.Linear(self.model.classifier.in_features, 1)
|
| 20 |
+
|
| 21 |
+
# Load custom weights
|
| 22 |
+
self.model.load_state_dict(torch.load(f"{path}/model.pt", map_location=self.device))
|
| 23 |
+
self.model.to(self.device)
|
| 24 |
+
self.model.eval()
|
| 25 |
+
|
| 26 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 27 |
+
# Get the image data from the request
|
| 28 |
+
image_bytes = data.get("inputs", "")
|
| 29 |
+
|
| 30 |
+
# Decode and open the image
|
| 31 |
+
image = Image.open(io.BytesIO(base64.b64decode(image_bytes))).convert('RGB')
|
| 32 |
+
|
| 33 |
+
# Prepare the image for the model
|
| 34 |
+
inputs = self.feature_extractor(images=image, return_tensors="pt")
|
| 35 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 36 |
+
|
| 37 |
+
# Make prediction
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
outputs = self.model(**inputs)
|
| 40 |
+
|
| 41 |
+
prediction = outputs.logits.item() # For regression, we directly use the output
|
| 42 |
+
|
| 43 |
+
return [{"prediction": float(prediction)}]
|