-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path_dump_tools.py
More file actions
172 lines (151 loc) · 7.22 KB
/
_dump_tools.py
File metadata and controls
172 lines (151 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import inspect
import json
import logging
from pathlib import Path
from types import UnionType
from typing import Any, TypeAlias, Union, get_args, get_origin
import joblib
import numpy as np
import numpy.typing as npt
from pydantic import BaseModel
from sklearn.base import BaseEstimator
from autointent import Embedder, Ranker, VectorIndex
from autointent.configs import CrossEncoderConfig, EmbedderConfig
from autointent.schemas import TagsList
ModuleSimpleAttributes = None | str | int | float | bool | list # type: ignore[type-arg]
ModuleAttributes: TypeAlias = (
ModuleSimpleAttributes | TagsList | np.ndarray | Embedder | VectorIndex | BaseEstimator | Ranker # type: ignore[type-arg]
)
logger = logging.getLogger(__name__)
class Dumper:
tags = "tags"
simple_attrs = "simple_attrs.json"
arrays = "arrays.npz"
embedders = "embedders"
indexes = "vector_indexes"
estimators = "estimators"
cross_encoders = "cross_encoders"
pydantic_models: str = "pydantic"
@staticmethod
def make_subdirectories(path: Path) -> None:
subdirectories = [
path / Dumper.tags,
path / Dumper.embedders,
path / Dumper.indexes,
path / Dumper.estimators,
path / Dumper.cross_encoders,
path / Dumper.pydantic_models,
]
for subdir in subdirectories:
subdir.mkdir(parents=True, exist_ok=True)
@staticmethod
def dump(obj: Any, path: Path) -> None: # noqa: ANN401, C901
"""Dump modules attributes to filestystem."""
attrs: dict[str, ModuleAttributes] = vars(obj)
simple_attrs = {}
arrays: dict[str, npt.NDArray[Any]] = {}
Dumper.make_subdirectories(path)
for key, val in attrs.items():
if isinstance(val, TagsList):
val.dump(path / Dumper.tags / key)
elif isinstance(val, ModuleSimpleAttributes):
simple_attrs[key] = val
elif isinstance(val, np.ndarray):
arrays[key] = val
elif isinstance(val, Embedder):
val.dump(path / Dumper.embedders / key)
elif isinstance(val, VectorIndex):
val.dump(path / Dumper.indexes / key)
elif isinstance(val, BaseEstimator):
joblib.dump(val, path / Dumper.estimators / key)
elif isinstance(val, Ranker):
val.save(str(path / Dumper.cross_encoders / key))
elif isinstance(val, CrossEncoderConfig | EmbedderConfig):
try:
pydantic_path = path / Dumper.pydantic_models / f"{key}.json"
with pydantic_path.open("w", encoding="utf-8") as file:
json.dump(val.model_dump(), file, ensure_ascii=False, indent=4)
except Exception as e:
msg = f"Error dumping pydantic model {key}: {e}"
logging.exception(msg)
else:
msg = f"Attribute {key} of type {type(val)} cannot be dumped to file system."
logger.error(msg)
with (path / Dumper.simple_attrs).open("w") as file:
json.dump(simple_attrs, file, ensure_ascii=False, indent=4)
np.savez(path / Dumper.arrays, allow_pickle=False, **arrays)
@staticmethod
def load( # noqa: PLR0912, C901, PLR0915
obj: Any, # noqa: ANN401
path: Path,
embedder_config: EmbedderConfig | None = None,
cross_encoder_config: CrossEncoderConfig | None = None,
) -> None:
"""Load attributes from file system."""
tags: dict[str, Any] = {}
simple_attrs: dict[str, Any] = {}
arrays: dict[str, Any] = {}
embedders: dict[str, Any] = {}
indexes: dict[str, Any] = {}
estimators: dict[str, Any] = {}
cross_encoders: dict[str, Any] = {}
pydantic_models: dict[str, Any] = {}
for child in path.iterdir():
if child.name == Dumper.tags:
tags = {tags_dump.name: TagsList.load(tags_dump) for tags_dump in child.iterdir()}
elif child.name == Dumper.simple_attrs:
with child.open() as file:
simple_attrs = json.load(file)
elif child.name == Dumper.arrays:
arrays = dict(np.load(child))
elif child.name == Dumper.embedders:
embedders = {
embedder_dump.name: Embedder.load(embedder_dump, override_config=embedder_config)
for embedder_dump in child.iterdir()
}
elif child.name == Dumper.indexes:
indexes = {index_dump.name: VectorIndex.load(index_dump) for index_dump in child.iterdir()}
elif child.name == Dumper.estimators:
estimators = {estimator_dump.name: joblib.load(estimator_dump) for estimator_dump in child.iterdir()}
elif child.name == Dumper.cross_encoders:
cross_encoders = {
cross_encoder_dump.name: Ranker.load(cross_encoder_dump, override_config=cross_encoder_config)
for cross_encoder_dump in child.iterdir()
}
elif child.name == Dumper.pydantic_models:
for model_file in child.iterdir():
with model_file.open("r", encoding="utf-8") as file:
content = json.load(file)
variable_name = model_file.stem
# First try to get the type annotation from the class annotations.
model_type = obj.__class__.__annotations__.get(variable_name)
# Fallback: inspect __init__ signature if not found in class-level annotations.
if model_type is None:
sig = inspect.signature(obj.__init__)
if variable_name in sig.parameters:
model_type = sig.parameters[variable_name].annotation
if model_type is None:
msg = f"No type annotation found for {variable_name}"
logger.error(msg)
continue
# If the annotation is a Union, extract the pydantic model type.
if get_origin(model_type) in (UnionType, Union):
for arg in get_args(model_type):
if isinstance(arg, type) and issubclass(arg, BaseModel):
model_type = arg
break
else:
msg = f"No pydantic type found in Union for {variable_name}"
logger.error(msg)
continue
if not (isinstance(model_type, type) and issubclass(model_type, BaseModel)):
msg = f"Type for {variable_name} is not a pydantic model: {model_type}"
logger.error(msg)
continue
pydantic_models[variable_name] = model_type(**content)
else:
msg = f"Found unexpected child {child}"
logger.error(msg)
obj.__dict__.update(
tags | simple_attrs | arrays | embedders | indexes | estimators | cross_encoders | pydantic_models
)