Create handler.py
Browse files- handler.py +20 -0
handler.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn.functional as F
|
2 |
+
from torch import Tensor
|
3 |
+
from transformers import AutoTokenizer, AutoModel, AutoProcessor
|
4 |
+
from torch import cuda
|
5 |
+
|
6 |
+
class EndpointHandler():
|
7 |
+
def __init__(self, path=""):
|
8 |
+
self.processor = AutoProcessor.from_pretrained(path)
|
9 |
+
self.model = AutoModel.from_pretrained(path, trust_remote_code=True)
|
10 |
+
self.device = "cuda" if cuda.is_available() else "cpu"
|
11 |
+
self.model.to(self.device)
|
12 |
+
|
13 |
+
def __call__(self, data: Dict[str, Any]) -> List[List[int]]:
|
14 |
+
image = data.pop("inputs",data)
|
15 |
+
|
16 |
+
processed = self.processor(images=image, return_tensors="pt").to(self.device)
|
17 |
+
|
18 |
+
prediction = self.model(processed["pixel_values"])
|
19 |
+
|
20 |
+
return prediction.item()
|