Skip to content

Commit

Permalink
fix: multiple images
Browse files Browse the repository at this point in the history
  • Loading branch information
ispirtraian committed Apr 19, 2024
1 parent 0a60ace commit 964c9ac
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
18 changes: 18 additions & 0 deletions app/src/main_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ async def predict_image(
result = eng.predict_image(contents, params)
return result

# image request
@router_serving.post("/predict/images")
async def predict_images(
images: List[UploadFile] = File(...),
exec_params: Optional[ExecParams] = Form(
None,
description="Example:\n\n \{\n\"device\":\"cpu|gpu\",\n\"no_runs\":int\n\}"
)
):
contents =[]
for image in images:
contents.append(await image.read())

params = exec_params.dict() if exec_params else None
# Process the image data
result = eng.predict_images(contents, params)
return result

@router_serving.post("/predict")
async def predict(
text: str ,
Expand Down
88 changes: 59 additions & 29 deletions app/src/serving/serving_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,31 @@ def appmon_callback(self):
input = self.__input
if input is None:
# try loading from disk
task_content_path = self.cache_root+"/tasks/"+taskid+".bin"
try:
with open(task_content_path, 'rb') as file:
byte_data = file.read()
# convert if necessary
if model_type == 'text':
input_str = byte_data.decode('utf-8')
try:
#check if list
input = json.loads(input_str)
except json.JSONDecodeError:
input = input_str
if model_type == 'image':
input = Image.open(io.BytesIO(byte_data))
except Exception as exc:
self.P("Content file read error: {}".format(exc))
content_list = []
iterate = True
while iterate:
task_content_path = f"{self.cache_root}/tasks/{taskid}_{len(content_list)}.bin"
try:
with open(task_content_path, 'rb') as file:
byte_data = file.read()
# convert if necessary
if model_type == 'text':
input_str = byte_data.decode('utf-8')
try:
#check if list
content_list.append(json.loads(input_str))
except json.JSONDecodeError:
content_list.append(input_str)
if model_type == 'image':
content_list.append(Image.open(io.BytesIO(byte_data)))

except FileNotFoundError:
iterate = False
except IOError as exc:
self.P("Content file read error: {}".format(exc))
iterate = False
if len(content_list) > 0:
input = content_list if len(content_list) > 1 else content_list[0]
if input is not None:
result = self._predict(
model_type,
Expand Down Expand Up @@ -295,20 +304,33 @@ def _predict_job (self, model_type: str, input, params:dict = None):

if not self.__available :
#if current insance is unavailable, save input to file
task_content_path = self.cache_root+"/tasks/"+taskid+".bin"

try:
bytes_data = None
os.makedirs(os.path.dirname(task_content_path), exist_ok=True)
#transform input to bytes if necessary
if isinstance(input, str):
bytes_data = input.encode('utf-8')
elif isinstance(input, Image.Image):
bytes_data = input.tobytes()
elif isinstance(input, List):
bytes_data = json.dumps(input).encode('utf-8')
with open(task_content_path, 'wb') as file:
file.write(bytes_data)
print(f"Data successfully written to {task_content_path}")
content_list = []
if isinstance(input, List):
if isinstance(input[0], str):
content_list.append(json.dumps(input))
else:
content_list.extend(input)
else:
content_list.append(input)

ndx=0
for ndx, content in enumerate(content_list):
task_content_path = f"{self.cache_root}/tasks/{taskid}_{ndx}.bin"
os.makedirs(os.path.dirname(task_content_path), exist_ok=True)
#transform input to bytes if necessary
bytes_data = None
if isinstance(content, str):
bytes_data=content.encode('utf-8')
elif isinstance(input, Image.Image):
bytes_data.content.tobytes()
else:
raise Exception("Unsupported content type")

with open(task_content_path, 'wb') as file:
file.write(bytes_data)
print(f"Data successfully written to {task_content_path}")
except Exception as exc:
self.P("Error saving job input: {}".format(exc))
result = "Exception saving job input"
Expand Down Expand Up @@ -386,6 +408,14 @@ 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)

def predict_image(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.add(image)
self.P(f"Predict {len(image_data_list)} images")
return self._predict_job('image', images, params)

def predict_json(self, data: dict, params: dict = None):
model = self.get_model('json')
Expand Down

0 comments on commit 964c9ac

Please sign in to comment.