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

feat(gRPC): client implementation #3280

Merged
merged 21 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
60 changes: 37 additions & 23 deletions grpc-client/python/client.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
from __future__ import annotations

import asyncio
import logging

import numpy as np

import grpc
import bentoml

from bentoml.grpc.utils import import_generated_stubs

pb, services = import_generated_stubs()
async def async_run(client: bentoml.client.Client):

res = await client.async_classify(np.array([[5.9, 3, 5.1, 1.8]]))
logger.info("Result from 'client.async_classify':\n%s", res)
res = await client.async_call("classify", np.array([[5.9, 3, 5.1, 1.8]]))
logger.info("Result from 'client.async_call':\n%s", res)

async def run():
async with grpc.aio.insecure_channel("localhost:3000") as channel:
stub = services.BentoServiceStub(channel)
req = await stub.Call(
request=pb.Request(
api_name="classify",
ndarray=pb.NDArray(
dtype=pb.NDArray.DTYPE_FLOAT,
shape=(1, 4),
float_values=[5.9, 3, 5.1, 1.8],
),
)
)
print(req)

def run(client: bentoml.client.Client):
res = client.classify(np.array([[5.9, 3, 5.1, 1.8]]))
logger.info("Result from 'client.classify':\n%s", res)
res = client.call("classify", np.array([[5.9, 3, 5.1, 1.8]]))
logger.info("Result from 'client.call(bentoml_api_name='classify')':\n%s", res)


if __name__ == "__main__":
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(run())
finally:
loop.close()
assert loop.is_closed()
import argparse

logger = logging.getLogger(__name__)

ch = logging.StreamHandler()
formatter = logging.Formatter("%(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(logging.DEBUG)

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--sync", action="store_true", default=False)
args = parser.parse_args()

c = bentoml.client.Client.from_url("localhost:3000")

if args.sync:
run(c)
else:
asyncio.run(async_run(c))
136 changes: 136 additions & 0 deletions src/bentoml/_internal/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from __future__ import annotations

import typing as t
import asyncio
import logging
import functools
from abc import ABC
from abc import abstractmethod
from typing import TYPE_CHECKING
from http.client import BadStatusLine
from urllib.parse import urlparse

from ...exceptions import BentoMLException
from ..service.inference_api import InferenceAPI

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
from .grpc import GrpcClient
from .http import HTTPClient
from ..service import Service


class Client(ABC):
server_url: str
_svc: Service

def __init__(self, svc: Service, server_url: str):
self._svc = svc
self.server_url = server_url

if svc is not None and len(svc.apis) == 0:
raise BentoMLException("No APIs were found when constructing client.")

for name, api in self._svc.apis.items():
if not hasattr(self, name):
setattr(
self, name, functools.partial(self._sync_call, _bentoml_api=api)
)

if not hasattr(self, f"async_{name}"):
setattr(
self,
f"async_{name}",
functools.partial(self._call, _bentoml_api=api),
)

def call(self, bentoml_api_name: str, inp: t.Any = None, **kwargs: t.Any) -> t.Any:
return self._sync_call(
inp, _bentoml_api=self._svc.apis[bentoml_api_name], **kwargs
)

async def async_call(
self, bentoml_api_name: str, inp: t.Any = None, **kwargs: t.Any
) -> t.Any:
return await self._call(
inp, _bentoml_api=self._svc.apis[bentoml_api_name], **kwargs
)

@abstractmethod
def wait_until_server_ready(
self,
*,
server_url: str | None = None,
timeout: int = 30,
**kwargs: t.Any,
) -> None:
raise NotImplementedError

@t.overload
@classmethod
@abstractmethod
def from_url(
cls, server_url: str, *, kind: None | t.Literal["auto"] = ...
) -> GrpcClient | HTTPClient:
...

@t.overload
@classmethod
@abstractmethod
def from_url(cls, server_url: str, *, kind: t.Literal["http"] = ...) -> HTTPClient:
...

@t.overload
@classmethod
@abstractmethod
def from_url(cls, server_url: str, *, kind: t.Literal["grpc"] = ...) -> GrpcClient:
...

@classmethod
@abstractmethod
def from_url(
cls, server_url: str, *, kind: str | None = None, **kwargs: t.Any
) -> Client:
url_parsed = urlparse(server_url)
if url_parsed.scheme == "http":
kind = "http"
elif url_parsed.scheme == "grpc":
kind = "grpc"

if kind is None or kind == "auto":
try:
from .http import HTTPClient

return HTTPClient.from_url(server_url, **kwargs)
except BadStatusLine:
from .grpc import GrpcClient

return GrpcClient.from_url(server_url, **kwargs)
except Exception as e: # pylint: disable=broad-except
raise BentoMLException(
f"Failed to create a BentoML client from given URL '{server_url}': {e} ({e.__class__.__name__})"
) from e
elif kind == "http":
from .http import HTTPClient

return HTTPClient.from_url(server_url, **kwargs)
elif kind == "grpc":
from .grpc import GrpcClient

return GrpcClient.from_url(server_url, **kwargs)
else:
raise BentoMLException(
"Invalid client kind. Must be one of ['http', 'grpc', 'auto']"
aarnphm marked this conversation as resolved.
Show resolved Hide resolved
)

def _sync_call(
self, inp: t.Any = None, *, _bentoml_api: InferenceAPI, **kwargs: t.Any
):
return asyncio.run(self._call(inp, _bentoml_api=_bentoml_api, **kwargs))

@abstractmethod
async def _call(
self, inp: t.Any = None, *, _bentoml_api: InferenceAPI, **kwargs: t.Any
) -> t.Any:
raise NotImplementedError
Loading