openmodels 0.1.0a20__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {openmodels-0.1.0a20 → openmodels-0.2.0}/LICENSE +1 -1
- openmodels-0.1.0a20/README.md → openmodels-0.2.0/PKG-INFO +89 -19
- openmodels-0.1.0a20/PKG-INFO → openmodels-0.2.0/README.md +55 -39
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/__init__.py +8 -3
- openmodels-0.2.0/openmodels/converters/__init__.py +13 -0
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/converters/json_converter.py +2 -0
- openmodels-0.2.0/openmodels/converters/msgpack_converter.py +74 -0
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/converters/pickle_converter.py +16 -0
- openmodels-0.2.0/openmodels/converters/yaml_converter.py +81 -0
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/core.py +31 -19
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/protocols.py +6 -1
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/serializers/base.py +36 -3
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/serializers/sklearn/_custom_estimator.py +1 -1
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/serializers/sklearn/sklearn_serializer.py +417 -78
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/test_helpers.py +97 -2
- openmodels-0.2.0/pyproject.toml +76 -0
- openmodels-0.1.0a20/openmodels/converters/__init__.py +0 -11
- openmodels-0.1.0a20/pyproject.toml +0 -30
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/exceptions.py +0 -0
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/format_registry.py +0 -0
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/serializers/__init__.py +0 -0
- {openmodels-0.1.0a20 → openmodels-0.2.0}/openmodels/serializers/sklearn/__init__.py +0 -0
|
@@ -1,9 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openmodels
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Export scikit-learn model files to JSON for sharing or deploying predictive models with peace of mind.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: scikit-learn,serialization,machine-learning,json,model-export,mlops
|
|
8
|
+
Author: Alejandro Gutierrez
|
|
9
|
+
Author-email: a.gutierrez@g-npd.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Provides-Extra: msgpack
|
|
23
|
+
Provides-Extra: yaml
|
|
24
|
+
Requires-Dist: PyYAML (>=6.0,<7.0) ; extra == "yaml"
|
|
25
|
+
Requires-Dist: msgpack (>=1.0,<2.0) ; extra == "msgpack"
|
|
26
|
+
Requires-Dist: scikit-learn (>=1.6.0,<2.0.0)
|
|
27
|
+
Project-URL: Changelog, https://github.com/Gnpd/openmodels/blob/main/CHANGELOG.md
|
|
28
|
+
Project-URL: Documentation, https://gnpd.github.io/openmodels/
|
|
29
|
+
Project-URL: Homepage, https://gnpd.github.io/openmodels/
|
|
30
|
+
Project-URL: Issues, https://github.com/Gnpd/openmodels/issues
|
|
31
|
+
Project-URL: Repository, https://github.com/Gnpd/openmodels
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
1
34
|
# OpenModels
|
|
2
35
|
|
|
3
|
-
[](https://badge.fury.io/py/openmodels)
|
|
4
37
|
[](https://opensource.org/licenses/MIT)
|
|
5
38
|
[](https://pypi.org/project/openmodels/)
|
|
6
|
-
[](https://test.pypi.org/project/openmodels/)
|
|
7
39
|
|
|
8
40
|
OpenModels is a flexible and extensible library for serializing and deserializing machine learning models. It's designed to support any serialization format through a plugin-based architecture, providing a safe and transparent solution for exporting and sharing predictive models.
|
|
9
41
|
|
|
@@ -11,7 +43,7 @@ OpenModels is a flexible and extensible library for serializing and deserializin
|
|
|
11
43
|
|
|
12
44
|
- **Format Agnostic**: Supports any serialization format through a plugin-based system.
|
|
13
45
|
- **Extensible**: Easily add support for new model types and serialization formats.
|
|
14
|
-
- **Safe**: Provides alternatives to potentially unsafe serialization methods like Pickle.
|
|
46
|
+
- **Safe**: Provides alternatives to potentially unsafe serialization methods like Pickle (see [Security](#security)).
|
|
15
47
|
- **Transparent**: Supports human-readable formats for easy inspection of serialized models.
|
|
16
48
|
|
|
17
49
|
## Installation
|
|
@@ -20,6 +52,14 @@ OpenModels is a flexible and extensible library for serializing and deserializin
|
|
|
20
52
|
pip install openmodels
|
|
21
53
|
```
|
|
22
54
|
|
|
55
|
+
JSON and Pickle work out of the box. The MessagePack and YAML formats need their (optional)
|
|
56
|
+
underlying library:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install openmodels[msgpack] # format_name="msgpack"
|
|
60
|
+
pip install openmodels[yaml] # format_name="yaml"
|
|
61
|
+
```
|
|
62
|
+
|
|
23
63
|
## Quick Start
|
|
24
64
|
|
|
25
65
|
```python
|
|
@@ -63,27 +103,31 @@ OpenModels is designed to be easily extended with new serialization formats and
|
|
|
63
103
|
|
|
64
104
|
### Adding a New Format
|
|
65
105
|
|
|
66
|
-
To add a new serialization format, create a class that implements the `FormatConverter` protocol and register it with the `FormatRegistry
|
|
106
|
+
To add a new serialization format, create a class that implements the `FormatConverter` protocol and register it with the `FormatRegistry`. `is_binary` tells `SerializationManager.load()` whether to open a saved file in binary or text mode:
|
|
67
107
|
|
|
68
108
|
```python
|
|
69
109
|
from openmodels.protocols import FormatConverter
|
|
70
110
|
from openmodels.format_registry import FormatRegistry
|
|
71
111
|
from typing import Dict, Any
|
|
72
112
|
|
|
73
|
-
class
|
|
113
|
+
class TOMLConverter(FormatConverter):
|
|
114
|
+
is_binary = False
|
|
115
|
+
|
|
74
116
|
@staticmethod
|
|
75
117
|
def serialize_to_format(data: Dict[str, Any]) -> str:
|
|
76
|
-
import
|
|
77
|
-
return
|
|
118
|
+
import toml
|
|
119
|
+
return toml.dumps(data)
|
|
78
120
|
|
|
79
121
|
@staticmethod
|
|
80
122
|
def deserialize_from_format(formatted_data: str) -> Dict[str, Any]:
|
|
81
|
-
import
|
|
82
|
-
return
|
|
123
|
+
import toml
|
|
124
|
+
return toml.loads(formatted_data)
|
|
83
125
|
|
|
84
|
-
FormatRegistry.register("
|
|
126
|
+
FormatRegistry.register("toml", TOMLConverter)
|
|
85
127
|
```
|
|
86
128
|
|
|
129
|
+
JSON, Pickle, MessagePack, and YAML already ship built-in this way - see `openmodels/converters/` for their real implementations, including how the optional `msgpack`/`PyYAML` dependencies are imported lazily so they're only required if you actually use those formats.
|
|
130
|
+
|
|
87
131
|
### Adding a New Model Serializer
|
|
88
132
|
|
|
89
133
|
To add support for a new type of model, create a class that implements the `ModelSerializer` protocol:
|
|
@@ -104,14 +148,19 @@ class TensorFlowSerializer(ModelSerializer):
|
|
|
104
148
|
|
|
105
149
|
## Supported Models (scikit-learn)
|
|
106
150
|
|
|
107
|
-
OpenModels
|
|
151
|
+
OpenModels supports every scikit-learn estimator, including:
|
|
108
152
|
|
|
109
153
|
- Classification: LogisticRegression, SVC, etc.
|
|
110
154
|
- Regression: LinearRegression, SVR, etc.
|
|
111
155
|
- Clustering: KMeans
|
|
112
156
|
- Dimensionality Reduction: PCA
|
|
113
157
|
|
|
114
|
-
|
|
158
|
+
See the [Supported Models](https://gnpd.github.io/openmodels/supported_models.html) docs page
|
|
159
|
+
for the full breakdown by category, and the
|
|
160
|
+
[serialized format reference](https://gnpd.github.io/openmodels/format.html) for what actually
|
|
161
|
+
gets written to disk.
|
|
162
|
+
|
|
163
|
+
You can also retrieve the list programmatically using the `SklearnSerializer.all_estimators()` method:
|
|
115
164
|
|
|
116
165
|
```python
|
|
117
166
|
from openmodels.serializers import SklearnSerializer
|
|
@@ -129,8 +178,6 @@ regressors = SklearnSerializer.all_estimators(type_filter="regressor")
|
|
|
129
178
|
print([name for name, cls in regressors])
|
|
130
179
|
```
|
|
131
180
|
|
|
132
|
-
This will print the names of all scikit-learn estimators supported by OpenModels, filtered to exclude those that are not currently supported.
|
|
133
|
-
|
|
134
181
|
## Using Custom Estimators and Pipelines (Third-Party Support)
|
|
135
182
|
|
|
136
183
|
OpenModels can serialize and deserialize models and pipelines that include third-party estimators, such as those from [chemotools](https://github.com/paucablop/chemotools).
|
|
@@ -169,11 +216,24 @@ print(y_train_pred)
|
|
|
169
216
|
|
|
170
217
|
You can pass any compatible `all_estimators` function, list, or dictionary to `SklearnSerializer(custom_estimators=...)` to extend support for custom or third-party estimators.
|
|
171
218
|
|
|
219
|
+
## scikit-learn Version Compatibility
|
|
220
|
+
|
|
221
|
+
OpenModels is tested against the following scikit-learn versions via the on-demand [`scikit-learn compatibility`](https://github.com/Gnpd/openmodels/actions/workflows/sklearn-compat.yml) CI workflow:
|
|
222
|
+
|
|
223
|
+
| scikit-learn | Status |
|
|
224
|
+
|---|---|
|
|
225
|
+
| 1.6.1 | ✅ Tested |
|
|
226
|
+
| 1.7.2 | ✅ Tested |
|
|
227
|
+
| 1.8.0 | ✅ Tested |
|
|
228
|
+
| 1.9.1 | ✅ Tested |
|
|
229
|
+
|
|
230
|
+
If you encounter any incompatibility or a use case where the library does not work correctly with your version of scikit-learn, please [open an issue](https://github.com/Gnpd/openmodels/issues/new) — we would greatly appreciate your feedback!
|
|
231
|
+
|
|
172
232
|
## Contributing
|
|
173
233
|
|
|
174
234
|
We welcome contributions to OpenModels! Whether you want to add support for new models, implement new serialization formats, or improve the existing codebase, your help is appreciated.
|
|
175
235
|
|
|
176
|
-
Please refer to our [Contributing Guidelines](https://github.com/
|
|
236
|
+
Please refer to our [Contributing Guidelines](https://github.com/Gnpd/openmodels/blob/main/CONTRIBUTING.md) for more information on how to get started.
|
|
177
237
|
|
|
178
238
|
## Running Tests
|
|
179
239
|
|
|
@@ -184,7 +244,7 @@ To run the tests:
|
|
|
184
244
|
1. Clone the repository:
|
|
185
245
|
|
|
186
246
|
```bash
|
|
187
|
-
git clone https://github.com/
|
|
247
|
+
git clone https://github.com/Gnpd/openmodels.git
|
|
188
248
|
cd openmodels
|
|
189
249
|
```
|
|
190
250
|
|
|
@@ -200,17 +260,26 @@ To run the tests:
|
|
|
200
260
|
task test
|
|
201
261
|
```
|
|
202
262
|
|
|
263
|
+
## Security
|
|
264
|
+
|
|
265
|
+
OpenModels's JSON, MessagePack, and YAML formats are all safe to load from untrusted
|
|
266
|
+
sources (YAML because OpenModels always uses `yaml.safe_load`/`safe_dump`). The optional
|
|
267
|
+
Pickle format is not: like any pickle-based tool, deserializing it can execute arbitrary
|
|
268
|
+
code, so only load pickle files from sources you trust. See
|
|
269
|
+
[SECURITY.md](https://github.com/Gnpd/openmodels/blob/main/SECURITY.md) for details and
|
|
270
|
+
how to report a vulnerability.
|
|
271
|
+
|
|
203
272
|
## License
|
|
204
273
|
|
|
205
|
-
This project is licensed under the MIT License. See the [LICENSE](https://github.com/
|
|
274
|
+
This project is licensed under the MIT License. See the [LICENSE](https://github.com/Gnpd/openmodels/blob/main/LICENSE) file for details.
|
|
206
275
|
|
|
207
276
|
## Changelog
|
|
208
277
|
|
|
209
|
-
For a detailed changelog, please see the [CHANGELOG.md](https://github.com/
|
|
278
|
+
For a detailed changelog, please see the [CHANGELOG.md](https://github.com/Gnpd/openmodels/blob/main/CHANGELOG.md) file.
|
|
210
279
|
|
|
211
280
|
## Support
|
|
212
281
|
|
|
213
|
-
If you encounter any issues or have questions, please [file an issue](https://github.com/
|
|
282
|
+
If you encounter any issues or have questions, please [file an issue](https://github.com/Gnpd/openmodels/issues/new) on our GitHub repository.
|
|
214
283
|
|
|
215
284
|
We're always looking to improve OpenModels. If you have any suggestions or feature requests, please let us know!
|
|
216
285
|
|
|
@@ -219,3 +288,4 @@ We're always looking to improve OpenModels. If you have any suggestions or featu
|
|
|
219
288
|
During the revision and design of OpenModels, we came across the discontinued project [**sklearn-json**](https://github.com/mlrequest/sklearn-json).
|
|
220
289
|
Although no longer maintained, it provided valuable ideas — particularly around testing approaches — that inspired parts of our implementation.
|
|
221
290
|
We would like to acknowledge and thank its authors for their earlier contributions to open model serialization efforts in the scikit-learn ecosystem.
|
|
291
|
+
|
|
@@ -1,28 +1,8 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: openmodels
|
|
3
|
-
Version: 0.1.0a20
|
|
4
|
-
Summary: Export scikit-learn model files to JSON for sharing or deploying predictive models with peace of mind.
|
|
5
|
-
License: MIT
|
|
6
|
-
License-File: LICENSE
|
|
7
|
-
Author: Alejandro Gutierrez
|
|
8
|
-
Author-email: agutierrez@sftec.es>, Pau Cabaneros <pau.cabaneros@gmail.com>, Raúl Marín <hi@raulmarin.dev>, Ruben Parrilla <rparrilla@sftec.es
|
|
9
|
-
Requires-Python: >=3.10,<4.0
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
-
Classifier: Programming Language :: Python :: 3
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
-
Requires-Dist: scikit-learn (>=1.6.0,<2.0.0)
|
|
18
|
-
Description-Content-Type: text/markdown
|
|
19
|
-
|
|
20
1
|
# OpenModels
|
|
21
2
|
|
|
22
|
-
[](https://badge.fury.io/py/openmodels)
|
|
23
4
|
[](https://opensource.org/licenses/MIT)
|
|
24
5
|
[](https://pypi.org/project/openmodels/)
|
|
25
|
-
[](https://test.pypi.org/project/openmodels/)
|
|
26
6
|
|
|
27
7
|
OpenModels is a flexible and extensible library for serializing and deserializing machine learning models. It's designed to support any serialization format through a plugin-based architecture, providing a safe and transparent solution for exporting and sharing predictive models.
|
|
28
8
|
|
|
@@ -30,7 +10,7 @@ OpenModels is a flexible and extensible library for serializing and deserializin
|
|
|
30
10
|
|
|
31
11
|
- **Format Agnostic**: Supports any serialization format through a plugin-based system.
|
|
32
12
|
- **Extensible**: Easily add support for new model types and serialization formats.
|
|
33
|
-
- **Safe**: Provides alternatives to potentially unsafe serialization methods like Pickle.
|
|
13
|
+
- **Safe**: Provides alternatives to potentially unsafe serialization methods like Pickle (see [Security](#security)).
|
|
34
14
|
- **Transparent**: Supports human-readable formats for easy inspection of serialized models.
|
|
35
15
|
|
|
36
16
|
## Installation
|
|
@@ -39,6 +19,14 @@ OpenModels is a flexible and extensible library for serializing and deserializin
|
|
|
39
19
|
pip install openmodels
|
|
40
20
|
```
|
|
41
21
|
|
|
22
|
+
JSON and Pickle work out of the box. The MessagePack and YAML formats need their (optional)
|
|
23
|
+
underlying library:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install openmodels[msgpack] # format_name="msgpack"
|
|
27
|
+
pip install openmodels[yaml] # format_name="yaml"
|
|
28
|
+
```
|
|
29
|
+
|
|
42
30
|
## Quick Start
|
|
43
31
|
|
|
44
32
|
```python
|
|
@@ -82,27 +70,31 @@ OpenModels is designed to be easily extended with new serialization formats and
|
|
|
82
70
|
|
|
83
71
|
### Adding a New Format
|
|
84
72
|
|
|
85
|
-
To add a new serialization format, create a class that implements the `FormatConverter` protocol and register it with the `FormatRegistry
|
|
73
|
+
To add a new serialization format, create a class that implements the `FormatConverter` protocol and register it with the `FormatRegistry`. `is_binary` tells `SerializationManager.load()` whether to open a saved file in binary or text mode:
|
|
86
74
|
|
|
87
75
|
```python
|
|
88
76
|
from openmodels.protocols import FormatConverter
|
|
89
77
|
from openmodels.format_registry import FormatRegistry
|
|
90
78
|
from typing import Dict, Any
|
|
91
79
|
|
|
92
|
-
class
|
|
80
|
+
class TOMLConverter(FormatConverter):
|
|
81
|
+
is_binary = False
|
|
82
|
+
|
|
93
83
|
@staticmethod
|
|
94
84
|
def serialize_to_format(data: Dict[str, Any]) -> str:
|
|
95
|
-
import
|
|
96
|
-
return
|
|
85
|
+
import toml
|
|
86
|
+
return toml.dumps(data)
|
|
97
87
|
|
|
98
88
|
@staticmethod
|
|
99
89
|
def deserialize_from_format(formatted_data: str) -> Dict[str, Any]:
|
|
100
|
-
import
|
|
101
|
-
return
|
|
90
|
+
import toml
|
|
91
|
+
return toml.loads(formatted_data)
|
|
102
92
|
|
|
103
|
-
FormatRegistry.register("
|
|
93
|
+
FormatRegistry.register("toml", TOMLConverter)
|
|
104
94
|
```
|
|
105
95
|
|
|
96
|
+
JSON, Pickle, MessagePack, and YAML already ship built-in this way - see `openmodels/converters/` for their real implementations, including how the optional `msgpack`/`PyYAML` dependencies are imported lazily so they're only required if you actually use those formats.
|
|
97
|
+
|
|
106
98
|
### Adding a New Model Serializer
|
|
107
99
|
|
|
108
100
|
To add support for a new type of model, create a class that implements the `ModelSerializer` protocol:
|
|
@@ -123,14 +115,19 @@ class TensorFlowSerializer(ModelSerializer):
|
|
|
123
115
|
|
|
124
116
|
## Supported Models (scikit-learn)
|
|
125
117
|
|
|
126
|
-
OpenModels
|
|
118
|
+
OpenModels supports every scikit-learn estimator, including:
|
|
127
119
|
|
|
128
120
|
- Classification: LogisticRegression, SVC, etc.
|
|
129
121
|
- Regression: LinearRegression, SVR, etc.
|
|
130
122
|
- Clustering: KMeans
|
|
131
123
|
- Dimensionality Reduction: PCA
|
|
132
124
|
|
|
133
|
-
|
|
125
|
+
See the [Supported Models](https://gnpd.github.io/openmodels/supported_models.html) docs page
|
|
126
|
+
for the full breakdown by category, and the
|
|
127
|
+
[serialized format reference](https://gnpd.github.io/openmodels/format.html) for what actually
|
|
128
|
+
gets written to disk.
|
|
129
|
+
|
|
130
|
+
You can also retrieve the list programmatically using the `SklearnSerializer.all_estimators()` method:
|
|
134
131
|
|
|
135
132
|
```python
|
|
136
133
|
from openmodels.serializers import SklearnSerializer
|
|
@@ -148,8 +145,6 @@ regressors = SklearnSerializer.all_estimators(type_filter="regressor")
|
|
|
148
145
|
print([name for name, cls in regressors])
|
|
149
146
|
```
|
|
150
147
|
|
|
151
|
-
This will print the names of all scikit-learn estimators supported by OpenModels, filtered to exclude those that are not currently supported.
|
|
152
|
-
|
|
153
148
|
## Using Custom Estimators and Pipelines (Third-Party Support)
|
|
154
149
|
|
|
155
150
|
OpenModels can serialize and deserialize models and pipelines that include third-party estimators, such as those from [chemotools](https://github.com/paucablop/chemotools).
|
|
@@ -188,11 +183,24 @@ print(y_train_pred)
|
|
|
188
183
|
|
|
189
184
|
You can pass any compatible `all_estimators` function, list, or dictionary to `SklearnSerializer(custom_estimators=...)` to extend support for custom or third-party estimators.
|
|
190
185
|
|
|
186
|
+
## scikit-learn Version Compatibility
|
|
187
|
+
|
|
188
|
+
OpenModels is tested against the following scikit-learn versions via the on-demand [`scikit-learn compatibility`](https://github.com/Gnpd/openmodels/actions/workflows/sklearn-compat.yml) CI workflow:
|
|
189
|
+
|
|
190
|
+
| scikit-learn | Status |
|
|
191
|
+
|---|---|
|
|
192
|
+
| 1.6.1 | ✅ Tested |
|
|
193
|
+
| 1.7.2 | ✅ Tested |
|
|
194
|
+
| 1.8.0 | ✅ Tested |
|
|
195
|
+
| 1.9.1 | ✅ Tested |
|
|
196
|
+
|
|
197
|
+
If you encounter any incompatibility or a use case where the library does not work correctly with your version of scikit-learn, please [open an issue](https://github.com/Gnpd/openmodels/issues/new) — we would greatly appreciate your feedback!
|
|
198
|
+
|
|
191
199
|
## Contributing
|
|
192
200
|
|
|
193
201
|
We welcome contributions to OpenModels! Whether you want to add support for new models, implement new serialization formats, or improve the existing codebase, your help is appreciated.
|
|
194
202
|
|
|
195
|
-
Please refer to our [Contributing Guidelines](https://github.com/
|
|
203
|
+
Please refer to our [Contributing Guidelines](https://github.com/Gnpd/openmodels/blob/main/CONTRIBUTING.md) for more information on how to get started.
|
|
196
204
|
|
|
197
205
|
## Running Tests
|
|
198
206
|
|
|
@@ -203,7 +211,7 @@ To run the tests:
|
|
|
203
211
|
1. Clone the repository:
|
|
204
212
|
|
|
205
213
|
```bash
|
|
206
|
-
git clone https://github.com/
|
|
214
|
+
git clone https://github.com/Gnpd/openmodels.git
|
|
207
215
|
cd openmodels
|
|
208
216
|
```
|
|
209
217
|
|
|
@@ -219,17 +227,26 @@ To run the tests:
|
|
|
219
227
|
task test
|
|
220
228
|
```
|
|
221
229
|
|
|
230
|
+
## Security
|
|
231
|
+
|
|
232
|
+
OpenModels's JSON, MessagePack, and YAML formats are all safe to load from untrusted
|
|
233
|
+
sources (YAML because OpenModels always uses `yaml.safe_load`/`safe_dump`). The optional
|
|
234
|
+
Pickle format is not: like any pickle-based tool, deserializing it can execute arbitrary
|
|
235
|
+
code, so only load pickle files from sources you trust. See
|
|
236
|
+
[SECURITY.md](https://github.com/Gnpd/openmodels/blob/main/SECURITY.md) for details and
|
|
237
|
+
how to report a vulnerability.
|
|
238
|
+
|
|
222
239
|
## License
|
|
223
240
|
|
|
224
|
-
This project is licensed under the MIT License. See the [LICENSE](https://github.com/
|
|
241
|
+
This project is licensed under the MIT License. See the [LICENSE](https://github.com/Gnpd/openmodels/blob/main/LICENSE) file for details.
|
|
225
242
|
|
|
226
243
|
## Changelog
|
|
227
244
|
|
|
228
|
-
For a detailed changelog, please see the [CHANGELOG.md](https://github.com/
|
|
245
|
+
For a detailed changelog, please see the [CHANGELOG.md](https://github.com/Gnpd/openmodels/blob/main/CHANGELOG.md) file.
|
|
229
246
|
|
|
230
247
|
## Support
|
|
231
248
|
|
|
232
|
-
If you encounter any issues or have questions, please [file an issue](https://github.com/
|
|
249
|
+
If you encounter any issues or have questions, please [file an issue](https://github.com/Gnpd/openmodels/issues/new) on our GitHub repository.
|
|
233
250
|
|
|
234
251
|
We're always looking to improve OpenModels. If you have any suggestions or feature requests, please let us know!
|
|
235
252
|
|
|
@@ -238,4 +255,3 @@ We're always looking to improve OpenModels. If you have any suggestions or featu
|
|
|
238
255
|
During the revision and design of OpenModels, we came across the discontinued project [**sklearn-json**](https://github.com/mlrequest/sklearn-json).
|
|
239
256
|
Although no longer maintained, it provided valuable ideas — particularly around testing approaches — that inspired parts of our implementation.
|
|
240
257
|
We would like to acknowledge and thank its authors for their earlier contributions to open model serialization efforts in the scikit-learn ecosystem.
|
|
241
|
-
|
|
@@ -8,7 +8,8 @@ through a plugin-based system.
|
|
|
8
8
|
Main components:
|
|
9
9
|
- SerializationManager: Coordinates the serialization and deserialization process.
|
|
10
10
|
- SklearnSerializer: Handles serialization for scikit-learn models.
|
|
11
|
-
- JSONConverter, PickleConverter: Convert between dict and
|
|
11
|
+
- JSONConverter, PickleConverter, MsgpackConverter, YAMLConverter: Convert between dict and
|
|
12
|
+
specific formats.
|
|
12
13
|
- FormatRegistry: Manages available format converters.
|
|
13
14
|
|
|
14
15
|
Example usage:
|
|
@@ -36,12 +37,16 @@ For more advanced usage and custom serializers or converters, refer to the docum
|
|
|
36
37
|
|
|
37
38
|
from .core import SerializationManager
|
|
38
39
|
from .serializers import SklearnSerializer
|
|
39
|
-
from .converters import JSONConverter, PickleConverter
|
|
40
|
+
from .converters import JSONConverter, PickleConverter, MsgpackConverter, YAMLConverter
|
|
40
41
|
from .format_registry import FormatRegistry
|
|
41
42
|
|
|
42
|
-
# Register the
|
|
43
|
+
# Register the built-in format converters. MsgpackConverter/YAMLConverter only import their
|
|
44
|
+
# underlying library lazily, inside each method - so registering them here doesn't require
|
|
45
|
+
# 'msgpack'/'PyYAML' to be installed; only actually using format_name="msgpack"/"yaml" does.
|
|
43
46
|
FormatRegistry.register("json", JSONConverter)
|
|
44
47
|
FormatRegistry.register("pickle", PickleConverter)
|
|
48
|
+
FormatRegistry.register("msgpack", MsgpackConverter)
|
|
49
|
+
FormatRegistry.register("yaml", YAMLConverter)
|
|
45
50
|
|
|
46
51
|
__all__ = [
|
|
47
52
|
"FormatRegistry",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Converters module for the OpenModels library.
|
|
3
|
+
|
|
4
|
+
This module provides converters for different serialization formats.
|
|
5
|
+
Currently, it includes converters for JSON, pickle, MessagePack, and YAML formats.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .json_converter import JSONConverter
|
|
9
|
+
from .pickle_converter import PickleConverter
|
|
10
|
+
from .msgpack_converter import MsgpackConverter
|
|
11
|
+
from .yaml_converter import YAMLConverter
|
|
12
|
+
|
|
13
|
+
__all__ = ["JSONConverter", "PickleConverter", "MsgpackConverter", "YAMLConverter"]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MessagePack converter for the OpenModels library.
|
|
3
|
+
|
|
4
|
+
This module provides a converter for serializing to and from MessagePack format - a
|
|
5
|
+
binary format with the same data model as JSON (no code execution risk on load), useful when
|
|
6
|
+
JSON's text encoding is too slow/large for a given model. Requires the optional ``msgpack``
|
|
7
|
+
package (``pip install openmodels[msgpack]``).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from typing import Any, Dict
|
|
11
|
+
|
|
12
|
+
from openmodels.protocols import FormatConverter
|
|
13
|
+
|
|
14
|
+
_INSTALL_HINT = (
|
|
15
|
+
"The 'msgpack' package is required for format_name=\"msgpack\". "
|
|
16
|
+
"Install it with: pip install openmodels[msgpack]"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MsgpackConverter(FormatConverter):
|
|
21
|
+
"""
|
|
22
|
+
Converter for MessagePack format.
|
|
23
|
+
|
|
24
|
+
This class provides static methods to convert between dictionary
|
|
25
|
+
representations and MessagePack byte strings.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
is_binary = True
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def serialize_to_format(data: Dict[str, Any]) -> bytes:
|
|
32
|
+
"""
|
|
33
|
+
Convert a dictionary to a MessagePack byte string.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
data : Dict[str, Any]
|
|
38
|
+
The dictionary to convert.
|
|
39
|
+
|
|
40
|
+
Returns
|
|
41
|
+
-------
|
|
42
|
+
bytes
|
|
43
|
+
The MessagePack byte string representation of the data.
|
|
44
|
+
"""
|
|
45
|
+
try:
|
|
46
|
+
import msgpack
|
|
47
|
+
except ImportError as e:
|
|
48
|
+
raise ImportError(_INSTALL_HINT) from e
|
|
49
|
+
|
|
50
|
+
if not isinstance(data, dict):
|
|
51
|
+
raise TypeError("Data must be a dictionary.")
|
|
52
|
+
return msgpack.packb(data, use_bin_type=True)
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def deserialize_from_format(formatted_data: bytes) -> Dict[str, Any]:
|
|
56
|
+
"""
|
|
57
|
+
Convert a MessagePack byte string to a dictionary.
|
|
58
|
+
|
|
59
|
+
Parameters
|
|
60
|
+
----------
|
|
61
|
+
formatted_data : bytes
|
|
62
|
+
The MessagePack byte string to convert.
|
|
63
|
+
|
|
64
|
+
Returns
|
|
65
|
+
-------
|
|
66
|
+
Dict[str, Any]
|
|
67
|
+
The dictionary representation of the MessagePack data.
|
|
68
|
+
"""
|
|
69
|
+
try:
|
|
70
|
+
import msgpack
|
|
71
|
+
except ImportError as e:
|
|
72
|
+
raise ImportError(_INSTALL_HINT) from e
|
|
73
|
+
|
|
74
|
+
return msgpack.unpackb(formatted_data, raw=False)
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
Pickle converter for the OpenModels library.
|
|
3
3
|
|
|
4
4
|
This module provides a converter for serializing to and from pickle format.
|
|
5
|
+
|
|
6
|
+
.. warning::
|
|
7
|
+
Unpickling can execute arbitrary code. Only deserialize pickle data from sources you
|
|
8
|
+
trust - see SECURITY.md. Prefer the JSON format for data from untrusted sources.
|
|
5
9
|
"""
|
|
6
10
|
|
|
7
11
|
import pickle
|
|
@@ -16,8 +20,15 @@ class PickleConverter(FormatConverter):
|
|
|
16
20
|
|
|
17
21
|
This class provides static methods to convert between dictionary
|
|
18
22
|
representations and pickle byte strings.
|
|
23
|
+
|
|
24
|
+
.. warning::
|
|
25
|
+
``deserialize_from_format`` calls ``pickle.loads()``, which can execute arbitrary
|
|
26
|
+
code as part of deserialization. Only use this converter with pickle data from
|
|
27
|
+
sources you trust.
|
|
19
28
|
"""
|
|
20
29
|
|
|
30
|
+
is_binary = True
|
|
31
|
+
|
|
21
32
|
@staticmethod
|
|
22
33
|
def serialize_to_format(data: Dict[str, Any]) -> bytes:
|
|
23
34
|
"""
|
|
@@ -54,5 +65,10 @@ class PickleConverter(FormatConverter):
|
|
|
54
65
|
------
|
|
55
66
|
pickle.UnpicklingError
|
|
56
67
|
If the input bytes cannot be unpickled.
|
|
68
|
+
|
|
69
|
+
Warns
|
|
70
|
+
-----
|
|
71
|
+
Unpickling can execute arbitrary code. Only call this with pickle data from a
|
|
72
|
+
source you trust.
|
|
57
73
|
"""
|
|
58
74
|
return pickle.loads(formatted_data)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""
|
|
2
|
+
YAML converter for the OpenModels library.
|
|
3
|
+
|
|
4
|
+
This module provides a converter for serializing to and from YAML format - useful when a
|
|
5
|
+
human wants to read, hand-edit, or diff a model's serialized ``metadata`` cleanly. Requires
|
|
6
|
+
the optional ``PyYAML`` package (``pip install openmodels[yaml]``).
|
|
7
|
+
|
|
8
|
+
.. warning::
|
|
9
|
+
This converter only ever calls ``yaml.safe_load``/``yaml.safe_dump``, never ``yaml.load``/
|
|
10
|
+
``yaml.dump`` with the default (unsafe) ``Loader``/``Dumper``, which support a
|
|
11
|
+
``!!python/object`` tag that can construct arbitrary Python objects on load - YAML's
|
|
12
|
+
equivalent of pickle's arbitrary-code-execution risk.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from typing import Any, Dict
|
|
16
|
+
|
|
17
|
+
from openmodels.protocols import FormatConverter
|
|
18
|
+
|
|
19
|
+
_INSTALL_HINT = (
|
|
20
|
+
"The 'PyYAML' package is required for format_name=\"yaml\". "
|
|
21
|
+
"Install it with: pip install openmodels[yaml]"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class YAMLConverter(FormatConverter):
|
|
26
|
+
"""
|
|
27
|
+
Converter for YAML format.
|
|
28
|
+
|
|
29
|
+
This class provides static methods to convert between dictionary
|
|
30
|
+
representations and YAML strings, using only the safe loader/dumper.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
is_binary = False
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def serialize_to_format(data: Dict[str, Any]) -> str:
|
|
37
|
+
"""
|
|
38
|
+
Convert a dictionary to a YAML string.
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
data : Dict[str, Any]
|
|
43
|
+
The dictionary to convert.
|
|
44
|
+
|
|
45
|
+
Returns
|
|
46
|
+
-------
|
|
47
|
+
str
|
|
48
|
+
The YAML string representation of the data.
|
|
49
|
+
"""
|
|
50
|
+
try:
|
|
51
|
+
import yaml
|
|
52
|
+
except ImportError as e:
|
|
53
|
+
raise ImportError(_INSTALL_HINT) from e
|
|
54
|
+
|
|
55
|
+
if not isinstance(data, dict):
|
|
56
|
+
raise TypeError("Data must be a dictionary.")
|
|
57
|
+
# sort_keys=False keeps the natural field order (estimator_class, params, ...,
|
|
58
|
+
# metadata) instead of alphabetizing it - readability is the whole point of this format.
|
|
59
|
+
return yaml.safe_dump(data, sort_keys=False)
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def deserialize_from_format(formatted_data: str) -> Dict[str, Any]:
|
|
63
|
+
"""
|
|
64
|
+
Convert a YAML string to a dictionary.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
formatted_data : str
|
|
69
|
+
The YAML string to convert.
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
-------
|
|
73
|
+
Dict[str, Any]
|
|
74
|
+
The dictionary representation of the YAML data.
|
|
75
|
+
"""
|
|
76
|
+
try:
|
|
77
|
+
import yaml
|
|
78
|
+
except ImportError as e:
|
|
79
|
+
raise ImportError(_INSTALL_HINT) from e
|
|
80
|
+
|
|
81
|
+
return yaml.safe_load(formatted_data)
|