Mnist serve
This is an example of a LitServe api for the Mnist LightningModule.
MNISTServeAPI
#
Bases: LitAPI
LitServe API for serving the MNIST model.
Source code in src/serve_apis/mnist_serve.py
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 | |
__init__(model_class, checkpoint_path)
#
Initialize the MNISTServeAPI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
type[LightningModule]
|
The LightningModule class to serve. |
required |
checkpoint_path
|
str
|
The path to the model checkpoint. |
required |
Source code in src/serve_apis/mnist_serve.py
15 16 17 18 19 20 21 22 23 24 | |
decode_request(request)
#
Decode the incoming request and prepare the input for the model.
The request payload is expected to be a [28, 28] nested list of floats in [0, 1]
(e.g. a PIL.Image converted with numpy.asarray(img) / 255).
Source code in src/serve_apis/mnist_serve.py
42 43 44 45 46 47 48 49 50 51 52 | |
encode_response(output)
#
Encode the model's output into a response payload.
Source code in src/serve_apis/mnist_serve.py
64 65 66 | |
predict(x)
#
Run inference using the MNIST model and return the prediction.
x is already batched ([batch, 1, 28, 28]) by LitServe.
Source code in src/serve_apis/mnist_serve.py
54 55 56 57 58 59 60 61 62 | |
setup(device)
#
Setup is called once at startup.
Load the model, set the device, and prepare any other necessary components.
Source code in src/serve_apis/mnist_serve.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |