Update handler.py
Browse files- handler.py +13 -50
handler.py
CHANGED
@@ -1,7 +1,3 @@
|
|
1 |
-
# import sys
|
2 |
-
# import base64
|
3 |
-
# import logging
|
4 |
-
# import copy
|
5 |
import numpy as np
|
6 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration, BlipForQuestionAnswering, BitsAndBytesConfig
|
7 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
@@ -19,13 +15,7 @@ class EndpointHandler():
|
|
19 |
# self.base_model = Blip2ForConditionalGeneration.from_pretrained(self.model_base, load_in_8bit=True)
|
20 |
# self.pipe = Blip2ForConditionalGeneration.from_pretrained(self.model_base, load_in_8bit=True, torch_dtype=torch.float16)
|
21 |
|
22 |
-
|
23 |
-
# "EleutherAI/gpt-neox-20b",
|
24 |
-
# torch_dtype=torch.float16,
|
25 |
-
# device_map="auto",
|
26 |
-
# quantization_config=quantization_config,
|
27 |
-
|
28 |
-
quantization_config = BitsAndBytesConfig(load_in_8bit=True)#, bnb_4bit_compute_dtype=torch.bfloat16)
|
29 |
|
30 |
# self.processor = Blip2Processor.from_pretrained(self.model_name)
|
31 |
self.processor = AutoProcessor.from_pretrained(self.model_name)
|
@@ -38,32 +28,6 @@ class EndpointHandler():
|
|
38 |
).to(self.device)
|
39 |
# self.model = PeftModel.from_pretrained(self.model_name, self.base_model_name).to(self.device)
|
40 |
|
41 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
42 |
-
self.model.to(self.device)
|
43 |
-
|
44 |
-
# def _generate_answer(
|
45 |
-
# self,
|
46 |
-
# model_path,
|
47 |
-
# prompt,
|
48 |
-
# # num_inference_steps=25,
|
49 |
-
# # guidance_scale=7.5,
|
50 |
-
# # num_images_per_prompt=1
|
51 |
-
# ):
|
52 |
-
|
53 |
-
# self.pipe.to(self.device)
|
54 |
-
|
55 |
-
# # pil_images = self.pipe(
|
56 |
-
# # prompt=prompt,
|
57 |
-
# # num_inference_steps=num_inference_steps,
|
58 |
-
# # guidance_scale=guidance_scale,
|
59 |
-
# # num_images_per_prompt=num_images_per_prompt).images
|
60 |
-
|
61 |
-
# # np_images = []
|
62 |
-
# # for i in range(len(pil_images)):
|
63 |
-
# # np_images.append(np.asarray(pil_images[i]))
|
64 |
-
|
65 |
-
# return np.stack(np_images, axis=0)
|
66 |
-
|
67 |
# inputs = data.get("inputs")
|
68 |
# imageBase64 = inputs.get("image")
|
69 |
# # imageURL = inputs.get("image")
|
@@ -96,13 +60,14 @@ class EndpointHandler():
|
|
96 |
inputs = data.pop("inputs", data)
|
97 |
parameters = data.pop("parameters", {})
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
question = inputs["question"]
|
108 |
|
@@ -112,14 +77,12 @@ class EndpointHandler():
|
|
112 |
# image = Image.open(requests.get(imageBase64, stream=True).raw)
|
113 |
# image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
114 |
#### https://huggingface.co/SlowPacer/witron-image-captioning/blob/main/handler.py
|
115 |
-
inputs = data.pop("inputs", data)
|
116 |
-
parameters = data.pop("parameters", {})
|
117 |
|
118 |
-
if isinstance(inputs, Image.Image):
|
119 |
-
|
120 |
-
else:
|
121 |
-
|
122 |
-
|
123 |
|
124 |
# processed_images = self.processor(images=raw_images, return_tensors="pt")
|
125 |
# processed_images["pixel_values"] = processed_images["pixel_values"].to(device)
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration, BlipForQuestionAnswering, BitsAndBytesConfig
|
3 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
|
|
15 |
# self.base_model = Blip2ForConditionalGeneration.from_pretrained(self.model_base, load_in_8bit=True)
|
16 |
# self.pipe = Blip2ForConditionalGeneration.from_pretrained(self.model_base, load_in_8bit=True, torch_dtype=torch.float16)
|
17 |
|
18 |
+
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# self.processor = Blip2Processor.from_pretrained(self.model_name)
|
21 |
self.processor = AutoProcessor.from_pretrained(self.model_name)
|
|
|
28 |
).to(self.device)
|
29 |
# self.model = PeftModel.from_pretrained(self.model_name, self.base_model_name).to(self.device)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# inputs = data.get("inputs")
|
32 |
# imageBase64 = inputs.get("image")
|
33 |
# # imageURL = inputs.get("image")
|
|
|
60 |
inputs = data.pop("inputs", data)
|
61 |
parameters = data.pop("parameters", {})
|
62 |
|
63 |
+
try:
|
64 |
+
imageBase64 = inputs["image"]
|
65 |
+
# image = Image.open(BytesIO(base64.b64decode(imageBase64.split(",")[1].encode())))
|
66 |
+
image = Image.open(BytesIO(base64.b64decode(imageBase64)))
|
67 |
|
68 |
+
except:
|
69 |
+
image_url = inputs['image']
|
70 |
+
image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
71 |
|
72 |
question = inputs["question"]
|
73 |
|
|
|
77 |
# image = Image.open(requests.get(imageBase64, stream=True).raw)
|
78 |
# image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
79 |
#### https://huggingface.co/SlowPacer/witron-image-captioning/blob/main/handler.py
|
|
|
|
|
80 |
|
81 |
+
# if isinstance(inputs, Image.Image):
|
82 |
+
# image = [inputs]
|
83 |
+
# else:
|
84 |
+
# inputs = isinstance(inputs, str) and [inputs] or inputs
|
85 |
+
# image = [Image.open(BytesIO(base64.b64decode(_img))) for _img in inputs]
|
86 |
|
87 |
# processed_images = self.processor(images=raw_images, return_tensors="pt")
|
88 |
# processed_images["pixel_values"] = processed_images["pixel_values"].to(device)
|