Skip to content

Commit 1d701f6

Browse files
committed
feat: add Python SDK for ModelPack spec types and validation
Signed-off-by: pradhyum6144 <pradhyum314@gmail.com>
1 parent cf78357 commit 1d701f6

13 files changed

Lines changed: 1760 additions & 0 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
# Dependency directories (remove the comment below to include it)
1212
vendor/
13+
14+
# Python
15+
__pycache__/
16+
*.pyc
17+
*.egg-info/
18+
.venv/
19+
.pytest_cache/
1320
.idea
1421
.vscode
1522
.cache

specs-python/modelpack/__init__.py

Whitespace-only changes.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright 2025 The CNCF ModelPack Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""ModelPack Python SDK - CNCF standard for packaging and distributing AI models."""
16+
17+
from modelpack.v1.config import (
18+
Model,
19+
ModelCapabilities,
20+
ModelConfig,
21+
ModelDescriptor,
22+
ModelFS,
23+
Modality,
24+
)
25+
from modelpack.v1.annotations import (
26+
ANNOTATION_FILEPATH,
27+
ANNOTATION_FILE_METADATA,
28+
ANNOTATION_MEDIA_TYPE_UNTESTED,
29+
FileMetadata,
30+
)
31+
from modelpack.v1.mediatype import (
32+
ARTIFACT_TYPE_MODEL_MANIFEST,
33+
MEDIA_TYPE_MODEL_CONFIG,
34+
MEDIA_TYPE_MODEL_WEIGHT_RAW,
35+
MEDIA_TYPE_MODEL_WEIGHT,
36+
MEDIA_TYPE_MODEL_WEIGHT_GZIP,
37+
MEDIA_TYPE_MODEL_WEIGHT_ZSTD,
38+
MEDIA_TYPE_MODEL_WEIGHT_CONFIG_RAW,
39+
MEDIA_TYPE_MODEL_WEIGHT_CONFIG,
40+
MEDIA_TYPE_MODEL_WEIGHT_CONFIG_GZIP,
41+
MEDIA_TYPE_MODEL_WEIGHT_CONFIG_ZSTD,
42+
MEDIA_TYPE_MODEL_DOC_RAW,
43+
MEDIA_TYPE_MODEL_DOC,
44+
MEDIA_TYPE_MODEL_DOC_GZIP,
45+
MEDIA_TYPE_MODEL_DOC_ZSTD,
46+
MEDIA_TYPE_MODEL_CODE_RAW,
47+
MEDIA_TYPE_MODEL_CODE,
48+
MEDIA_TYPE_MODEL_CODE_GZIP,
49+
MEDIA_TYPE_MODEL_CODE_ZSTD,
50+
MEDIA_TYPE_MODEL_DATASET_RAW,
51+
MEDIA_TYPE_MODEL_DATASET,
52+
MEDIA_TYPE_MODEL_DATASET_GZIP,
53+
MEDIA_TYPE_MODEL_DATASET_ZSTD,
54+
)
55+
from modelpack.v1.validator import validate_config
56+
57+
__all__ = [
58+
"Model",
59+
"ModelCapabilities",
60+
"ModelConfig",
61+
"ModelDescriptor",
62+
"ModelFS",
63+
"Modality",
64+
"FileMetadata",
65+
"ANNOTATION_FILEPATH",
66+
"ANNOTATION_FILE_METADATA",
67+
"ANNOTATION_MEDIA_TYPE_UNTESTED",
68+
"ARTIFACT_TYPE_MODEL_MANIFEST",
69+
"MEDIA_TYPE_MODEL_CONFIG",
70+
"MEDIA_TYPE_MODEL_WEIGHT_RAW",
71+
"MEDIA_TYPE_MODEL_WEIGHT",
72+
"MEDIA_TYPE_MODEL_WEIGHT_GZIP",
73+
"MEDIA_TYPE_MODEL_WEIGHT_ZSTD",
74+
"MEDIA_TYPE_MODEL_WEIGHT_CONFIG_RAW",
75+
"MEDIA_TYPE_MODEL_WEIGHT_CONFIG",
76+
"MEDIA_TYPE_MODEL_WEIGHT_CONFIG_GZIP",
77+
"MEDIA_TYPE_MODEL_WEIGHT_CONFIG_ZSTD",
78+
"MEDIA_TYPE_MODEL_DOC_RAW",
79+
"MEDIA_TYPE_MODEL_DOC",
80+
"MEDIA_TYPE_MODEL_DOC_GZIP",
81+
"MEDIA_TYPE_MODEL_DOC_ZSTD",
82+
"MEDIA_TYPE_MODEL_CODE_RAW",
83+
"MEDIA_TYPE_MODEL_CODE",
84+
"MEDIA_TYPE_MODEL_CODE_GZIP",
85+
"MEDIA_TYPE_MODEL_CODE_ZSTD",
86+
"MEDIA_TYPE_MODEL_DATASET_RAW",
87+
"MEDIA_TYPE_MODEL_DATASET",
88+
"MEDIA_TYPE_MODEL_DATASET_GZIP",
89+
"MEDIA_TYPE_MODEL_DATASET_ZSTD",
90+
"validate_config",
91+
]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2025 The CNCF ModelPack Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Annotation constants and types matching specs-go/v1/annotations.go."""
16+
17+
from __future__ import annotations
18+
19+
from dataclasses import dataclass
20+
from datetime import datetime
21+
22+
# Annotation key for the file path of the layer.
23+
ANNOTATION_FILEPATH = "org.cncf.model.filepath"
24+
25+
# Annotation key for the file metadata of the layer.
26+
ANNOTATION_FILE_METADATA = "org.cncf.model.file.metadata+json"
27+
28+
# Annotation key for file media type untested flag of the layer.
29+
ANNOTATION_MEDIA_TYPE_UNTESTED = "org.cncf.model.file.mediatype.untested"
30+
31+
32+
@dataclass
33+
class FileMetadata:
34+
"""Represents the metadata of a file.
35+
36+
Mirrors the Go FileMetadata struct in specs-go/v1/annotations.go.
37+
"""
38+
39+
name: str = ""
40+
mode: int = 0
41+
uid: int = 0
42+
gid: int = 0
43+
size: int = 0
44+
mod_time: datetime | None = None
45+
typeflag: int = 0
46+
47+
def to_dict(self) -> dict:
48+
"""Serialize to a dict matching the JSON field names."""
49+
d: dict = {
50+
"name": self.name,
51+
"mode": self.mode,
52+
"uid": self.uid,
53+
"gid": self.gid,
54+
"size": self.size,
55+
"typeflag": self.typeflag,
56+
}
57+
if self.mod_time is not None:
58+
d["mtime"] = self.mod_time.isoformat()
59+
return d
60+
61+
@classmethod
62+
def from_dict(cls, data: dict) -> FileMetadata:
63+
"""Deserialize from a dict with JSON field names."""
64+
mod_time = None
65+
if "mtime" in data:
66+
mod_time = datetime.fromisoformat(data["mtime"])
67+
return cls(
68+
name=data.get("name", ""),
69+
mode=data.get("mode", 0),
70+
uid=data.get("uid", 0),
71+
gid=data.get("gid", 0),
72+
size=data.get("size", 0),
73+
mod_time=mod_time,
74+
typeflag=data.get("typeflag", 0),
75+
)

0 commit comments

Comments
 (0)