YassineB commited on
Commit
1b1dde5
·
1 Parent(s): b9f6509

Add clip for test inference

Browse files
Files changed (2) hide show
  1. handler.py +41 -0
  2. requirements.txt +6 -0
handler.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from PIL import Image
3
+ import clip
4
+ import torch
5
+ import requests
6
+ import io
7
+
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+
10
+ class EndpointHandler():
11
+ def __init__(self, path=""):
12
+ # load the optimized model
13
+ self.model, self.preprocess = clip.load('ViT-B/32', device)
14
+ self.model.eval()
15
+ self.model = self.model.to(device)
16
+
17
+
18
+ def __call__(self, data: Any) -> Dict[str, List[float]]:
19
+ """
20
+ Args:
21
+ data (:obj:):
22
+ includes the input data and the parameters for the inference.
23
+ Return:
24
+ A :obj:`dict`:. The object returned should be a dict like {"feature_vector": [0.6331314444541931,0.8802216053009033,...,-0.7866355180740356,]} containing :
25
+ - "feature_vector": A list of floats corresponding to the image embedding.
26
+ """
27
+ inputs = data.pop("inputs", data)
28
+ if inputs.startswith("http") or inputs.startswith("www"):
29
+ response = requests.get(inputs).content
30
+ img = Image.open(io.BytesIO(response))
31
+ else:
32
+ img = Image.open(inputs['image'])
33
+ # decode base64 image to PIL
34
+
35
+ image_input = self.preprocess(img).unsqueeze(0).to(device)
36
+
37
+ # Calculate features
38
+ with torch.no_grad():
39
+ image_features = self.model.encode_image(image_input)
40
+ # postprocess the prediction
41
+ return {"feature_vector": image_features.tolist()[0]}
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ ftfy
2
+ regex
3
+ tqdm
4
+ git+https://github.com/openai/CLIP.git
5
+ torch
6
+ torchvision