kumararvindibs commited on
Commit
74b1841
·
verified ·
1 Parent(s): 862e175

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +36 -26
handler.py CHANGED
@@ -6,6 +6,7 @@ import base64
6
  import io
7
  from transformers import BlipForConditionalGeneration, BlipProcessor
8
  import logging
 
9
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
  # Configure logging
11
  logging.basicConfig(level=logging.DEBUG)
@@ -23,36 +24,45 @@ class EndpointHandler():
23
  logging.error(f"----------This is an error message {str(data)}")
24
  input_data = data.get("inputs", {})
25
  logging.warning(f"------input_data-- {str(input_data)}")
26
- # logging.warning(f"------input_data-- {str(input_data)}")
27
- # encoded_images = input_data.get("images")
28
- # # Convert image to bytes
29
- # image = Image.open(encoded_images[0])
30
- # image_bytes = image.tobytes()
31
- # logging.warning(f"---image_bytes----- {str(image_bytes)}")
32
- # # Encode image bytes as base64
33
- # image_base64 = base64.b64encode(image_bytes).decode("utf-8")
34
- # #logging.warning(f"---encoded_images----- {str(image_base64)}")
35
- # # print("000--------", image_base64)
36
- # if not encoded_images:
37
- # logging.warning(f"---encoded_images--not provided in if block--- {str(encoded_images)}")
38
- # return {"captions": [], "error": "No images provided"}
 
 
 
 
 
 
 
 
 
 
39
  try:
40
- # logging.warning(f"---encoded_images-- provided in try block--- {str(encoded_images)}")
41
- # byteImgIO = io.BytesIO()
42
 
43
- # byteImg = Image.open(encoded_images[0])
44
- # byteImg.save(byteImgIO, "PNG")
45
- # byteImgIO.seek(0)
46
- # byteImg = byteImgIO.read()
 
47
 
48
 
49
- # # Non test code
50
- # dataBytesIO = io.BytesIO(byteImg)
51
- # raw_images =[Image.open(dataBytesIO)]
52
- # logging.warning(f"----raw_images----0--- {str(raw_images)}")
53
  # Check if any images were successfully decoded
54
- raw_images = [input_data]
55
- logging.warning(f"---raw_images-raw_images----------- {str(raw_images)}")
56
  if not raw_images:
57
  print("No valid images found.")
58
  processed_inputs = [
@@ -72,4 +82,4 @@ class EndpointHandler():
72
  except Exception as e:
73
  print(f"Error during processing: {str(e)}")
74
  logging.error(f"Error during processing: ----------------{str(e)}")
75
- return {"captions": [], "error": str(e)}
 
6
  import io
7
  from transformers import BlipForConditionalGeneration, BlipProcessor
8
  import logging
9
+ from io import BytesIO
10
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
  # Configure logging
12
  logging.basicConfig(level=logging.DEBUG)
 
24
  logging.error(f"----------This is an error message {str(data)}")
25
  input_data = data.get("inputs", {})
26
  logging.warning(f"------input_data-- {str(input_data)}")
27
+ encoded_images = input_data.get("url")
28
+ print("url---",encoded_images)
29
+ # Convert image to bytes
30
+ # image = Image.open(encoded_images[0])
31
+ #url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
32
+
33
+ # Send a GET request to the URL to get the image data
34
+ response = requests.get(encoded_images)
35
+
36
+ # Read the image data from the response
37
+ image_data = BytesIO(response.content)
38
+
39
+ #image_bytes = image.tobytes()
40
+ # img = Image.open(image_data)
41
+ #print("testing img--------------", img)
42
+ #logging.warning(f"---image_bytes----- {str(image_bytes)}")
43
+ # Encode image bytes as base64
44
+ #image_base64 = base64.b64encode(image_bytes).decode("utf-8")
45
+ #logging.warning(f"---encoded_images----- {str(image_base64)}")
46
+ # print("000--------", image_base64)
47
+ if not encoded_images:
48
+ logging.warning(f"---encoded_images--not provided in if block--- {str(encoded_images)}")
49
+ return {"captions": [], "error": "No images provided"}
50
  try:
51
+ logging.warning(f"---encoded_images-- provided in try block--- {str(encoded_images)}")
52
+ byteImgIO = io.BytesIO()
53
 
54
+ byteImg = Image.open(image_data)
55
+ print("testing img---byteImg-----------", byteImg)
56
+ byteImg.save(byteImgIO, "PNG")
57
+ byteImgIO.seek(0)
58
+ byteImg = byteImgIO.read()
59
 
60
 
61
+ # Non test code
62
+ dataBytesIO = io.BytesIO(byteImg)
63
+ raw_images =[Image.open(dataBytesIO)]
64
+ logging.warning(f"----raw_images----0--- {str(raw_images)}")
65
  # Check if any images were successfully decoded
 
 
66
  if not raw_images:
67
  print("No valid images found.")
68
  processed_inputs = [
 
82
  except Exception as e:
83
  print(f"Error during processing: {str(e)}")
84
  logging.error(f"Error during processing: ----------------{str(e)}")
85
+ return {"captions": [], "error": str(e)}