/
base.py
175 lines (143 loc) · 4.54 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import annotations
import typing as t
from abc import ABC
from abc import abstractmethod
from typing import overload
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from types import UnionType
from google.protobuf import message
from google.protobuf import struct_pb2
from google.protobuf import wrappers_pb2
from typing_extensions import Self
from starlette.requests import Request
from starlette.responses import Response
from google.protobuf.internal.containers import MessageMap
from bentoml.grpc.types import ProtoField
from bentoml.grpc.v1alpha1 import service_pb2 as pb
from ..types import LazyType
from ..context import InferenceApiContext as Context
from ..service.openapi.specification import Schema
from ..service.openapi.specification import Response as OpenAPIResponse
from ..service.openapi.specification import Reference
from ..service.openapi.specification import RequestBody
InputType = (
UnionType
| t.Type[t.Any]
| LazyType[t.Any]
| dict[str, t.Type[t.Any] | UnionType | LazyType[t.Any]]
)
IOType = t.TypeVar("IOType")
class IODescriptor(ABC, t.Generic[IOType]):
"""
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
endpoint IO descriptor types in BentoServer.
"""
HTTP_METHODS = ["POST"]
_mime_type: str
_rpc_content_type: str
_proto_field: str
def __new__( # pylint: disable=unused-argument
cls: t.Type[Self],
*args: t.Any,
**kwargs: t.Any,
) -> Self:
self = super().__new__(cls)
# default grpc content type is application/grpc
self._rpc_content_type = "application/grpc"
return self
@property
def accepted_proto_fields(self) -> ProtoField:
"""
Returns a proto field that the IODescriptor can accept.
Note that all proto field will also accept _internal_bytes_contents
"""
return t.cast("ProtoField", self._proto_field)
def __repr__(self) -> str:
return self.__class__.__qualname__
@abstractmethod
def input_type(self) -> InputType:
...
@abstractmethod
def openapi_schema(self) -> Schema | Reference:
raise NotImplementedError
@abstractmethod
def openapi_components(self) -> dict[str, t.Any] | None:
raise NotImplementedError
@abstractmethod
def openapi_request_body(self) -> RequestBody:
raise NotImplementedError
@abstractmethod
def openapi_responses(self) -> OpenAPIResponse:
raise NotImplementedError
@abstractmethod
async def from_http_request(self, request: Request) -> IOType:
...
@abstractmethod
async def to_http_response(
self, obj: IOType, ctx: Context | None = None
) -> Response:
...
@overload
@abstractmethod
async def from_proto(
self,
field: wrappers_pb2.StringValue | pb.Part | bytes,
*,
_use_internal_bytes_contents: bool,
) -> IOType:
...
@overload
@abstractmethod
async def from_proto(
self,
field: struct_pb2.Value | pb.Part | bytes,
*,
_use_internal_bytes_contents: bool,
) -> IOType:
...
@overload
@abstractmethod
async def from_proto(
self, field: MessageMap[str, pb.Part], *, _use_internal_bytes_contents: bool
) -> IOType:
...
@overload
@abstractmethod
async def from_proto(
self, field: pb.NDArray | pb.Part | bytes, *, _use_internal_bytes_contents: bool
) -> IOType:
...
@overload
@abstractmethod
async def from_proto(
self, field: pb.File | pb.Part | bytes, *, _use_internal_bytes_contents: bool
) -> IOType:
...
@overload
@abstractmethod
async def from_proto(
self,
field: pb.DataFrame | pb.Part | bytes,
*,
_use_internal_bytes_contents: bool,
) -> IOType:
...
@overload
@abstractmethod
async def from_proto(
self, field: pb.Series | pb.Part | bytes, *, _use_internal_bytes_contents: bool
) -> IOType:
...
@abstractmethod
async def from_proto(
self,
field: message.Message | bytes | MessageMap[str, pb.Part],
*,
_use_internal_bytes_contents: bool = False,
) -> IOType:
...
@abstractmethod
async def to_proto(self, obj: IOType) -> MessageMap[str, pb.Part] | message.Message:
...