def _get_flavor_backend(model_uri, **kwargs): with TempDir() as tmp: local_path = _download_artifact_from_uri(posixpath.join(model_uri, "MLmodel"), output_path=tmp.path()) model = Model.load(local_path) flavor_name, flavor_backend = get_flavor_backend(model, **kwargs) if flavor_backend is None: raise Exception("No suitable flavor backend was found for the model.") _logger.info("Selected backend for flavor '%s'", flavor_name) return flavor_backend
def _get_flavor_backend(model_uri, **kwargs): with TempDir() as tmp: if ModelsArtifactRepository.is_models_uri(model_uri): underlying_model_uri = ModelsArtifactRepository.get_underlying_uri( model_uri) else: underlying_model_uri = model_uri local_path = _download_artifact_from_uri(append_to_uri_path( underlying_model_uri, "MLmodel"), output_path=tmp.path()) model = Model.load(local_path) flavor_name, flavor_backend = get_flavor_backend(model, **kwargs) if flavor_backend is None: raise Exception("No suitable flavor backend was found for the model.") _logger.info("Selected backend for flavor '%s'", flavor_name) return flavor_backend
def build_image_local_from_model_uri(self, model_uri, base_image, mlflow_home=None, **kwargs): """build PythonModel Backed service image from model_uri :param base_image: image base from which build model image :param mlflow_home: mllfow local copy used to startup the model service in container if None install from pip. :param model_uri: directory contains pyfunc model filesystem. <"pyfunc-filename-system" https://mlflow.org/docs/latest/python_api/mlflow.pyfunc.html#pyfunc-filename-system>_ """ with tempfile.TemporaryDirectory() as tmp_dir: if ModelsArtifactRepository.is_models_uri(model_uri): underlying_model_uri = ModelsArtifactRepository.get_underlying_uri( model_uri) else: underlying_model_uri = model_uri local_path = _download_artifact_from_uri(append_to_uri_path( underlying_model_uri, MLMODEL_FILE_NAME), output_path=tmp_dir) model_meta = Model.load(local_path) flavor_name, flavor_backend = get_flavor_backend( model_meta, **kwargs) if flavor_name is None: raise TypeError("no suitable backend was found for the model") if not flavor_backend.can_build_image(): raise AttributeError( 'flavor {} not support build image'.format(flavor_name)) # always intall mlflow for override office mlflow package in container return_code = flavor_backend.build_image(model_uri, self.image_name, install_mlflow=True, mlflow_home=mlflow_home, base_image=base_image) return True if not return_code else False