adasdimchom
commited on
Commit
•
ba84673
1
Parent(s):
d9b4a75
Upload handler.py
Browse files- handler.py +36 -0
handler.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
2 |
+
from typing import Dict, List, Any
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import pipeline
|
5 |
+
import requests
|
6 |
+
import torch
|
7 |
+
|
8 |
+
class EndpointHandler():
|
9 |
+
def __init__(self, path=""):
|
10 |
+
"""
|
11 |
+
path:
|
12 |
+
"""
|
13 |
+
# Preload all the elements you are going to need at inference.
|
14 |
+
# pseudo:
|
15 |
+
# self.model= load_model(path)
|
16 |
+
#self.processor = Blip2Processor.from_pretrained(path)
|
17 |
+
self.pipeline = pipeline(model = path)
|
18 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
+
|
20 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
21 |
+
"""
|
22 |
+
data args:
|
23 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
24 |
+
kwargs
|
25 |
+
Return:
|
26 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
27 |
+
"""
|
28 |
+
inputs = data.pop("inputs", data)
|
29 |
+
image_url = inputs['image_url']
|
30 |
+
#image = Image.open(requests.get(image_url, stream=True).raw)
|
31 |
+
#processed_image = self.processor(images=image, return_tensors="pt").to(self.device, torch.float16)
|
32 |
+
|
33 |
+
#generated_ids = self.pipeline(**inputs)
|
34 |
+
#generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
35 |
+
|
36 |
+
return image_url
|