Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable preparing nested OutputSchemas for serialization #1357

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/deepsparse/server/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from http import HTTPStatus
from typing import Any, Dict, List, Optional
from typing import Dict, List, Optional, Union

import numpy
from pydantic import BaseModel
Expand Down Expand Up @@ -75,23 +75,35 @@ def server_logger_from_config(config: ServerConfig) -> BaseLogger:
)


def prep_outputs_for_serialization(pipeline_outputs: Any):
def prep_outputs_for_serialization(
dbogunowicz marked this conversation as resolved.
Show resolved Hide resolved
pipeline_outputs: Union[BaseModel, numpy.ndarray, list]
) -> Union[BaseModel, list]:
"""
Prepares a pipeline output for JSON serialization by converting any numpy array
field to a list. For large numpy arrays, this operation will take a while to run.

:param pipeline_outputs: output data to clean
:return: cleaned pipeline_outputs
:param pipeline_outputs: output data to that is to be processed before
serialisation. Nested objects are supported.
:return: Pipeline_outputs with potential numpy arrays
converted to lists
"""
if isinstance(pipeline_outputs, BaseModel):
for field_name in pipeline_outputs.__fields__.keys():
field_value = getattr(pipeline_outputs, field_name)
if isinstance(field_value, numpy.ndarray):
# numpy arrays aren't JSON serializable
setattr(pipeline_outputs, field_name, field_value.tolist())
if isinstance(field_value, (numpy.ndarray, BaseModel, list)):
setattr(
pipeline_outputs,
field_name,
prep_outputs_for_serialization(field_value),
)

elif isinstance(pipeline_outputs, numpy.ndarray):
pipeline_outputs = pipeline_outputs.tolist()

elif isinstance(pipeline_outputs, list):
for i, value in enumerate(pipeline_outputs):
pipeline_outputs[i] = prep_outputs_for_serialization(value)

return pipeline_outputs


Expand Down
Loading