[docs]classErsiliaBase(object):""" Base class of Ersilia. This class is used as a configuration for many of the classes of the package. """def__init__(self,config_json=None,credentials_json=None):self.eos_dir=EOSself.config_json=config_jsonself.credentials_json=credentials_jsonself.cfg=Config(json_file=config_json)self.cred=Credentials(json_file=credentials_json)self._tmp_dir=self._abs_path(os.path.join(EOS,self.cfg.LOCAL.TMP))ifnotos.path.exists(self._tmp_dir):os.makedirs(self._tmp_dir,exist_ok=True)self._dest_dir=self._abs_path(os.path.join(EOS,self.cfg.LOCAL.DEST))ifnotos.path.exists(self._dest_dir):os.makedirs(self._dest_dir,exist_ok=True)self._bentoml_dir=os.path.join(self._abs_path(os.path.join(Path.home(),"bentoml")),"repository")self._bundles_dir=os.path.join(self.eos_dir,"repository")ifnotos.path.exists(self._bundles_dir):os.makedirs(self._bundles_dir,exist_ok=True)self.logger=logger@staticmethoddef_abs_path(path):returnos.path.abspath(path)def_model_path(self,model_id):folder=os.path.join(self._dest_dir,model_id)returnfolderdef_get_latest_bentoml_tag(self,model_id):path=os.path.join(self._bentoml_dir,model_id)ifnotos.path.exists(path):returnNoneitems=sorted(os.listdir(path))ifnotitems:returnNoneelse:returnitems[-1]def_get_latest_bundle_tag(self,model_id):path=os.path.join(self._bundles_dir,model_id)ifnotos.path.exists(path):returnNoneitems=sorted(os.listdir(path))ifnotitems:returnNoneelse:returnitems[-1]def_get_bentoml_location(self,model_id):tag=self._get_latest_bentoml_tag(model_id)path=os.path.join(self._bentoml_dir,model_id)ifnotos.path.exists(path):self.logger.debug(f"BentoML path not found: {path}")returnNoneiftagisnotNone:returnos.path.join(path,tag)else:returnpathdef_get_bundle_location(self,model_id):tag=self._get_latest_bundle_tag(model_id)path=os.path.join(self._bundles_dir,model_id)ifnotos.path.exists(path):self.logger.debug(f"Bundle path not found: {path}")returnNoneiftagisnotNone:returnos.path.join(path,tag)else:returnpathdef_get_bento_location(self,model_id):ifself._resolve_pack_method_source(model_id)!="bentoml":returnNonecmd=["bentoml","get",f"{model_id}:latest","--print-location","--quiet"]stdout,stderr,returncode=run_command(cmd,quiet=True)ifreturncode!=0:self.logger.error(f"BentoML command failed: {stderr}")raiseBentoMLException(f"BentoML error: {stderr}")returnstdout.strip()def_is_ready(self,model_id):"""Check whether a model exists in the local computer"""try:self._get_latest_bundle_tag(model_id)exceptExceptionase:self.logger.debug(f"Model {model_id} not ready: {str(e)}")returnFalsepath=os.path.join(self._abs_path(self._dest_dir),model_id)ifnotos.path.exists(path):returnFalsereturnTruedef_has_credentials(self):ifself.credisNone:self.logger.warning("No credentials found.")returnFalsereturnTruedef_resolve_pack_method_source(self,model_id):bundle_path=self._get_bundle_location(model_id)ifos.path.exists(os.path.join(bundle_path,"installs","install.sh")):returnPACK_METHOD_FASTAPIelifos.path.exists(os.path.join(bundle_path,"bentoml.yml")):returnPACK_METHOD_BENTOMLself.logger.warning("Could not resolve pack method by simply looking at the bundle path")returnNone