Skip to content

Commit

Permalink
Add support for adding tags from filepaths (#3230)
Browse files Browse the repository at this point in the history
* fix

* fix

* fix

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

* addressing comments

---------

Co-authored-by: Ali Soylemezoglu <[email protected]>
  • Loading branch information
acsoylemezoglu and Ali Soylemezoglu authored Aug 8, 2024
1 parent 9da1610 commit f33b580
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions scripts/azureml-assets/azureml/assets/model/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from azure.ai.ml import load_model, MLClient
from azure.ai.ml._utils._registry_utils import get_asset_body_for_registry_storage
from azureml.assets.util import logger
from azureml.assets.util.util import resolve_from_file_for_asset
from azureml.assets.config import PathType
from azureml.assets.model.download_utils import CopyUpdater, copy_azure_artifacts, download_git_model
from azureml.assets.deployment_config import AssetVersionUpdate
Expand Down Expand Up @@ -65,6 +66,9 @@ def __init__(self, spec_path, model_config, registry_name, temp_dir, copy_update
self._model = load_model(spec_path)
self._model.description = model_config.description
self._model.type = model_config.type.value
if self._model.tags:
self._model.tags = {k: resolve_from_file_for_asset(self._model_config, v)
for k, v in self._model.tags.items()}
except Exception as e:
logger.error(f"Error in loading model spec file at {spec_path}: {e}")
return False
Expand Down
3 changes: 2 additions & 1 deletion scripts/azureml-assets/azureml/assets/publish_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,15 @@ def update_asset_metadata(asset: AssetConfig, ml_client: MLClient, allow_no_op_u
spec_path = asset.spec_with_path
model_config = asset.extra_config_as_object()

# get tags to update from model spec file
tags_to_update = None
try:
with open(spec_path) as f:
model_spec = YAML().load(f)
tags = model_spec.get("tags", {})
properties = model_spec.get("properties", {})

tags = {k: util.util.resolve_from_file(model_config._append_to_file_path(v)) for k, v in tags.items()}

# convert tags, properties value to string
tags = stringify_dictionary(tags)
properties = stringify_dictionary(properties)
Expand Down
8 changes: 8 additions & 0 deletions scripts/azureml-assets/azureml/assets/update_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Update spec files."""

import argparse
import os
import sys
from git import Repo
from pathlib import Path
Expand All @@ -14,6 +15,7 @@
import azureml.assets as assets
import azureml.assets.util as util
from azureml.assets.util import logger
from azureml.assets.util.util import resolve_from_file_for_asset


def create_template_data(asset_config: assets.AssetConfig, release_directory_root: Path = None, version: str = None,
Expand Down Expand Up @@ -115,6 +117,12 @@ def update(asset_config: assets.AssetConfig, release_directory_root: Path = None
# Replace description in spec
contents_yaml['description'] = LiteralScalarString(description)

if 'tags' in contents_yaml:
unresolved_tags = contents_yaml['tags']
contents_yaml['tags'] = {k: (LiteralScalarString(resolve_from_file_for_asset(asset_config, v))
if os.path.isfile(asset_config._append_to_file_path(v)) else v)
for k, v in unresolved_tags.items()}

# Write spec
if output_file == "-":
logger.print(contents)
Expand Down
25 changes: 25 additions & 0 deletions scripts/azureml-assets/azureml/assets/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import difflib
import filecmp
import os
import re
import shutil
from pathlib import Path
Expand Down Expand Up @@ -93,6 +94,30 @@ def _are_files_equal_ignore_eol(file1: Path, file2: Path) -> bool:
return True


def resolve_from_file(value: str):
"""Resolve the value from a file if it is a file, otherwise returns the value.
Args:
value (str): value to try and resolve
"""
if os.path.isfile(value):
with open(value, 'r') as f:
content = f.read()
return content
else:
return value


def resolve_from_file_for_asset(asset: assets.AssetConfig, value: str):
"""Resolve the value from a file for an asset if it is a file, otherwise returns the value.
Args:
asset (AssetConfig): the asset to try and resolve the value for
value (str): value to try and resolve
"""
return resolve_from_file(asset._append_to_file_path(value))


def copy_replace_dir(source: Path, dest: Path, paths: List[Path] = None):
"""Copy a directory tree, replacing any existing one.
Expand Down

0 comments on commit f33b580

Please sign in to comment.