Exemplo n.º 1
0
def load_model(model_path: str,
               suppress_warnings: bool = False) -> PyFuncModel:
    """Load a model that has python_function flavor.

    Parameters
    ----------
    model_path : str
        Filepath of the model directory.
    suppress_warnings : bool, optional
        If Fatal, non-fatal warning messages associated with the model loading process
        will be emitted, by default True

    Returns
    -------
    PyFuncModel
        A python_function model.

    Raises
    ------
    ClearboxWrapperException
        If the model does not have the python_function flavor.
    """
    mlmodel = Model.load(os.path.join(model_path, MLMODEL_FILE_NAME))
    pyfunc_flavor_configuration = mlmodel.flavors.get(FLAVOR_NAME)

    if pyfunc_flavor_configuration is None:
        raise ClearboxWrapperException(
            'Model does not have the "{flavor_name}" flavor'.format(
                flavor_name=FLAVOR_NAME))

    model_python_version = pyfunc_flavor_configuration.get(PY_VERSION)

    if not suppress_warnings:
        _warn_potentially_incompatible_py_version_if_necessary(
            model_py_version=model_python_version)

    if CODE in pyfunc_flavor_configuration and pyfunc_flavor_configuration[
            CODE]:
        code_path = os.path.join(model_path, pyfunc_flavor_configuration[CODE])
        _add_code_to_system_path(code_path=code_path)

    data_path = (os.path.join(model_path, pyfunc_flavor_configuration[DATA]) if
                 (DATA in pyfunc_flavor_configuration) else model_path)

    model_implementation = importlib.import_module(
        pyfunc_flavor_configuration[MAIN])._load_pyfunc(data_path)

    return PyFuncModel(model_meta=mlmodel, model_impl=model_implementation)
Exemplo n.º 2
0
def _get_flavor_configuration(model_path: str, flavor_name: str) -> Dict:
    """Get the configuration for a specified flavor of a model.

    Parameters
    ----------
    model_path : str
        Path to the model directory.
    flavor_name : str
        Name of the flavor configuration to load.

    Returns
    -------
    Dict
        Flavor configuration as a dictionary.

    Raises
    ------
    ClearboxWrapperException
        If it couldn't find a MLmodel file or if the model doesn't contain
        the specified flavor.
    """
    mlmodel_path = os.path.join(model_path, MLMODEL_FILE_NAME)
    if not os.path.exists(mlmodel_path):
        raise ClearboxWrapperException(
            'Could not find an "{}" configuration file at "{}"'.format(
                MLMODEL_FILE_NAME, model_path
            )
        )

    mlmodel = Model.load(mlmodel_path)
    if flavor_name not in mlmodel.flavors:
        raise ClearboxWrapperException(
            'Model does not have the "{}" flavor'.format(flavor_name)
        )
    flavor_configuration_dict = mlmodel.flavors[flavor_name]
    return flavor_configuration_dict
Exemplo n.º 3
0
def load_model(model_path: str,
               suppress_warnings: bool = False) -> WrapperModel:
    """Load a model that has python_function flavor.

    Parameters
    ----------
    model_path : str
        Filepath of the model directory.
    suppress_warnings : bool, optional
        If Fatal, non-fatal warning messages associated with the model loading process
        will be emitted, by default True

    Returns
    -------
    PyFuncModel
        A python_function model.

    Raises
    ------
    ClearboxWrapperException
        If the model does not have the python_function flavor.
    """
    preprocessing = None
    data_preparation = None

    mlmodel = Model.load(os.path.join(model_path, MLMODEL_FILE_NAME))
    clearbox_flavor_configuration = mlmodel.flavors.get(FLAVOR_NAME)

    if clearbox_flavor_configuration is None:
        raise ClearboxWrapperException(
            'Model does not have the "{flavor_name}" flavor'.format(
                flavor_name=FLAVOR_NAME))

    model_python_version = clearbox_flavor_configuration.get(PY_VERSION)

    if not suppress_warnings:
        _warn_potentially_incompatible_py_version_if_necessary(
            model_py_version=model_python_version)

    data_path = (os.path.join(model_path, clearbox_flavor_configuration[DATA])
                 if (DATA in clearbox_flavor_configuration) else model_path)

    model_implementation = importlib.import_module(
        clearbox_flavor_configuration[MAIN])._load_clearbox(data_path)

    if PREPROCESSING in clearbox_flavor_configuration:
        preprocessing_path = os.path.join(
            model_path, clearbox_flavor_configuration[PREPROCESSING])
        preprocessing = load_serialized_preprocessing(preprocessing_path)

    if DATA_PREPARATION in clearbox_flavor_configuration:
        data_preparation_path = os.path.join(
            model_path, clearbox_flavor_configuration[DATA_PREPARATION])
        data_preparation = load_serialized_data_preparation(
            data_preparation_path)

    loaded_model = WrapperModel(
        model_meta=mlmodel,
        model_impl=model_implementation,
        preprocessing=preprocessing,
        data_preparation=data_preparation,
    )

    return loaded_model