Upload handler.py
Browse files- handler.py +25 -0
handler.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
11 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
12 |
+
parameters = data.pop("inputs",data)
|
13 |
+
inputs = data.pop("inputs", data)
|
14 |
+
|
15 |
+
if parameters is not None:
|
16 |
+
prediction = self.pipeline(inputs, **parameters)
|
17 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
18 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
19 |
+
prompt = "USER: <image>\nWhat does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT:"
|
20 |
+
outputs = self.pipeline(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
21 |
+
return outputs
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|