forked from bentoml/BentoML
/
base.py
155 lines (115 loc) · 4.31 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
from __future__ import annotations
import typing as t
from abc import ABC
from abc import abstractmethod
from typing import TYPE_CHECKING
from ...exceptions import InvalidArgument
if TYPE_CHECKING:
from types import UnionType
from typing_extensions import Self
from starlette.requests import Request
from starlette.responses import Response
from bentoml.grpc.types import ProtoField
from ..types import LazyType
from ..context import InferenceApiContext as Context
from ..service.openapi.specification import Schema
from ..service.openapi.specification import MediaType
from ..service.openapi.specification import Reference
InputType = (
UnionType
| t.Type[t.Any]
| LazyType[t.Any]
| dict[str, t.Type[t.Any] | UnionType | LazyType[t.Any]]
)
OpenAPIResponse = dict[str, str | dict[str, MediaType] | dict[str, t.Any]]
IO_DESCRIPTOR_REGISTRY: dict[str, type[IODescriptor[t.Any]]] = {}
IOType = t.TypeVar("IOType")
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)
class _OpenAPIMeta:
@abstractmethod
def openapi_schema(self) -> Schema | Reference:
raise NotImplementedError
@abstractmethod
def openapi_components(self) -> dict[str, t.Any] | None:
raise NotImplementedError
@abstractmethod
def openapi_example(self) -> t.Any | None:
raise NotImplementedError
@abstractmethod
def openapi_request_body(self) -> dict[str, t.Any]:
raise NotImplementedError
@abstractmethod
def openapi_responses(self) -> dict[str, t.Any]:
raise NotImplementedError
class IODescriptor(ABC, _OpenAPIMeta, 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.
"""
__slots__ = ("_args", "_kwargs", "_proto_fields", "_mime_type", "descriptor_id")
HTTP_METHODS = ["POST"]
descriptor_id: str | None
_mime_type: str
_rpc_content_type: str = "application/grpc"
_proto_fields: tuple[ProtoField]
_sample: IOType | None = None
_args: t.Sequence[t.Any]
_kwargs: dict[str, t.Any]
def __init_subclass__(cls, *, descriptor_id: str | None = None):
if descriptor_id is not None:
if descriptor_id in IO_DESCRIPTOR_REGISTRY:
raise ValueError(
f"Descriptor ID {descriptor_id} already registered to {IO_DESCRIPTOR_REGISTRY[descriptor_id]}."
)
IO_DESCRIPTOR_REGISTRY[descriptor_id] = cls
cls.descriptor_id = descriptor_id
if TYPE_CHECKING:
def __init__(self, **kwargs: t.Any) -> None:
...
def __repr__(self) -> str:
return self.__class__.__qualname__
@property
def sample(self) -> IOType | None:
return self._sample
@sample.setter
def sample(self, value: IOType) -> None:
self._sample = value
@classmethod
def from_sample(cls, sample: IOType | t.Any, **kwargs: t.Any) -> Self:
klass = cls(**kwargs)
klass.sample = klass._from_sample(sample)
return klass
@abstractmethod
def _from_sample(self, sample: t.Any) -> IOType:
raise NotImplementedError
@property
def mime_type(self) -> str:
return self._mime_type
@abstractmethod
def to_spec(self) -> dict[str, t.Any]:
raise NotImplementedError
@classmethod
@abstractmethod
def from_spec(cls, spec: dict[str, t.Any]) -> Self:
raise NotImplementedError
@abstractmethod
def input_type(self) -> InputType:
raise NotImplementedError
@abstractmethod
async def from_http_request(self, request: Request) -> IOType:
raise NotImplementedError
@abstractmethod
async def to_http_response(
self, obj: IOType, ctx: Context | None = None
) -> Response:
raise NotImplementedError
@abstractmethod
async def from_proto(self, field: t.Any) -> IOType:
raise NotImplementedError
@abstractmethod
async def to_proto(self, obj: IOType) -> t.Any:
raise NotImplementedError