Upload catalog.py with huggingface_hub
Browse files12345674 unmodified lines1213141516171819202122232425262728293031323334353612 unmodified lines4950515253545556575859606162636465666768697071727374757677787980818283848586878889import osfrom .artifact import Artifact, Artifactory, register_atrifactoryfrom .file_utils import get_all_files_in_dirclass Catalog(Artifactory):4 unmodified linestry:import unitxtcatalog_path = os.path.dirname(unitxt.__file__) + "/catalog"except ImportError:catalog_path = os.path.dirname(__file__) + "/catalog"class LocalCatalog(Catalog):name: str = "local"location: str = catalog_path@propertydef path_dict(self):result = {}for path in get_all_files_in_dir(self.location, recursive=True, file_extension=".json"):name = os.path.splitext(os.path.basename(path))[0]result[name] = pathreturn resultdef path(self, artifact_identifier: str):return self.path_dict.get(artifact_identifier, None)def load(self, artifact_identifier: str):assert artifact_identifier in self, "Artifact with name {} does not exist".format(artifact_identifier)12 unmodified linesreturn Falsereturn os.path.exists(path) and os.path.isfile(path)def save(self, artifact: Artifact, artifact_identifier: str, collection: str, overwrite: bool = False):assert isinstance(artifact, Artifact), "Artifact must be an instance of Artifact"if not overwrite:assert (artifact_identifier not in self), f"Artifact with name {artifact_identifier} already exists in catalog {self.name}"collection_dir = os.path.join(self.location, collection)os.makedirs(collection_dir, exist_ok=True)path = os.path.join(collection_dir, artifact_identifier + ".json")artifact.save(path)register_atrifactory(LocalCatalog())try:import unitxtlibrary_catalog = LocalCatalog("library", unitxt.__path__[0] + "/catalog")register_atrifactory(library_catalog)except:pass# create a catalog for the communityclass CommunityCatalog(Catalog):name = "community"location = "https://raw.githubusercontent.com/unitxt/unitxt/main/catalog/community.json"def load(self, artifact_identifier: str):passdef add_to_catalog(artifact: Artifact, name: str, collection=str, catalog: Catalog = None, overwrite: bool = False):if catalog is None:catalog = LocalCatalog()catalog.save(artifact, name, collection, overwrite=overwrite)123456789101112134 unmodified lines181920212223242526272829303132333412 unmodified lines4748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import osimport refrom pathlib import Pathimport requestsimport jsonfrom .artifact import Artifact, ArtifactoryCOLLECTION_SEPARATOR = '::'PATHS_SEP = ':'class Catalog(Artifactory):4 unmodified linestry:import unitxtdefault_catalog_path = os.path.dirname(unitxt.__file__) + "/catalog"except ImportError:default_catalog_path = os.path.dirname(__file__) + "/catalog"class LocalCatalog(Catalog):name: str = "local"location: str = default_catalog_pathdef path(self, artifact_identifier: str):return os.path.join(self.location, *(artifact_identifier + ".json").split(COLLECTION_SEPARATOR))def load(self, artifact_identifier: str):assert artifact_identifier in self, "Artifact with name {} does not exist".format(artifact_identifier)12 unmodified linesreturn Falsereturn os.path.exists(path) and os.path.isfile(path)def save_artifact(self, artifact: Artifact, artifact_identifier: str, overwrite: bool = False):assert isinstance(artifact, Artifact), f"Input artifact must be an instance of Artifact, got {type(artifact)}"if not overwrite:assert (artifact_identifier not in self), f"Artifact with name {artifact_identifier} already exists in catalog {self.name}"path = self.path(artifact_identifier)os.makedirs(Path(path).parent.absolute(), exist_ok=True)artifact.save(path)class GithubCatalog(LocalCatalog):name = "community"repo = "unitxt"repo_dir = "src/unitxt/catalog"user = "IBM"branch = "master"def prepare(self):self.location = f"https://raw.githubusercontent.com/{self.user}/{self.repo}/{self.branch}/{self.repo_dir}"def load(self, artifact_identifier: str):url = self.path(artifact_identifier)response = requests.get(url)data = response.json()return Artifact.from_dict(data)def __contains__(self, artifact_identifier: str):url = self.path(artifact_identifier)response = requests.head(url)return response.status_code == 200def verify_legal_catalog_name(name):assert re.match('^[\w' + COLLECTION_SEPARATOR + ']+$', name),\'Catalog name should be alphanumeric, ":" should specify dirs (instead of "/").'def add_to_catalog(artifact: Artifact, name: str, catalog: Catalog = None, overwrite: bool = False,catalog_path: str = None):if catalog is None:if catalog_path is None:catalog_path = default_catalog_pathcatalog = LocalCatalog(location=catalog_path)verify_legal_catalog_name(name)catalog.save_artifact(artifact, name, overwrite=overwrite) # remove collection (its actually the dir).# verify name
catalog.py+51-38