Skip to main content

pynavio

pynavio is a publicly available Python package that helps automate the wrapping and simplifies the handling of navio models.

pynavio is available on PyPI.

Features

  • pynavio.Client is a navio API client that enables users to upload models and data, deploy and retrain models etc.
  • pynavio.mlflow.to_navio function calls the mlflow.pyfunc.save_model function, saving a model zip file as required by navio.
    • it enables inferring the conda environment (with pip requirements) and adding extra pip dependencies to the inferred environment
    • it enables adding sys dependencies to the navio model
    • it also validates the models with pynavio.mlflow.ModelValidator by default
  • pynavio.mlflow.ModelValidator is a class that validates the model (prediction/example request/MLmodel metadata schema checks, warnings related to nested types/big model sizes)
  • pynavio.infer_external_dependencies is a helper function that infers the external dependencies based on the file path. For its limitations please refer to its doc string.
  • pynavio.infer_imported_code_path is a helper function that infers the imported code paths based on the file path and the root path. For its limitations please refer to its doc string.
  • pynavio.make_example_request generates a request schema for a navio model from data.

Metadata

To automate the generation of metadata field values for the MLmodel file, use pynavio.mlflow.to_navio setting the corresponding keywords to their corresponding values: example_request, explanations, dataset and oodd, e.g.:

to_navio(...,
example_request=example_request,
explanations='disabled',
dataset=dataset,
oodd='disabled')

Saving a model

pynavio.mlflow.to_navio - this function calls the mlflow.pyfunc.save_model function, saving a model zip file following specifications as required by navio.

Infer Dependencies

pynavio.infer_external_dependencies is a helper function that infers the external dependencies based on the file path, automating the step of managing the model's dependencies to some extent. For its limitations please refer to its doc string. The inferred dependencies can be provided to the pip_packages argument of pynavio.mlflow.to_navio:

to_navio(...,
pip_packages=infer_external_dependencies(__file__)
)

Code Path

pynavio.infer_imported_code_path is a helper function that infers the imported code paths based on the file path and the root path. For its limitations please refer to its doc string.

This function needs to be called from the script that imports the model source code (with the to_navio call), so that the code paths are inferred relative to the script. The derived code path can be then passed to the model setup function.

# model.py
...
def setup(code_path):
...
to_navio(...,
code_path=code_path
)

# main_script.py
from pynavio import infer_imported_code_path

import model

root_path = '.' # the root directory of your model code
code_path = infer_imported_code_path(__file__, root_path)
model.setup(code_path)

Request Schema

pynavio.make_example_request generates a request schema for a navio model from data.

example_request = make_example_request(
df.to_dict(orient='records')[0], 'target')
to_navio(...,
example_request=example_request)

In order to automate the inclusion of a model's dataset into the MLflow model archive, the dataset keyword of pynavio.mlflow.to_navio needs to be set:

dataset = dict(name='data', path=f'{tmp_dir}/data.csv')
to_navio(...,
dataset=dataset)