Vidensogende
commited on
Commit
•
6cc79d4
1
Parent(s):
2227ac3
added custom handler
Browse files- handler.py +22 -0
handler.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
4 |
+
from typing import Dict, List, Any
|
5 |
+
import torch
|
6 |
+
|
7 |
+
class EndpointHandler():
|
8 |
+
def __init__(self, path=""):
|
9 |
+
self.processor = Blip2Processor.from_pretrained("Salesforce/blip-image-captioning-large")
|
10 |
+
self.model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
11 |
+
|
12 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
self.model.to(self.device)
|
14 |
+
|
15 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
16 |
+
image = data.pop("inputs", data)
|
17 |
+
|
18 |
+
processed = self.processor(images=image, return_tensors="pt").to(self.device)
|
19 |
+
|
20 |
+
out = self.model.generate(**processed)
|
21 |
+
|
22 |
+
return self.processor.decode(out[0], skip_special_tokens=True)
|