Upload artifact.py with huggingface_hub
Browse files12344561 unmodified line8910111213141516171819104 unmodified lines12412512612412712812912713013113213313413513613413713813914017 unmodified lines15815916016116216316416516616716822 unmodified lines19119219318618719419519619725 unmodified lines22322422521922022122222322622722822923034 unmodified lines2652662672642682692702716 unmodified lines2782792802772812822832802842852862873 unmodified lines291292293290294import difflibimport inspectimport jsonimport loggingimport osimport pkgutilfrom abc import abstractmethod1 unmodified linefrom typing import Dict, List, Union, final
from .dataclass import Dataclass, Field, fieldsfrom .logging_utils import get_loggerfrom .text_utils import camel_to_snake_case, is_camel_casefrom .type_utils import issubtypefrom .utils import load_json, save_json
logger = get_logger()
class Artifactories:104 unmodified lines def register_class(cls, artifact_class): assert issubclass( artifact_class, Artifact ), f"Artifact class must be a subclass of Artifact, got {artifact_class}" ), f"Artifact class must be a subclass of Artifact, got '{artifact_class}'" assert is_camel_case( artifact_class.__name__ ), f"Artifact class name must be legal camel case, got {artifact_class.__name__}" ), f"Artifact class name must be legal camel case, got '{artifact_class.__name__}'"
snake_case_key = camel_to_snake_case(artifact_class.__name__)
if cls.is_registered_type(snake_case_key): assert ( cls._class_register[snake_case_key] == artifact_class ), f"Artifact class name must be unique, {snake_case_key} already exists for {cls._class_register[snake_case_key]}" ), f"Artifact class name must be unique, '{snake_case_key}' already exists for '{cls._class_register[snake_case_key]}'"
return snake_case_key
17 unmodified lines def is_registered_type(cls, type: str): return type in cls._class_register
@classmethod def is_registered_class_name(cls, class_name: str): snake_case_key = camel_to_snake_case(class_name) return cls.is_registered_type(snake_case_key)
@classmethod def is_registered_class(cls, clz: object): return clz in set(cls._class_register.values())22 unmodified lines
@classmethod def load(cls, path): with open(path) as f: d = json.load(f) d = load_json(path) return cls.from_dict(d)
def prepare(self):25 unmodified lines return {"type": self.type, **self._init_dict}
def save(self, path): with open(path, "w") as f: init_dict = self.to_dict() dumped = json.dumps(init_dict, indent=4) f.write(dumped) f.write("\n") data = self.to_dict() save_json(path, data)
class ArtifactList(list, Artifact):34 unmodified lines
def verbosed_fetch_artifact(identifer): artifact, artifactory = fetch_artifact(identifer) logging.info(f"Artifact {identifer} is fetched from {artifactory}") logger.info(f"Artifact {identifer} is fetched from {artifactory}") return artifact
6 unmodified lines
def register_all_artifacts(path): for loader, module_name, _is_pkg in pkgutil.walk_packages(path): logging.info(__name__) logger.info(__name__) if module_name == __name__: continue logging.info(f"Loading {module_name}") logger.info(f"Loading {module_name}") # Import the module module = loader.find_module(module_name).load_module(module_name)
3 unmodified lines if inspect.isclass(obj): # Make sure the class is a subclass of Artifact (but not Artifact itself) if issubclass(obj, Artifact) and obj is not Artifact: logging.info(obj) logger.info(obj)artifact.py+19-15