Commit ·
df87eca
1
Parent(s): eaa60c2
uploaded handler and requirements for it
Browse files- handler.py +22 -0
- requirements.txt +2 -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/blip2-opt-2.7b")
|
| 10 |
+
self.model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 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)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers
|
| 2 |
+
git+https://github.com/huggingface/accelerate
|