Skip to content

Commit

Permalink
feat: grpc servicer implementation per version (#3316)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarnphm committed Dec 7, 2022
1 parent 4fdc981 commit eb3df53
Show file tree
Hide file tree
Showing 22 changed files with 589 additions and 450 deletions.
7 changes: 4 additions & 3 deletions src/bentoml/_internal/bento/build_dev_bentoml_whl.py
Expand Up @@ -7,6 +7,7 @@
from ..utils.pkg import source_locations
from ...exceptions import BentoMLException
from ...exceptions import MissingDependencyException
from ...grpc.utils import LATEST_PROTOCOL_VERSION
from ..configuration import is_pypi_installed_bentoml

logger = logging.getLogger(__name__)
Expand All @@ -15,7 +16,7 @@


def build_bentoml_editable_wheel(
target_path: str, *, _internal_stubs_version: str = "v1"
target_path: str, *, _internal_protocol_version: str = LATEST_PROTOCOL_VERSION
) -> None:
"""
This is for BentoML developers to create Bentos that contains the local bentoml
Expand Down Expand Up @@ -52,10 +53,10 @@ def build_bentoml_editable_wheel(
bentoml_path = Path(module_location)

if not Path(
module_location, "grpc", _internal_stubs_version, "service_pb2.py"
module_location, "grpc", _internal_protocol_version, "service_pb2.py"
).exists():
raise ModuleNotFoundError(
f"Generated stubs for version {_internal_stubs_version} are missing. Make sure to run '{bentoml_path.as_posix()}/scripts/generate_grpc_stubs.sh {_internal_stubs_version}' beforehand to generate gRPC stubs."
f"Generated stubs for version {_internal_protocol_version} are missing. Make sure to run '{bentoml_path.as_posix()}/scripts/generate_grpc_stubs.sh {_internal_protocol_version}' beforehand to generate gRPC stubs."
) from None

# location to pyproject.toml
Expand Down
2 changes: 1 addition & 1 deletion src/bentoml/_internal/io_descriptors/base.py
Expand Up @@ -36,7 +36,7 @@
IOType = t.TypeVar("IOType")


def from_spec(spec: dict[str, str]) -> IODescriptor[t.Any]:
def from_spec(spec: dict[str, t.Any]) -> IODescriptor[t.Any]:
if "id" not in spec:
raise InvalidArgument(f"IO descriptor spec ({spec}) missing ID.")
return IO_DESCRIPTOR_REGISTRY[spec["id"]].from_spec(spec)
Expand Down
41 changes: 26 additions & 15 deletions src/bentoml/_internal/io_descriptors/json.py
Expand Up @@ -30,6 +30,7 @@

import pydantic
import pydantic.schema as schema
from google.protobuf import message as _message
from google.protobuf import struct_pb2
from typing_extensions import Self

Expand Down Expand Up @@ -392,19 +393,29 @@ async def to_proto(self, obj: JSONType) -> struct_pb2.Value:
if LazyType["pydantic.BaseModel"]("pydantic.BaseModel").isinstance(obj):
obj = obj.dict()
msg = struct_pb2.Value()
# To handle None cases.
if obj is not None:
from google.protobuf.json_format import ParseDict

if isinstance(obj, (dict, str, list, float, int, bool)):
# ParseDict handles google.protobuf.Struct type
# directly if given object has a supported type
ParseDict(obj, msg)
else:
# If given object doesn't have a supported type, we will
# use given JSON encoder to convert it to dictionary
# and then parse it to google.protobuf.Struct.
# Note that if a custom JSON encoder is used, it mustn't
# take any arguments.
ParseDict(self._json_encoder().default(obj), msg)
return parse_dict_to_proto(obj, msg, json_encoder=self._json_encoder)


def parse_dict_to_proto(
obj: JSONType,
msg: _message.Message,
json_encoder: type[json.JSONEncoder] = DefaultJsonEncoder,
) -> t.Any:
if obj is None:
# this function is an identity op for the msg if obj is None.
return msg

from google.protobuf.json_format import ParseDict

if isinstance(obj, (dict, str, list, float, int, bool)):
# ParseDict handles google.protobuf.Struct type
# directly if given object has a supported type
ParseDict(obj, msg)
else:
# If given object doesn't have a supported type, we will
# use given JSON encoder to convert it to dictionary
# and then parse it to google.protobuf.Struct.
# Note that if a custom JSON encoder is used, it mustn't
# take any arguments.
ParseDict(json_encoder().default(obj), msg)
return msg
4 changes: 0 additions & 4 deletions src/bentoml/_internal/server/grpc/__init__.py
@@ -1,4 +0,0 @@
from .server import Server
from .servicer import Servicer

__all__ = ["Server", "Servicer"]
242 changes: 0 additions & 242 deletions src/bentoml/_internal/server/grpc/server.py

This file was deleted.

Empty file.

0 comments on commit eb3df53

Please sign in to comment.