Update handler.py
Browse files- 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 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
try:
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
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)}
|