pdich2085 commited on
Commit
76f6af0
·
1 Parent(s): a7af85b

updated handler, again

Browse files
Files changed (1) hide show
  1. handler.py +49 -4
handler.py CHANGED
@@ -13,21 +13,66 @@ class EndpointHandler():
13
  "Salesforce/blip-image-captioning-large"
14
  ).to(device)
15
  self.model.eval()
 
 
16
 
17
  def __call__(self, image_data: str) -> dict:
18
  try:
 
19
  raw_image = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB")
20
 
21
- processed_input = self.processor(raw_image, return_tensors="pt").to(device)
 
 
22
 
23
- with torch.no_grad():
24
- out = self.model.generate(**processed_input)
 
 
 
 
 
 
25
 
26
- caption = self.processor.batch_decode(out, skip_special_tokens=True)[0]
27
  return {"caption": caption}
28
  except Exception as e:
29
  print(f"Error during processing: {str(e)}")
30
  return {"caption": "", "error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
 
33
 
 
13
  "Salesforce/blip-image-captioning-large"
14
  ).to(device)
15
  self.model.eval()
16
+ self.max_length = 16
17
+ self.num_beams = 4
18
 
19
  def __call__(self, image_data: str) -> dict:
20
  try:
21
+ # Convert base64 encoded image string to a PIL Image
22
  raw_image = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB")
23
 
24
+ # Ensure the image is in RGB mode
25
+ if raw_image.mode != "RGB":
26
+ raw_image = raw_image.convert(mode="RGB")
27
 
28
+ # Extract pixel values and move them to the device
29
+ pixel_values = self.processor(raw_image, return_tensors="pt").pixel_values.to(device)
30
+
31
+ # Generate the caption
32
+ gen_kwargs = {"max_length": self.max_length, "num_beams": self.num_beams}
33
+ output_ids = self.model.generate(pixel_values, **gen_kwargs)
34
+
35
+ caption = self.processor.batch_decode(output_ids, skip_special_tokens=True)[0]
36
 
 
37
  return {"caption": caption}
38
  except Exception as e:
39
  print(f"Error during processing: {str(e)}")
40
  return {"caption": "", "error": str(e)}
41
+
42
+
43
+ # === Below code works, but getting the following error:
44
+ # == "error": "argument should be a bytes-like object or ASCII string, not 'dict'"
45
+
46
+ # from PIL import Image
47
+ # import torch
48
+ # import base64
49
+ # from io import BytesIO
50
+ # from transformers import BlipForConditionalGeneration, BlipProcessor
51
+
52
+ # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
53
+
54
+ # class EndpointHandler():
55
+ # def __init__(self, path=""):
56
+ # self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
57
+ # self.model = BlipForConditionalGeneration.from_pretrained(
58
+ # "Salesforce/blip-image-captioning-large"
59
+ # ).to(device)
60
+ # self.model.eval()
61
+
62
+ # def __call__(self, image_data: str) -> dict:
63
+ # try:
64
+ # raw_image = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB")
65
+
66
+ # processed_input = self.processor(raw_image, return_tensors="pt").to(device)
67
+
68
+ # with torch.no_grad():
69
+ # out = self.model.generate(**processed_input)
70
+
71
+ # caption = self.processor.batch_decode(out, skip_special_tokens=True)[0]
72
+ # return {"caption": caption}
73
+ # except Exception as e:
74
+ # print(f"Error during processing: {str(e)}")
75
+ # return {"caption": "", "error": str(e)}
76
 
77
 
78