Skip to content

Commit

Permalink
Move cloud event decode logic from preprocess to decode method (kubef…
Browse files Browse the repository at this point in the history
…low#2881)

* Move cloud event decode logic from preprocess to decode method

Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>

* Fix parameters argument default value

Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>

* Resolve comments

Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>

* Use importlib instead of deprecated pkg_resources

Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>

* Remove unused parameter

Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>

---------

Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>
  • Loading branch information
sivanantha321 authored May 13, 2023
1 parent 8618d32 commit b16a393
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 102 deletions.
1 change: 1 addition & 0 deletions python/aiffairness/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 54 additions & 41 deletions python/alibiexplainer/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions python/artexplainer/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions python/custom_model/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions python/custom_transformer/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 1 addition & 21 deletions python/kserve/kserve/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from .protocol.grpc.grpc_predict_v2_pb2 import ModelInferRequest, ModelInferResponse

from .errors import InvalidInput
from .utils.utils import is_structured_cloudevent

PREDICTOR_URL_FORMAT = "http://{0}/v1/models/{1}:predict"
EXPLAINER_URL_FORMAT = "http://{0}/v1/models/{1}:explain"
Expand Down Expand Up @@ -200,27 +199,8 @@ async def preprocess(self, payload: Union[Dict, CloudEvent, InferRequest],
Returns:
Dict|InferRequest: Transformed inputs to ``predict`` handler or return InferRequest for predictor call.
"""
response = payload

if isinstance(payload, CloudEvent):
response = payload.data
# Try to decode and parse JSON UTF-8 if possible, otherwise
# just pass the CloudEvent data on to the predict function.
# This is for the cases that CloudEvent encoding is protobuf, avro etc.
try:
response = orjson.loads(response.decode('UTF-8'))
except (orjson.JSONDecodeError, UnicodeDecodeError) as e:
# If decoding or parsing failed, check if it was supposed to be JSON UTF-8
if "content-type" in payload._attributes and \
(payload._attributes["content-type"] == "application/cloudevents+json" or
payload._attributes["content-type"] == "application/json"):
raise InvalidInput(f"Failed to decode or parse binary json cloudevent: {e}")

elif isinstance(payload, dict):
if is_structured_cloudevent(payload):
response = payload["data"]

return response
return payload

def postprocess(self, response: Union[Dict, InferResponse, ModelInferResponse], headers: Dict[str, str] = None) \
-> Union[Dict, ModelInferResponse]:
Expand Down
Loading

0 comments on commit b16a393

Please sign in to comment.