Commit
·
a79245f
1
Parent(s):
2e8bef0
Update handler.py
Browse files- handler.py +16 -4
handler.py
CHANGED
@@ -4,7 +4,10 @@ from typing import Dict, List, Any
|
|
4 |
# import torch
|
5 |
from datetime import datetime
|
6 |
|
|
|
7 |
|
|
|
|
|
8 |
|
9 |
|
10 |
import requests
|
@@ -19,6 +22,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)
|
@@ -71,11 +80,14 @@ class EndpointHandler():
|
|
71 |
|
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("cuda")
|
|
|
|
|
76 |
|
77 |
out = self.model.generate(**inputs)
|
78 |
-
|
|
|
79 |
|
80 |
current = datetime.now()
|
81 |
|
@@ -100,4 +112,4 @@ class EndpointHandler():
|
|
100 |
# new_tokens = output_ids[0, len(input_ids[0]) :]
|
101 |
# output_text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
102 |
|
103 |
-
return [{"gen_text":
|
|
|
4 |
# import torch
|
5 |
from datetime import datetime
|
6 |
|
7 |
+
import torch
|
8 |
|
9 |
+
import logging
|
10 |
+
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
|
11 |
|
12 |
|
13 |
import requests
|
|
|
22 |
self.processor = Blip2Processor.from_pretrained(path)
|
23 |
self.model = Blip2ForConditionalGeneration.from_pretrained(path, device_map="auto")
|
24 |
|
25 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
+
|
27 |
+
self.model.to(self.device)
|
28 |
+
|
29 |
+
logging.info('Model moved to device-' + self.device)
|
30 |
+
|
31 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
32 |
# self.model.eval()
|
33 |
# self.model.to(device=device, dtype=self.torch_dtype)
|
|
|
80 |
|
81 |
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
82 |
|
83 |
+
# question = "how many dogs are in the picture?"
|
84 |
+
# inputs = self.processor(raw_image, question, return_tensors="pt").to("cuda")
|
85 |
+
|
86 |
+
inputs = self.processor(raw_image, return_tensors="pt").to("cuda")
|
87 |
|
88 |
out = self.model.generate(**inputs)
|
89 |
+
|
90 |
+
generated_text = self.processor.batch_decode(out, skip_special_tokens=True)[0].strip()
|
91 |
|
92 |
current = datetime.now()
|
93 |
|
|
|
112 |
# new_tokens = output_ids[0, len(input_ids[0]) :]
|
113 |
# output_text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
114 |
|
115 |
+
return [{"gen_text":generated_text, "time_elapsed": str(current-now)}]
|