agiera commited on
Commit
556d4bd
1 Parent(s): 0ac3561

Create pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +46 -0
pipeline.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ import PIL
3
+ import torch
4
+ import base64
5
+ import os
6
+ import io
7
+ from transformers import ViTImageProcessor, ViTModel
8
+
9
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
+
11
+
12
+ class PreTrainedPipeline():
13
+ def __init__(self, path=""):
14
+ self.model = ViTModel.from_pretrained(
15
+ pretrained_model_name_or_path=os.path.join(
16
+ path, 'pytorch_model.bin'),
17
+ config=os.path.join(path, 'config.json')
18
+ )
19
+ self.model.eval()
20
+ self.model = self.model.to(device)
21
+
22
+ self.processor = ViTImageProcessor.from_pretrained(
23
+ pretrained_model_name_or_path=os.path.join(
24
+ path, 'preprocessor_config.json')
25
+ )
26
+
27
+ def __call__(self, data: Any) -> Dict[str, List[float]]:
28
+ """
29
+ Args:
30
+ data (:dict | str:):
31
+ Includes the input data and the parameters for the inference.
32
+ Inputs should be an image encoded in base 64.
33
+ Return:
34
+ A :obj:`dict`:. The object returned should be a dict like
35
+ {"feature_vector": [0.6331314444541931,...,-0.7866355180740356,]} containing :
36
+ - "feature_vector": A list of floats corresponding to the image embedding.
37
+ """
38
+ inputs = data.pop("inputs", data)
39
+
40
+ # decode base64 image to PIL
41
+ image = PIL.Image.open(io.BytesIO(base64.b64decode(inputs['image'])))
42
+ inputs = self.processor(images=image, return_tensors="pt")
43
+ outputs = self.model(**inputs)
44
+ feature_vector = outputs.last_hidden_state[0, 0].tolist()
45
+ # postprocess the prediction
46
+ return {"feature_vector": feature_vector}