File size: 1,813 Bytes
b926327 a4e73a2 b926327 e62633b b926327 e62633b b926327 a4e73a2 dd31dc4 a4e73a2 dd31dc4 b926327 a4e73a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from io import BytesIO
import base64
import traceback
from PIL import Image
import torch
from transformers import CLIPProcessor, CLIPTextModel, CLIPVisionModelWithProjection
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class EndpointHandler():
def __init__(self, path=""):
self.text_model = CLIPTextModel.from_pretrained("rbanfield/clip-vit-large-patch14").to(device)
self.image_model = CLIPVisionModelWithProjection.from_pretrained("rbanfield/clip-vit-large-patch14").to(device)
self.processor = CLIPProcessor.from_pretrained("rbanfield/clip-vit-large-patch14")
def __call__(self, data):
try:
text_input = None
if isinstance(data, dict):
print('data is a dict: ', data)
inputs = data.pop("inputs", None)
text_input = inputs.get('text',None)
image_data = BytesIO(base64.b64decode(inputs['image'])) if 'image' in inputs else None
else:
# assuming its an image sent via binary
image_data = BytesIO(data)
if text_input:
processor = self.processor(text=text_input, return_tensors="pt", padding=True).to(device)
with torch.no_grad():
return {'embeddings':self.text_model(**processor).pooler_output.tolist()[0]}
elif image_data:
image = Image.open(image_data)
processor = self.processor(images=image, return_tensors="pt").to(device)
with torch.no_grad():
return {'embeddings':self.image_model(**processor).image_embeds.tolist()[0]}
else:
return {'embeddings':None}
except Exception:
return {'Error':traceback.format_exc()}
|