[docs]classModelBase(ErsiliaBase):""" Base class for managing models. This class provides foundational functionality for handling models, including initialization, validation, and checking local availability. Parameters ---------- model_id_or_slug : str, optional The model identifier or slug, by default None. repo_path : str, optional The repository path, by default None. config_json : dict, optional Configuration in JSON format, by default None. """@throw_ersilia_exception()def__init__(self,model_id_or_slug=None,repo_path=None,config_json=None):ErsiliaBase.__init__(self,config_json=config_json,credentials_json=None)ifmodel_id_or_slugisNoneandrepo_pathisNone:raiseExceptionifmodel_id_or_slugisnotNoneandrepo_pathisnotNone:raiseExceptionifmodel_id_or_slugisnotNone:self.text=model_id_or_slugslugger=Slug()ifslugger.is_slug(model_id_or_slug):self.slug=model_id_or_slugself.model_id=slugger.encode(self.slug)else:self.model_id=model_id_or_slugself.slug=slugger.decode(self.model_id)ifnotself.is_valid():raiseInvalidModelIdentifierError(model=self.text)ifrepo_pathisnotNone:self.logger.debug(f"Repo path specified: {repo_path}")abspath=os.path.abspath(repo_path)self.logger.debug(f"Absolute path: {abspath}")# Check if path actually existsifnotos.path.exists(abspath):raiseFileNotFoundError("Model directory does not exist at the provided path. Please check the path and try again.")self.text=self._get_model_id_from_path(repo_path)self.model_id=self.textslug=self._get_slug_if_available(repo_path)ifslugisNone:self.slug="my-model"else:self.slug=slugdef_get_model_id_from_path(self,repo_path):returnos.path.basename(os.path.abspath(repo_path)).rstrip("/")def_get_slug_if_available(self,repo_path):try:data=get_metadata_from_base_dir(repo_path)exceptFileNotFoundError:returnNoneslug=data["Slug"]ifslug=="":returnNoneelse:returnslug
[docs]defis_valid(self):""" Check if the model identifier and slug are valid. Returns ------- bool True if the model identifier and slug are valid, False otherwise. """ifself.model_idisNoneorself.slugisNone:returnFalseelse:returnTrue
def_is_available_locally_from_status(self):fetch_status_file=os.path.join(self._dest_dir,self.model_id,STATUS_FILE)ifnotos.path.exists(fetch_status_file):self.logger.debug("No status file exists")is_fetched=Falseelse:withopen(fetch_status_file,"r")asf:status=json.load(f)is_fetched=status[DONE_TAG]self.logger.debug("Is fetched: {0}".format(is_fetched))returnis_fetcheddef_is_available_locally_from_dockerhub(self):from_dockerhub_file=os.path.join(self._dest_dir,self.model_id,DOCKER_INFO_FILE)ifnotos.path.exists(from_dockerhub_file):returnFalseelse:returnTrue
[docs]defis_available_locally(self):""" Check if the model is available locally either from the status file or from DockerHub. Returns ------- bool True if the model is available locally, False otherwise. """bs=self._is_available_locally_from_status()bd=self._is_available_locally_from_dockerhub()ifbsorbd:returnTrueelse:returnFalse
[docs]defwas_fetched_from_dockerhub(self):""" Check if the model was fetched from DockerHub by reading the DockerHub file. Returns ------- bool True if the model was fetched from DockerHub, False otherwise. """from_dockerhub_file=os.path.join(self._dest_dir,self.model_id,DOCKER_INFO_FILE)ifnotos.path.exists(from_dockerhub_file):returnFalsewithopen(from_dockerhub_file,"r")asf:data=json.load(f)returndata["docker_hub"]