Skip to content

Commit

Permalink
safe image handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ispirtraian committed Apr 23, 2024
1 parent affb85b commit 3ba0dae
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/src/serving/serving_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ def _predict_job (self, model_type: str, input, params:dict = None):
with open(task_content_path, 'wb') as file:
file.write(content.encode('utf-8'))
elif isinstance(content, Image.Image):
content.save(task_content_path, format=content.format)
with content as img:
img.save(task_content_path, format=content.format)
else:
raise Exception("Unsupported content type")

Expand Down Expand Up @@ -426,15 +427,16 @@ def predict_texts(self, texts: List[str], params: dict = None):
return self._predict_job('text', texts, params)

def predict_image(self, image_data: bytes, params: dict = None):
image = Image.open(io.BytesIO(image_data))
self.P(f"Predict image with size: {len(image_data)}")
return self._predict_job('image', image, params)
with Image.open(io.BytesIO(image_data)) as image:
self.P(f"Predict image with size: {len(image_data)}")
result = self._predict_job('image', image, params)
return result

def predict_images(self, image_data_list: List[bytes], params: dict = None):
images = []
for image_data in image_data_list:
image = Image.open(io.BytesIO(image_data))
images.append(image)
with Image.open(io.BytesIO(image_data)) as image:
images.append(image)
self.P(f"Predict {len(image_data_list)} images")
return self._predict_job('image', images, params)

Expand Down

0 comments on commit 3ba0dae

Please sign in to comment.