Commit ·
206cd4d
1
Parent(s): 68637ef
Update handler
Browse files- handler.py +10 -3
handler.py
CHANGED
|
@@ -3,9 +3,10 @@ from typing import Dict, List, Any
|
|
| 3 |
# from transformers import AutoTokenizer
|
| 4 |
# import torch
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
import requests
|
| 11 |
from PIL import Image
|
|
@@ -19,6 +20,12 @@ class EndpointHandler():
|
|
| 19 |
self.processor = Blip2Processor.from_pretrained(path)
|
| 20 |
self.model = Blip2ForConditionalGeneration.from_pretrained(path, device_map="auto")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
# self.model.eval()
|
| 24 |
# self.model.to(device=device, dtype=self.torch_dtype)
|
|
@@ -72,7 +79,7 @@ class EndpointHandler():
|
|
| 72 |
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
| 73 |
|
| 74 |
question = "how many dogs are in the picture?"
|
| 75 |
-
inputs = self.processor(raw_image, question, return_tensors="pt").to(
|
| 76 |
|
| 77 |
out = self.model.generate(**inputs)
|
| 78 |
output_text = self.processor.decode(out[0], skip_special_tokens=True)
|
|
|
|
| 3 |
# from transformers import AutoTokenizer
|
| 4 |
# import torch
|
| 5 |
from datetime import datetime
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
import logging
|
| 9 |
+
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
|
| 10 |
|
| 11 |
import requests
|
| 12 |
from PIL import Image
|
|
|
|
| 20 |
self.processor = Blip2Processor.from_pretrained(path)
|
| 21 |
self.model = Blip2ForConditionalGeneration.from_pretrained(path, device_map="auto")
|
| 22 |
|
| 23 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
|
| 25 |
+
self.model.to(self.device)
|
| 26 |
+
|
| 27 |
+
logging.info('Model moved to device-' + self.device)
|
| 28 |
+
|
| 29 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
# self.model.eval()
|
| 31 |
# self.model.to(device=device, dtype=self.torch_dtype)
|
|
|
|
| 79 |
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
| 80 |
|
| 81 |
question = "how many dogs are in the picture?"
|
| 82 |
+
inputs = self.processor(raw_image, question, return_tensors="pt").to(self.device)
|
| 83 |
|
| 84 |
out = self.model.generate(**inputs)
|
| 85 |
output_text = self.processor.decode(out[0], skip_special_tokens=True)
|