Upload artifact.py with huggingface_hub
Browse files- artifact.py +58 -38
artifact.py
CHANGED
|
@@ -1,21 +1,22 @@
|
|
| 1 |
import difflib
|
| 2 |
import inspect
|
| 3 |
import json
|
|
|
|
| 4 |
import os
|
| 5 |
import pkgutil
|
| 6 |
-
from abc import
|
| 7 |
from copy import deepcopy
|
| 8 |
-
from typing import
|
| 9 |
|
| 10 |
-
from .dataclass import Dataclass, Field,
|
| 11 |
from .text_utils import camel_to_snake_case, is_camel_case
|
| 12 |
from .type_utils import issubtype
|
| 13 |
|
| 14 |
|
| 15 |
-
class Artifactories
|
| 16 |
def __new__(cls):
|
| 17 |
if not hasattr(cls, "instance"):
|
| 18 |
-
cls.instance = super(
|
| 19 |
cls.instance.artifactories = []
|
| 20 |
|
| 21 |
return cls.instance
|
|
@@ -27,15 +28,27 @@ class Artifactories(object):
|
|
| 27 |
return next(self.artifactories)
|
| 28 |
|
| 29 |
def register(self, artifactory):
|
| 30 |
-
assert isinstance(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def unregister(self, artifactory):
|
| 36 |
-
assert isinstance(
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
self.artifactories.remove(artifactory)
|
| 40 |
|
| 41 |
def reset(self):
|
|
@@ -62,7 +75,7 @@ def get_closest_artifact_type(type):
|
|
| 62 |
return None
|
| 63 |
|
| 64 |
|
| 65 |
-
class
|
| 66 |
def __init__(self, type) -> None:
|
| 67 |
maybe_class = "".join(word.capitalize() for word in type.split("_"))
|
| 68 |
message = f"'{type}' is not a recognized artifact 'type'. Make sure a the class defined this type (Probably called '{maybe_class}' or similar) is defined and/or imported anywhere in the code executed."
|
|
@@ -72,9 +85,11 @@ class UnrecognizedArtifactType(ValueError):
|
|
| 72 |
super().__init__(message)
|
| 73 |
|
| 74 |
|
| 75 |
-
class
|
| 76 |
def __init__(self, dic) -> None:
|
| 77 |
-
message =
|
|
|
|
|
|
|
| 78 |
super().__init__(message)
|
| 79 |
|
| 80 |
|
|
@@ -90,11 +105,13 @@ class Artifact(Dataclass):
|
|
| 90 |
@classmethod
|
| 91 |
def verify_artifact_dict(cls, d):
|
| 92 |
if not isinstance(d, dict):
|
| 93 |
-
raise ValueError(
|
|
|
|
|
|
|
| 94 |
if "type" not in d:
|
| 95 |
-
raise
|
| 96 |
if not cls.is_registered_type(d["type"]):
|
| 97 |
-
raise
|
| 98 |
|
| 99 |
@classmethod
|
| 100 |
def get_artifact_type(cls):
|
|
@@ -130,7 +147,7 @@ class Artifact(Dataclass):
|
|
| 130 |
def is_artifact_file(cls, path):
|
| 131 |
if not os.path.exists(path) or not os.path.isfile(path):
|
| 132 |
return False
|
| 133 |
-
with open(path
|
| 134 |
d = json.load(f)
|
| 135 |
return cls.is_artifact_dict(d)
|
| 136 |
|
|
@@ -155,10 +172,9 @@ class Artifact(Dataclass):
|
|
| 155 |
pass
|
| 156 |
if cls.is_artifact_dict(d):
|
| 157 |
cls.verify_artifact_dict(d)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
return d
|
| 162 |
|
| 163 |
@classmethod
|
| 164 |
def from_dict(cls, d):
|
|
@@ -167,7 +183,7 @@ class Artifact(Dataclass):
|
|
| 167 |
|
| 168 |
@classmethod
|
| 169 |
def load(cls, path):
|
| 170 |
-
with open(path
|
| 171 |
d = json.load(f)
|
| 172 |
return cls.from_dict(d)
|
| 173 |
|
|
@@ -186,7 +202,9 @@ class Artifact(Dataclass):
|
|
| 186 |
self.type = self.register_class(self.__class__)
|
| 187 |
|
| 188 |
for field in fields(self):
|
| 189 |
-
if issubtype(
|
|
|
|
|
|
|
| 190 |
value = getattr(self, field.name)
|
| 191 |
value = map_values_in_place(value, maybe_recover_artifact)
|
| 192 |
setattr(self, field.name, value)
|
|
@@ -200,7 +218,9 @@ class Artifact(Dataclass):
|
|
| 200 |
def save(self, path):
|
| 201 |
with open(path, "w") as f:
|
| 202 |
init_dict = self.to_dict()
|
| 203 |
-
json.
|
|
|
|
|
|
|
| 204 |
|
| 205 |
|
| 206 |
class ArtifactList(list, Artifact):
|
|
@@ -231,40 +251,40 @@ class UnitxtArtifactNotFoundError(Exception):
|
|
| 231 |
def fetch_artifact(name):
|
| 232 |
if Artifact.is_artifact_file(name):
|
| 233 |
return Artifact.load(name), None
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
|
| 239 |
raise UnitxtArtifactNotFoundError(name, Artifactories().artifactories)
|
| 240 |
|
| 241 |
|
| 242 |
def verbosed_fetch_artifact(identifer):
|
| 243 |
artifact, artifactory = fetch_artifact(identifer)
|
| 244 |
-
|
| 245 |
return artifact
|
| 246 |
|
| 247 |
|
| 248 |
def maybe_recover_artifact(artifact):
|
| 249 |
if isinstance(artifact, str):
|
| 250 |
return verbosed_fetch_artifact(artifact)
|
| 251 |
-
|
| 252 |
-
|
| 253 |
|
| 254 |
|
| 255 |
def register_all_artifacts(path):
|
| 256 |
-
for loader, module_name,
|
| 257 |
-
|
| 258 |
if module_name == __name__:
|
| 259 |
continue
|
| 260 |
-
|
| 261 |
# Import the module
|
| 262 |
module = loader.find_module(module_name).load_module(module_name)
|
| 263 |
|
| 264 |
# Iterate over every object in the module
|
| 265 |
-
for
|
| 266 |
# Make sure the object is a class
|
| 267 |
if inspect.isclass(obj):
|
| 268 |
# Make sure the class is a subclass of Artifact (but not Artifact itself)
|
| 269 |
if issubclass(obj, Artifact) and obj is not Artifact:
|
| 270 |
-
|
|
|
|
| 1 |
import difflib
|
| 2 |
import inspect
|
| 3 |
import json
|
| 4 |
+
import logging
|
| 5 |
import os
|
| 6 |
import pkgutil
|
| 7 |
+
from abc import abstractmethod
|
| 8 |
from copy import deepcopy
|
| 9 |
+
from typing import Dict, List, Union, final
|
| 10 |
|
| 11 |
+
from .dataclass import Dataclass, Field, fields
|
| 12 |
from .text_utils import camel_to_snake_case, is_camel_case
|
| 13 |
from .type_utils import issubtype
|
| 14 |
|
| 15 |
|
| 16 |
+
class Artifactories:
|
| 17 |
def __new__(cls):
|
| 18 |
if not hasattr(cls, "instance"):
|
| 19 |
+
cls.instance = super().__new__(cls)
|
| 20 |
cls.instance.artifactories = []
|
| 21 |
|
| 22 |
return cls.instance
|
|
|
|
| 28 |
return next(self.artifactories)
|
| 29 |
|
| 30 |
def register(self, artifactory):
|
| 31 |
+
assert isinstance(
|
| 32 |
+
artifactory, Artifactory
|
| 33 |
+
), "Artifactory must be an instance of Artifactory"
|
| 34 |
+
assert hasattr(
|
| 35 |
+
artifactory, "__contains__"
|
| 36 |
+
), "Artifactory must have __contains__ method"
|
| 37 |
+
assert hasattr(
|
| 38 |
+
artifactory, "__getitem__"
|
| 39 |
+
), "Artifactory must have __getitem__ method"
|
| 40 |
+
self.artifactories = [artifactory, *self.artifactories]
|
| 41 |
|
| 42 |
def unregister(self, artifactory):
|
| 43 |
+
assert isinstance(
|
| 44 |
+
artifactory, Artifactory
|
| 45 |
+
), "Artifactory must be an instance of Artifactory"
|
| 46 |
+
assert hasattr(
|
| 47 |
+
artifactory, "__contains__"
|
| 48 |
+
), "Artifactory must have __contains__ method"
|
| 49 |
+
assert hasattr(
|
| 50 |
+
artifactory, "__getitem__"
|
| 51 |
+
), "Artifactory must have __getitem__ method"
|
| 52 |
self.artifactories.remove(artifactory)
|
| 53 |
|
| 54 |
def reset(self):
|
|
|
|
| 75 |
return None
|
| 76 |
|
| 77 |
|
| 78 |
+
class UnrecognizedArtifactTypeError(ValueError):
|
| 79 |
def __init__(self, type) -> None:
|
| 80 |
maybe_class = "".join(word.capitalize() for word in type.split("_"))
|
| 81 |
message = f"'{type}' is not a recognized artifact 'type'. Make sure a the class defined this type (Probably called '{maybe_class}' or similar) is defined and/or imported anywhere in the code executed."
|
|
|
|
| 85 |
super().__init__(message)
|
| 86 |
|
| 87 |
|
| 88 |
+
class MissingArtifactTypeError(ValueError):
|
| 89 |
def __init__(self, dic) -> None:
|
| 90 |
+
message = (
|
| 91 |
+
f"Missing 'type' parameter. Expected 'type' in artifact dict, got {dic}"
|
| 92 |
+
)
|
| 93 |
super().__init__(message)
|
| 94 |
|
| 95 |
|
|
|
|
| 105 |
@classmethod
|
| 106 |
def verify_artifact_dict(cls, d):
|
| 107 |
if not isinstance(d, dict):
|
| 108 |
+
raise ValueError(
|
| 109 |
+
f"Artifact dict <{d}> must be of type 'dict', got '{type(d)}'."
|
| 110 |
+
)
|
| 111 |
if "type" not in d:
|
| 112 |
+
raise MissingArtifactTypeError(d)
|
| 113 |
if not cls.is_registered_type(d["type"]):
|
| 114 |
+
raise UnrecognizedArtifactTypeError(d["type"])
|
| 115 |
|
| 116 |
@classmethod
|
| 117 |
def get_artifact_type(cls):
|
|
|
|
| 147 |
def is_artifact_file(cls, path):
|
| 148 |
if not os.path.exists(path) or not os.path.isfile(path):
|
| 149 |
return False
|
| 150 |
+
with open(path) as f:
|
| 151 |
d = json.load(f)
|
| 152 |
return cls.is_artifact_dict(d)
|
| 153 |
|
|
|
|
| 172 |
pass
|
| 173 |
if cls.is_artifact_dict(d):
|
| 174 |
cls.verify_artifact_dict(d)
|
| 175 |
+
return cls._class_register[d.pop("type")](**d)
|
| 176 |
+
|
| 177 |
+
return d
|
|
|
|
| 178 |
|
| 179 |
@classmethod
|
| 180 |
def from_dict(cls, d):
|
|
|
|
| 183 |
|
| 184 |
@classmethod
|
| 185 |
def load(cls, path):
|
| 186 |
+
with open(path) as f:
|
| 187 |
d = json.load(f)
|
| 188 |
return cls.from_dict(d)
|
| 189 |
|
|
|
|
| 202 |
self.type = self.register_class(self.__class__)
|
| 203 |
|
| 204 |
for field in fields(self):
|
| 205 |
+
if issubtype(
|
| 206 |
+
field.type, Union[Artifact, List[Artifact], Dict[str, Artifact]]
|
| 207 |
+
):
|
| 208 |
value = getattr(self, field.name)
|
| 209 |
value = map_values_in_place(value, maybe_recover_artifact)
|
| 210 |
setattr(self, field.name, value)
|
|
|
|
| 218 |
def save(self, path):
|
| 219 |
with open(path, "w") as f:
|
| 220 |
init_dict = self.to_dict()
|
| 221 |
+
dumped = json.dumps(init_dict, indent=4)
|
| 222 |
+
f.write(dumped)
|
| 223 |
+
f.write("\n")
|
| 224 |
|
| 225 |
|
| 226 |
class ArtifactList(list, Artifact):
|
|
|
|
| 251 |
def fetch_artifact(name):
|
| 252 |
if Artifact.is_artifact_file(name):
|
| 253 |
return Artifact.load(name), None
|
| 254 |
+
|
| 255 |
+
for artifactory in Artifactories():
|
| 256 |
+
if name in artifactory:
|
| 257 |
+
return artifactory[name], artifactory
|
| 258 |
|
| 259 |
raise UnitxtArtifactNotFoundError(name, Artifactories().artifactories)
|
| 260 |
|
| 261 |
|
| 262 |
def verbosed_fetch_artifact(identifer):
|
| 263 |
artifact, artifactory = fetch_artifact(identifer)
|
| 264 |
+
logging.info(f"Artifact {identifer} is fetched from {artifactory}")
|
| 265 |
return artifact
|
| 266 |
|
| 267 |
|
| 268 |
def maybe_recover_artifact(artifact):
|
| 269 |
if isinstance(artifact, str):
|
| 270 |
return verbosed_fetch_artifact(artifact)
|
| 271 |
+
|
| 272 |
+
return artifact
|
| 273 |
|
| 274 |
|
| 275 |
def register_all_artifacts(path):
|
| 276 |
+
for loader, module_name, _is_pkg in pkgutil.walk_packages(path):
|
| 277 |
+
logging.info(__name__)
|
| 278 |
if module_name == __name__:
|
| 279 |
continue
|
| 280 |
+
logging.info(f"Loading {module_name}")
|
| 281 |
# Import the module
|
| 282 |
module = loader.find_module(module_name).load_module(module_name)
|
| 283 |
|
| 284 |
# Iterate over every object in the module
|
| 285 |
+
for _name, obj in inspect.getmembers(module):
|
| 286 |
# Make sure the object is a class
|
| 287 |
if inspect.isclass(obj):
|
| 288 |
# Make sure the class is a subclass of Artifact (but not Artifact itself)
|
| 289 |
if issubclass(obj, Artifact) and obj is not Artifact:
|
| 290 |
+
logging.info(obj)
|