def from_archive(cls, path): from bentoml.archive import load_bentoml_config # TODO: add model.env.verify() to check dependencies and python version etc if cls._bento_archive_path is not None and cls._bento_archive_path != path: raise BentoMLException( "Loaded BentoArchive(from {}) can't be loaded again from a different" "archive path {}".format(cls._bento_archive_path, path)) if is_s3_url(path): temporary_path = tempfile.mkdtemp() download_from_s3(path, temporary_path) # Use loacl temp path for the following loading operations path = temporary_path artifacts_path = path # For pip installed BentoService, artifacts directory is located at # 'package_path/artifacts/', but for loading from BentoArchive, it is # in 'path/{service_name}/artifacts/' if not os.path.isdir(os.path.join(path, 'artifacts')): artifacts_path = os.path.join(path, cls.name()) bentoml_config = load_bentoml_config(path) # TODO: check archive type and allow loading archive only if bentoml_config['service_name'] != cls.name(): raise BentoMLException( 'BentoService name does not match with BentoML Archive in path: {}' .format(path)) artifacts = ArtifactCollection.load(artifacts_path, cls._artifacts_spec) svc = cls(artifacts) return svc
def load_from_dir(cls, path): from bentoml.archive import load_bentoml_config if cls._bento_archive_path is not None and cls._bento_archive_path != path: logger.warning( "Loaded BentoArchive from '%s' can't be loaded again from a different" "path %s", cls._bento_archive_path, path, ) artifacts_path = path # For pip installed BentoService, artifacts directory is located at # 'package_path/artifacts/', but for loading from BentoArchive, it is # in 'path/{service_name}/artifacts/' if not os.path.isdir(os.path.join(path, "artifacts")): artifacts_path = os.path.join(path, cls.name()) bentoml_config = load_bentoml_config(path) if bentoml_config["metadata"]["service_name"] != cls.name(): raise BentoMLException( "BentoService name does not match with BentoArchive in path: {}" .format(path)) if bentoml_config["kind"] != "BentoService": raise BentoMLException( "BentoArchive type '{}' can not be loaded as a BentoService". format(bentoml_config["kind"])) artifacts = ArtifactCollection.load(artifacts_path, cls._artifacts_spec) svc = cls(artifacts) return svc
def _init_artifacts(self, artifacts): if artifacts is None: if self._bento_archive_path: artifacts = ArtifactCollection.load( self._bento_archive_path, self.__class__._artifacts_spec) else: raise BentoMLException( "Must provide artifacts or set cls._bento_archive_path" "before instantiating a BentoService class") if isinstance(artifacts, ArtifactCollection): self._artifacts = artifacts else: self._artifacts = ArtifactCollection() for artifact in artifacts: self._artifacts[artifact.name] = artifact
def __init__(self, artifacts=None, env=None): if artifacts is None: if self._bento_archive_path: artifacts = ArtifactCollection.load(self._bento_archive_path, self.__class__._artifacts_spec) else: raise BentoMLException("Must provide artifacts or set cls._bento_archive_path" "before instantiating a BentoService class") # TODO: validate artifacts arg matches self.__class__._artifacts_spec definition if isinstance(artifacts, ArtifactCollection): self._artifacts = artifacts else: self._artifacts = ArtifactCollection() for artifact in artifacts: self._artifacts[artifact.name] = artifact self._init_env(env) self._config_service_apis() self.name = self.__class__.name()
def from_archive(cls, path): from bentoml.archive import load_bentoml_config if cls._bento_archive_path is not None and cls._bento_archive_path != path: raise BentoMLException( "Loaded BentoArchive(from {}) can't be loaded again from a different" "archive path {}".format(cls._bento_archive_path, path)) if is_s3_url(path): temporary_path = tempfile.mkdtemp() download_from_s3(path, temporary_path) # Use loacl temp path for the following loading operations path = temporary_path artifacts_path = path # For pip installed BentoService, artifacts directory is located at # 'package_path/artifacts/', but for loading from BentoArchive, it is # in 'path/{service_name}/artifacts/' if not os.path.isdir(os.path.join(path, "artifacts")): artifacts_path = os.path.join(path, cls.name()) bentoml_config = load_bentoml_config(path) if bentoml_config["metadata"]["service_name"] != cls.name(): raise BentoMLException( "BentoService name does not match with BentoArchive in path: {}" .format(path)) if bentoml_config["kind"] != "BentoService": raise BentoMLException( "BentoArchive type '{}' can not be loaded as a BentoService". format(bentoml_config["kind"])) artifacts = ArtifactCollection.load(artifacts_path, cls._artifacts_spec) svc = cls(artifacts) return svc
def load(cls, path=None): if cls._bento_module_path is not None: path = cls._bento_module_path artifacts = ArtifactCollection.load(path, cls._artifacts_spec) return cls(artifacts)