Beispiel #1
0
def _save_model_with_loader_module_and_data_path(path,
                                                 loader_module,
                                                 data_path=None,
                                                 code_paths=None,
                                                 conda_env=None,
                                                 mlflow_model=Model()):
    """
    Export model as a generic Python function model.
    :param path: The path to which to save the Python model.
    :param loader_module: The name of the Python module that is used to load the model
                          from ``data_path``. This module must define a method with the prototype
                          ``_load_pyfunc(data_path)``.
    :param data_path: Path to a file or directory containing model data.
    :param code_paths: A list of local filesystem paths to Python file dependencies (or directories
                      containing file dependencies). These files are *prepended* to the system
                      path before the model is loaded.
    :param conda_env: Either a dictionary representation of a Conda environment or the path to a
                      Conda environment yaml file. If provided, this decsribes the environment
                      this model should be run in.
    :return: Model configuration containing model info.
    """
    if os.path.exists(path):
        raise MlflowException(message="Path '{}' already exists".format(path),
                              error_code=RESOURCE_ALREADY_EXISTS)
    os.makedirs(path)

    code = None
    data = None

    if data_path is not None:
        model_file = _copy_file_or_tree(src=data_path,
                                        dst=path,
                                        dst_dir="data")
        data = model_file

    if code_paths is not None:
        for code_path in code_paths:
            _copy_file_or_tree(src=code_path, dst=path, dst_dir="code")
        code = "code"

    conda_env_subpath = "mlflow_env.yml"
    if conda_env is None:
        conda_env = get_default_conda_env()
    elif not isinstance(conda_env, dict):
        with open(conda_env, "r") as f:
            conda_env = yaml.safe_load(f)
    with open(os.path.join(path, conda_env_subpath), "w") as f:
        yaml.safe_dump(conda_env, stream=f, default_flow_style=False)

    mlflow.pyfunc.add_to_model(mlflow_model,
                               loader_module=loader_module,
                               code=code,
                               data=data,
                               env=conda_env_subpath)
    mlflow_model.save(os.path.join(path, 'MLmodel'))
    return mlflow_model
Beispiel #2
0
def save_model(path, loader_module=None, data_path=None, conda_env=None, mlflow_model=None, **kwargs):
    """Save the explanation locally using the MLflow model format.

    This function is necessary for log_explanation to work properly.

    :param path: The destination path for the saved explanation.
    :type path: str
    :param loader_module: The package that will be used to reload a serialized explanation. In this case,
        always interpret_community.mlflow.
    :type loader_module: str
    :param data_path: The path to the serialized explanation files.
    :type data_path: str
    :param conda_env: The path to a YAML file with basic Python environment information.
    :type conda_env: str
    :param mlflow_model: In our case, always None.
    :type mlflow_model: None
    :return: The MLflow model representation of the explanation.
    :rtype: mlflow.models.Model
    """

    try:
        import mlflow
        from mlflow.models import Model
        from mlflow.utils.file_utils import _copy_file_or_tree
        from mlflow.pyfunc.model import get_default_conda_env
    except ImportError as e:
        raise Exception("Could not log_explanation to mlflow. Missing mlflow dependency, "
                        "pip install mlflow to resolve the error: {}.".format(e))

    if os.path.exists(path):
        raise Exception(
            message="Path '{}' already exists".format(path))
    os.makedirs(path)

    if mlflow_model is None:
        mlflow_model = Model()

    data = None
    if data_path is not None:
        model_file = _copy_file_or_tree(src=data_path, dst=path, dst_dir="data")
        data = model_file

    conda_env_subpath = "mlflow_env.yml"
    if conda_env is None:
        conda_env = get_default_conda_env()
    elif not isinstance(conda_env, dict):
        with open(conda_env, "r") as f:
            conda_env = yaml.safe_load(f)
    with open(os.path.join(path, conda_env_subpath), "w") as f:
        yaml.safe_dump(conda_env, stream=f, default_flow_style=False)

    mlflow.pyfunc.add_to_model(
        mlflow_model, loader_module=loader_module, data=data, env=conda_env_subpath, **kwargs)
    mlflow_model.save(os.path.join(path, 'MLmodel'))
    return mlflow_model