Skip to content

utils


Helper functions

get_single_model_config(name, base_path, versions, model_platform='tensorflow')

Generate string block for creating serving config file

Parameters:

Name Type Description Default
name

Name of the model. Will serve as an endpoint address for the servable

required
base_path

path to the folder containing the models. Should contain one or more folders with model, each with an integer name (1,3,121541). When serving with default config, the model inside the folder with the biggest number will be served

required
versions

version names. Will be used to generate version policy

required
model_platform

required field in config. Can be only tensorflow for now

'tensorflow'

Returns:

Name Type Description
out str

config in protostyle syntax

Source code in conftrainer/modifications/utils.py
def get_single_model_config(name, base_path, versions, model_platform="tensorflow"):
    """
    Generate string block for creating serving config file

    Parameters
    ----------
    name: str
        Name of the model. Will serve as an endpoint address for the servable
    base_path: str
        path to the folder containing the models. Should contain one or more folders with model,
        each with an integer name (1,3,121541). When serving with default config, the model inside
        the folder with the biggest number will be served
    versions: List[int]
        version names. Will be used to generate version policy
    model_platform: str
        required field in config. Can be only tensorflow for now

    Returns
    -------
    out: str
        config in protostyle syntax
    """
    version_str = ',\n'.join([f"versions: {version}" for version in versions])
    version_policy = f"specific{{ \n {version_str} \n }}"
    return f'config {{ \n name: "{name}"\n base_path: "/{base_path}"\n model_platform: ' \
           f'"{model_platform}"\n model_version_policy: {{\n{version_policy} \n }} \n}}\n'

generate_serving_config(param_list, config_save_path=None)

Generate a config file for serving tensorflow models

Parameters:

Name Type Description Default
param_list list of tuple

parameters for each network that should be added to config file

required
config_save_path str

path to save the generated config file. Extension should be .config or .conf

None

Returns:

Name Type Description
out str

config for serving the network

Source code in conftrainer/modifications/utils.py
def generate_serving_config(param_list, config_save_path=None):
    """
    Generate a config file for serving tensorflow models

    Parameters
    ----------
    param_list : list of tuple
        parameters for each network that should be added to config file
    config_save_path : str
        path to save the generated config file. Extension should be .config or .conf

    Returns
    -------
    out : str
        config for serving the network
    """
    serve_config = "model_config_list {\n"
    for name, path, versions in param_list:
        serve_config += get_single_model_config(name=name,
                                                base_path=path,
                                                versions=versions)
    serve_config += "}"
    if config_save_path:
        with open(config_save_path, "w+") as file:
            file.write(serve_config)
    return serve_config