Skip to content

Commit

Permalink
fix(numpy): handling numpy object and rename to generic types
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Aug 3, 2022
1 parent 091b994 commit d9d2fac
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 546 deletions.
1 change: 1 addition & 0 deletions bentoml/_internal/configuration/containers.py
Expand Up @@ -111,6 +111,7 @@ def _is_ip_address(addr: str) -> bool:
},
"grpc": {
"max_message_length": Or(int, None),
"maximum_concurrent_rpcs": Or(int, None),
},
},
"runners": {
Expand Down
1 change: 1 addition & 0 deletions bentoml/_internal/configuration/default_configuration.yaml
Expand Up @@ -25,6 +25,7 @@ api_server:
access_control_expose_headers: Null
grpc:
max_message_length: Null
maximum_concurrent_rpcs: Null

runners:
batching:
Expand Down
30 changes: 21 additions & 9 deletions bentoml/_internal/io_descriptors/base.py
@@ -1,13 +1,14 @@
from __future__ import annotations

import typing as t
from abc import ABC
from abc import ABCMeta
from abc import abstractmethod
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from types import UnionType

from typing_extensions import Self
from starlette.requests import Request
from starlette.responses import Response

Expand All @@ -32,7 +33,22 @@
_T = t.TypeVar("_T")


class IODescriptor(ABC, t.Generic[IOPyObj]):
class DescriptorMeta(ABCMeta):
def __new__(
cls: type[Self],
name: str,
bases: tuple[type, ...],
namespace: dict[str, t.Any],
*,
proto_fields: list[str] | None = None,
) -> Self:
if not proto_fields:
proto_fields = []
namespace["_proto_fields"] = proto_fields
return super().__new__(cls, name, bases, namespace)


class IODescriptor(t.Generic[IOPyObj], metaclass=DescriptorMeta, proto_fields=None):
"""
IODescriptor describes the input/output data format of an InferenceAPI defined
in a :code:`bentoml.Service`. This is an abstract base class for extending new HTTP
Expand All @@ -41,7 +57,7 @@ class IODescriptor(ABC, t.Generic[IOPyObj]):

HTTP_METHODS = ["POST"]
_init_str: str = ""
_proto_kind: list[str] | None = None
_proto_fields: list[str]

def __new__(cls: t.Type[_T], *args: t.Any, **kwargs: t.Any) -> _T:
self = super().__new__(cls)
Expand All @@ -56,17 +72,13 @@ def __repr__(self) -> str:
return self._init_str

@property
def accepted_proto_kind(self) -> list[str]:
def accepted_proto_fields(self) -> list[str]:
"""
Returns a list of kinds fields that the IODescriptor can accept.
Make sure to keep in sync with bentoml.grpc.v1.Value message.
"""
return self._proto_kind or []

@accepted_proto_kind.setter
def accepted_proto_kind(self, value: list[str]):
self._proto_kind = value
return self._proto_fields

@abstractmethod
def input_type(self) -> InputType:
Expand Down
2 changes: 1 addition & 1 deletion bentoml/_internal/io_descriptors/file.py
Expand Up @@ -24,7 +24,7 @@
FileType: t.TypeAlias = t.Union[io.IOBase, t.IO[bytes], FileLike[bytes]]


class File(IODescriptor[FileType]):
class File(IODescriptor[FileType], proto_fields=["raw_value"]):
"""
:code:`File` defines API specification for the inputs/outputs of a Service, where either
inputs will be converted to or outputs will be converted from file-like objects as
Expand Down
2 changes: 1 addition & 1 deletion bentoml/_internal/io_descriptors/image.py
Expand Up @@ -47,7 +47,7 @@
DEFAULT_PIL_MODE = "RGB"


class Image(IODescriptor[ImageType]):
class Image(IODescriptor[ImageType], proto_fields=["raw_value"]):
"""
:code:`Image` defines API specification for the inputs/outputs of a Service, where either
inputs will be converted to or outputs will be converted from images as specified
Expand Down
2 changes: 1 addition & 1 deletion bentoml/_internal/io_descriptors/json.py
Expand Up @@ -67,7 +67,7 @@ def default(self, o: _SerializableObj) -> t.Any:
return super().default(o)


class JSON(IODescriptor[JSONType]):
class JSON(IODescriptor[JSONType], proto_fields=["map_value", "raw_value"]):
"""
:code:`JSON` defines API specification for the inputs/outputs of a Service, where either
inputs will be converted to or outputs will be converted from a JSON representation
Expand Down

0 comments on commit d9d2fac

Please sign in to comment.