florentgbelidji HF staff commited on
Commit
35bf230
1 Parent(s): b45f054

Added handler.py

Browse files
Files changed (1) hide show
  1. handler.py +27 -0
handler.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from typing import Dict, List, Any
3
+ from transformers import pipeline
4
+
5
+
6
+ # check for GPU
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+
10
+ class EndpointHandler():
11
+ def __init__(self, path=""):
12
+ # Preload all the elements you are going to need at inference.
13
+ # pseudo:
14
+ self.pipeline= pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning", device=device)
15
+
16
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
17
+ """
18
+ data args:
19
+ inputs (:obj: `str` | `PIL.Image` | `np.array`)
20
+ kwargs
21
+ Return:
22
+ A :obj:`list` | `dict`: will be serialized and returned
23
+ """
24
+
25
+ inputs = data.pop("inputs", data)
26
+ return self.pipeline(inputs)
27
+