hongyiyang
commited on
Commit
•
680f9d0
1
Parent(s):
89112cf
add custom handler
Browse files- handler.py +30 -0
handler.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
|
6 |
+
class EndpointHandler():
|
7 |
+
def __init__(self, path="."):
|
8 |
+
self.pipeline = pipeline("image-to-text", model=path)
|
9 |
+
|
10 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
11 |
+
"""
|
12 |
+
data args:
|
13 |
+
inputs (:obj: `str`)
|
14 |
+
date (:obj: `str`)
|
15 |
+
Return:
|
16 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
17 |
+
"""
|
18 |
+
# get inputs
|
19 |
+
prompt = "USER: <image>\nWhat's in the image\nASSISTANT:"
|
20 |
+
default_url = "https://cdn.faire.com/fastly/3c335e5c06d3027964ee8351093784c94dfa264e5eb26430c803f4ab3c44da84.jpeg"
|
21 |
+
|
22 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
23 |
+
|
24 |
+
url = data.pop("image_url", default_url)
|
25 |
+
|
26 |
+
# run normal prediction
|
27 |
+
outputs = self.pipeline(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
28 |
+
print(outputs)
|
29 |
+
|
30 |
+
return outputs
|