Updated image handling
Browse files- handler.py +4 -1
handler.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
from transformers import CLIPProcessor, CLIPModel
|
| 4 |
from PIL import Image
|
| 5 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
@@ -14,11 +16,12 @@ class EndpointHandler:
|
|
| 14 |
print("this shows the custom endpoint handler is being called")
|
| 15 |
inputs = data.pop("inputs", data)
|
| 16 |
text = inputs.pop("text")
|
| 17 |
-
if "image_url" in
|
| 18 |
image_url = inputs.pop("image_url")
|
| 19 |
image = Image.open(requests.get(image_url, stream=True).raw)
|
| 20 |
else:
|
| 21 |
image = inputs.pop("image")
|
|
|
|
| 22 |
processed_inputs = self.processor(text=text, images=image,
|
| 23 |
return_tensors="pt", padding=True, truncation=True)
|
| 24 |
outputs = self.model(**processed_inputs)
|
|
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
import requests
|
| 3 |
+
import io
|
| 4 |
+
import base64
|
| 5 |
from transformers import CLIPProcessor, CLIPModel
|
| 6 |
from PIL import Image
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
| 16 |
print("this shows the custom endpoint handler is being called")
|
| 17 |
inputs = data.pop("inputs", data)
|
| 18 |
text = inputs.pop("text")
|
| 19 |
+
if "image_url" in inputs:
|
| 20 |
image_url = inputs.pop("image_url")
|
| 21 |
image = Image.open(requests.get(image_url, stream=True).raw)
|
| 22 |
else:
|
| 23 |
image = inputs.pop("image")
|
| 24 |
+
image = Image.open(io.BytesIO(base64.b64decode(image)))
|
| 25 |
processed_inputs = self.processor(text=text, images=image,
|
| 26 |
return_tensors="pt", padding=True, truncation=True)
|
| 27 |
outputs = self.model(**processed_inputs)
|