forked from bentoml/BentoML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
370 lines (338 loc) · 11.9 KB
/
serve.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from __future__ import annotations
import sys
import logging
import click
logger = logging.getLogger(__name__)
DEFAULT_DEV_SERVER_HOST = "127.0.0.1"
def add_serve_command(cli: click.Group) -> None:
from bentoml._internal.log import configure_server_logging
from bentoml._internal.configuration.containers import BentoMLContainer
@cli.command(aliases=["serve-http"])
@click.argument("bento", type=click.STRING, default=".")
@click.option(
"--production",
type=click.BOOL,
help="Run the BentoServer in production mode",
is_flag=True,
default=False,
show_default=True,
)
@click.option(
"-p",
"--port",
type=click.INT,
default=BentoMLContainer.http.port.get(),
help="The port to listen on for the REST api server",
envvar="BENTOML_PORT",
show_default=True,
)
@click.option(
"--host",
type=click.STRING,
default=BentoMLContainer.http.host.get(),
help="The host to bind for the REST api server",
envvar="BENTOML_HOST",
show_default=True,
)
@click.option(
"--api-workers",
type=click.INT,
default=BentoMLContainer.api_server_workers.get(),
help="Specify the number of API server workers to start. Default to number of available CPU cores in production mode",
envvar="BENTOML_API_WORKERS",
show_default=True,
)
@click.option(
"--backlog",
type=click.INT,
default=BentoMLContainer.api_server_config.backlog.get(),
help="The maximum number of pending connections.",
show_default=True,
)
@click.option(
"--reload",
type=click.BOOL,
is_flag=True,
help="Reload Service when code changes detected, this is only available in development mode",
default=False,
show_default=True,
)
@click.option(
"--working-dir",
type=click.Path(),
help="When loading from source code, specify the directory to find the Service instance",
default=".",
show_default=True,
)
@click.option(
"--ssl-certfile",
type=str,
default=None,
help="SSL certificate file",
show_default=True,
)
@click.option(
"--ssl-keyfile",
type=str,
default=None,
help="SSL key file",
show_default=True,
)
@click.option(
"--ssl-keyfile-password",
type=str,
default=None,
help="SSL keyfile password",
show_default=True,
)
@click.option(
"--ssl-version",
type=int,
default=None,
help="SSL version to use (see stdlib 'ssl' module)",
show_default=True,
)
@click.option(
"--ssl-cert-reqs",
type=int,
default=None,
help="Whether client certificate is required (see stdlib 'ssl' module)",
show_default=True,
)
@click.option(
"--ssl-ca-certs",
type=str,
default=None,
help="CA certificates file",
show_default=True,
)
@click.option(
"--ssl-ciphers",
type=str,
default=None,
help="Ciphers to use (see stdlib 'ssl' module)",
show_default=True,
)
def serve( # type: ignore (unused warning)
bento: str,
production: bool,
port: int,
host: str,
api_workers: int | None,
backlog: int,
reload: bool,
working_dir: str,
ssl_certfile: str | None,
ssl_keyfile: str | None,
ssl_keyfile_password: str | None,
ssl_version: int | None,
ssl_cert_reqs: int | None,
ssl_ca_certs: str | None,
ssl_ciphers: str | None,
) -> None:
"""Start a HTTP BentoServer from a given 🍱
\b
BENTO is the serving target, it can be the import as:
- the import path of a 'bentoml.Service' instance
- a tag to a Bento in local Bento store
- a folder containing a valid 'bentofile.yaml' build file with a 'service' field, which provides the import path of a 'bentoml.Service' instance
- a path to a built Bento (for internal & debug use only)
e.g.:
\b
Serve from a bentoml.Service instance source code (for development use only):
'bentoml serve fraud_detector.py:svc'
\b
Serve from a Bento built in local store:
'bentoml serve fraud_detector:4tht2icroji6zput3suqi5nl2'
'bentoml serve fraud_detector:latest'
\b
Serve from a Bento directory:
'bentoml serve ./fraud_detector_bento'
\b
If '--reload' is provided, BentoML will detect code and model store changes during development, and restarts the service automatically.
\b
The '--reload' flag will:
- be default, all file changes under '--working-dir' (default to current directory) will trigger a restart
- when specified, respect 'include' and 'exclude' under 'bentofile.yaml' as well as the '.bentoignore' file in '--working-dir', for code and file changes
- all model store changes will also trigger a restart (new model saved or existing model removed)
"""
configure_server_logging()
if sys.path[0] != working_dir:
sys.path.insert(0, working_dir)
if production:
if reload:
logger.warning(
"'--reload' is not supported with '--production'; ignoring"
)
from bentoml.serve import serve_http_production
serve_http_production(
bento,
working_dir=working_dir,
port=port,
host=host,
backlog=backlog,
api_workers=api_workers,
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
ssl_keyfile_password=ssl_keyfile_password,
ssl_version=ssl_version,
ssl_cert_reqs=ssl_cert_reqs,
ssl_ca_certs=ssl_ca_certs,
ssl_ciphers=ssl_ciphers,
)
else:
from bentoml.serve import serve_http_development
serve_http_development(
bento,
working_dir=working_dir,
port=port,
host=DEFAULT_DEV_SERVER_HOST if not host else host,
reload=reload,
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
ssl_keyfile_password=ssl_keyfile_password,
ssl_version=ssl_version,
ssl_cert_reqs=ssl_cert_reqs,
ssl_ca_certs=ssl_ca_certs,
ssl_ciphers=ssl_ciphers,
)
from bentoml._internal.utils import add_experimental_docstring
@cli.command(name="serve-grpc")
@click.argument("bento", type=click.STRING, default=".")
@click.option(
"--production",
type=click.BOOL,
help="Run the BentoServer in production mode",
is_flag=True,
default=False,
show_default=True,
)
@click.option(
"-p",
"--port",
type=click.INT,
default=BentoMLContainer.grpc.port.get(),
help="The port to listen on for the REST api server",
envvar="BENTOML_PORT",
show_default=True,
)
@click.option(
"--host",
type=click.STRING,
default=BentoMLContainer.grpc.host.get(),
help="The host to bind for the gRPC server",
envvar="BENTOML_HOST",
show_default=True,
)
@click.option(
"--api-workers",
type=click.INT,
default=BentoMLContainer.api_server_workers.get(),
help="Specify the number of API server workers to start. Default to number of available CPU cores in production mode",
envvar="BENTOML_API_WORKERS",
show_default=True,
)
@click.option(
"--reload",
type=click.BOOL,
is_flag=True,
help="Reload Service when code changes detected, this is only available in development mode",
default=False,
show_default=True,
)
@click.option(
"--backlog",
type=click.INT,
default=BentoMLContainer.api_server_config.backlog.get(),
help="The maximum number of pending connections.",
show_default=True,
)
@click.option(
"--working-dir",
type=click.Path(),
help="When loading from source code, specify the directory to find the Service instance",
default=".",
show_default=True,
)
@click.option(
"--enable-reflection",
is_flag=True,
default=BentoMLContainer.grpc.reflection.enabled.get(),
type=click.BOOL,
help="Enable reflection.",
show_default=True,
)
@click.option(
"--max-concurrent-streams",
default=BentoMLContainer.grpc.max_concurrent_streams.get(),
type=click.INT,
help="Maximum number of concurrent incoming streams to allow on a http2 connection.",
show_default=True,
)
@add_experimental_docstring
def serve_grpc( # type: ignore (unused warning)
bento: str,
production: bool,
port: int,
host: str,
api_workers: int | None,
backlog: int,
reload: bool,
working_dir: str,
enable_reflection: bool,
max_concurrent_streams: int | None,
):
"""Start a gRPC BentoServer from a given 🍱
\b
BENTO is the serving target, it can be the import as:
- the import path of a 'bentoml.Service' instance
- a tag to a Bento in local Bento store
- a folder containing a valid 'bentofile.yaml' build file with a 'service' field, which provides the import path of a 'bentoml.Service' instance
- a path to a built Bento (for internal & debug use only)
e.g.:
\b
Serve from a bentoml.Service instance source code (for development use only):
'bentoml serve-grpc fraud_detector.py:svc'
\b
Serve from a Bento built in local store:
'bentoml serve-grpc fraud_detector:4tht2icroji6zput3suqi5nl2'
'bentoml serve-grpc fraud_detector:latest'
\b
Serve from a Bento directory:
'bentoml serve-grpc ./fraud_detector_bento'
If '--reload' is provided, BentoML will detect code and model store changes during development, and restarts the service automatically.
\b
The '--reload' flag will:
- be default, all file changes under '--working-dir' (default to current directory) will trigger a restart
- when specified, respect 'include' and 'exclude' under 'bentofile.yaml' as well as the '.bentoignore' file in '--working-dir', for code and file changes
- all model store changes will also trigger a restart (new model saved or existing model removed)
"""
configure_server_logging()
if production:
if reload:
logger.warning(
"'--reload' is not supported with '--production'; ignoring"
)
from bentoml.serve import serve_grpc_production
serve_grpc_production(
bento,
working_dir=working_dir,
port=port,
host=host,
backlog=backlog,
api_workers=api_workers,
max_concurrent_streams=max_concurrent_streams,
reflection=enable_reflection,
)
else:
from bentoml.serve import serve_grpc_development
serve_grpc_development(
bento,
working_dir=working_dir,
port=port,
backlog=backlog,
reload=reload,
host=DEFAULT_DEV_SERVER_HOST if not host else host,
max_concurrent_streams=max_concurrent_streams,
reflection=enable_reflection,
)