Skip to main content

kserve-helper

kserve-helper is a toolkit for building docker images for ML models built on KServe. It supports model input validation, uploading generated files to S3 or GCS, building model images, etc. Here are some basic examples.

Implement a Model Class for Serving

To build a docker image for serving, we only need to implement a model class with load and predict methods:

class Model:

    def load(self):
        # Load the model
        pass

    def predict(
            self,
            image: str = Input(
                description="Base64 encoded image",
                default=""
            ),
            radius: float = Input(
                description="Standard deviation of the Gaussian kernel",
                default=2
            )
    ) -> Path:
        if image == "":
            raise ValueError("The input image is not set")
        im_binary = base64.b64decode(image)
        input_image = Image.open(io.BytesIO(im_binary))
        output_image = input_image.filter(ImageFilter.GaussianBlur(radius))
        output_path = KServeModel.generate_filepath("image.jpg")
        output_image.save(output_path)
        return Path(output_path)

The load function will be called during the model initialization step, which will be only called once. The predict function will be called for each request. The input parameter information is specified by the Input class. This Input class allows us to set parameter descriptions, default values and constraints (e.g., 0 <= input value <= 1).

The output typing of the predict function is important. If the output type is Path or List[Path], the webhook for uploading files will be called after predict is finished. In this case, the input request should also contain an additional key "upload_webhook" to specify the webhook server address (an example). If the output type is not Path, the results will be returned directly without calling the webhook.

If streaming outputs are required, the output of predict should be an iterator:

class Model:

    def load(self):
        pass

    def predict(
            self,
            repeat: int = Input(
                description="The number of repeats",
                default=5
            )
    ):
        def _generator():
            for i in range(repeat):
                yield "Hello World!"
                time.sleep(1)

        return KServeModel.wrap_generator(_generator)

Note that we combine streaming and non-streaming APIs together as predict when using KServe >= 0.13.1. For KServe <= 0.10.2, we seperate streaming and non-streaming APIs, i.e.,

class Model:

    def load(self):
        pass

    def predict(
            self,
            repeat: int = Input(
                description="The number of repeats",
                default=5
            )
    ):
        time.sleep(repeat)
        return {"output": " ".join(["Hello World!"] * repeat)}

    def generate(
            self,
            repeat: int = Input(
                description="The number of repeats",
                default=5
            )
    ):
        def _generator():
            for i in range(repeat):
                yield "Hello World!"
                time.sleep(1)

        return KServeModel.wrap_generator(_generator)

Write a Config for Building Docker Image

To build the corresponding docker image for serving, we only need to write a config file:

build:
  python_version: "3.10"
  cuda: "11.7"

  # a list of commands (optional)
  commands:
    - "apt install -y software-properties-common"

  # a list of ubuntu apt packages to install (optional)
  system_packages:
    - "git"
    - "python3-opencv"

  # choose requirements.txt (optional)
  python_requirements:
    - "requirements.txt"

  # a list of packages in the format <package-name>==<version>
  python_packages:
    - "kservehelper>=1.1.0"
    - "salesforce_lavis-1.1.0-py3-none-any.whl"
    - "git+https://github.com/huggingface/diffusers.git"
    - "controlnet_aux==0.0.7"
    - "opencv-python==4.8.0.74"
    - "Pillow"
    - "tensorboard"
    - "mediapipe"
    - "accelerate"
    - "bitsandbytes"

# The name given to built Docker images
image: "<DOCKER-IMAGE-NAME:TAG>"

# model.py defines the entrypoint
entrypoint: "model.py"

In the config file, we can choose python version, cuda version (and whether to use NGC images), system packages and python packages. We need to set the docker image name and the entrypoint. The entrypoint is just the file that defines the model class above.

To build the docker image, we can simply run in the folder containing the config file:

kservehelper build .

To push the docker image, run this command:

kservehelper push .

For more details, please check the implementations in the repo.

Metadata

Release files for kservehelper 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kservehelper 2.0.0
File Size Uploaded Provenance Actions
kservehelper-2.0.0.tar.gz 29.4 kB Without provenance Details for kservehelper-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kservehelper 2.0.0
File Interpreter ABI Platform Actions
kservehelper-2.0.0-py3-none-any.whl Python 3 none any Details for kservehelper-2.0.0-py3-none-any.whl

Total release size: 66.2 kB

Release files / kservehelper-2.0.0.tar.gz

Download URL kservehelper-2.0.0.tar.gz
Size 29.4 kB
Tags Source
SHA-256 checksum
How to use checksums
57a4888d0c92457cf7f95068b96dcfd902ee970f7fd8adf3d81a0725a58bdad0
BLAKE2b-256 checksum
How to use checksums
eac53cbd7ad96737feffa13830c69cce35220688845968ed192cf83812b6a4ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / kservehelper-2.0.0-py3-none-any.whl

Download URL kservehelper-2.0.0-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a757e789499cc8e2e46bc4e2a724031b0cf9b0dc04ac4653cb699d0c313ccc49
BLAKE2b-256 checksum
How to use checksums
58ebcbf67ac2bc06358922b2f2f083f37cedadd66dd256c3aaf646f27aae4b72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Security for kservehelper 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page