Skip to content

Commit

Permalink
core: re-use metadata files if found in directory (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: tarilabs <[email protected]>
  • Loading branch information
tarilabs authored Aug 22, 2024
1 parent 2d068f4 commit 3c87873
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
23 changes: 18 additions & 5 deletions omlmd/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import urllib.request
from collections.abc import Sequence
Expand All @@ -11,6 +12,9 @@
from omlmd.provider import OMLMDRegistry


logger = logging.getLogger(__name__)


def download_file(uri: str):
file_name = os.path.basename(uri)
urllib.request.urlretrieve(uri, file_name)
Expand Down Expand Up @@ -57,17 +61,25 @@ def push(
model_format_name=model_format_name,
model_format_version=model_format_version,
)
owns_meta_files = True
if isinstance(path, str):
path = Path(path)

json_meta = path.parent / "model_metadata.omlmd.json"
yaml_meta = path.parent / "model_metadata.omlmd.yaml"
if (p := json_meta).exists() or (p := yaml_meta).exists():
if model_metadata.is_empty() and json_meta.exists() and yaml_meta.exists():
owns_meta_files = False
logger.warning("No metadata supplied, but reusing md files found in path.")
logger.debug(f"{json_meta}, {yaml_meta}")
with open(json_meta, "r") as f:
model_metadata = ModelMetadata.from_json(f.read())
elif (p := json_meta).exists() or (p := yaml_meta).exists():
raise RuntimeError(
f"File '{p}' already exists. Aborting TODO: demonstrator."
)
json_meta.write_text(model_metadata.to_json())
yaml_meta.write_text(model_metadata.to_yaml())
else:
json_meta.write_text(model_metadata.to_json())
yaml_meta.write_text(model_metadata.to_yaml())

manifest_cfg = f"{json_meta}:application/x-config"
files = [
Expand All @@ -86,8 +98,9 @@ def push(
self.notify_listeners(PushEvent(target, model_metadata))
return result
finally:
json_meta.unlink()
yaml_meta.unlink()
if owns_meta_files:
json_meta.unlink()
yaml_meta.unlink()

def pull(
self, target: str, outdir: Path | str, media_types: Sequence[str] | None = None
Expand Down
3 changes: 3 additions & 0 deletions omlmd/model_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def to_annotations_dict(self) -> dict[str, str]:
v
) # post-fix "+json" for OCI annotation which is a str representing a json
return result

def is_empty(self) -> bool:
return all(getattr(self, f.name) is None for f in fields(ModelMetadata) if f.name != "customProperties") and not self.customProperties

@staticmethod
def from_json(json_str: str) -> "ModelMetadata":
Expand Down
23 changes: 23 additions & 0 deletions tests/test_omlmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,26 @@ def test_from_dict():
customProperties={"accuracy": 0.987},
)
assert ModelMetadata.from_dict(data) == md


def test_is_empty():
md = ModelMetadata(
name="mnist",
description="Lorem ipsum",
author="John Doe",
customProperties={"accuracy": 0.987},
)
assert not md.is_empty()

md = ModelMetadata()
assert md.is_empty()

md = ModelMetadata(
customProperties={"accuracy": 0.987},
)
assert not md.is_empty()

md = ModelMetadata(
name="mnist",
)
assert not md.is_empty()

0 comments on commit 3c87873

Please sign in to comment.