@@ -22,20 +22,20 @@ def get_current_workspace() -> Workspace:
22
22
return experiment .workspace
23
23
24
24
25
- def get_model_by_tag (
25
+ def get_latest_model (
26
26
model_name : str ,
27
- tag_name : str ,
28
- tag_value : str ,
27
+ tag_name : str = None ,
28
+ tag_value : str = None ,
29
29
aml_workspace : Workspace = None
30
30
) -> AMLModel :
31
31
"""
32
32
Retrieves and returns the latest model from the workspace
33
- by its name and tag.
33
+ by its name and (optional) tag.
34
34
35
35
Parameters:
36
36
aml_workspace (Workspace): aml.core Workspace that the model lives.
37
37
model_name (str): name of the model we are looking for
38
- tag (str): the tag value the model was registered under.
38
+ (optional) tag (str): the tag value & name the model was registered under.
39
39
40
40
Return:
41
41
A single aml model from the workspace that matches the name and tag.
@@ -44,37 +44,44 @@ def get_model_by_tag(
44
44
# Validate params. cannot be None.
45
45
if model_name is None :
46
46
raise ValueError ("model_name[:str] is required" )
47
- if tag_name is None :
48
- raise ValueError ("tag_name[:str] is required" )
49
- if tag_value is None :
50
- raise ValueError ("tag[:str] is required" )
47
+
51
48
if aml_workspace is None :
49
+ print ("No workspace defined - using current experiment workspace." )
52
50
aml_workspace = get_current_workspace ()
53
51
54
- # get model by tag.
55
- model_list = AMLModel .list (
56
- aml_workspace , name = model_name ,
57
- tags = [[tag_name , tag_value ]], latest = True
58
- )
52
+ model_list = None
53
+ tag_ext = ""
54
+
55
+ # Get lastest model
56
+ # True: by name and tags
57
+ if tag_name is not None and tag_value is not None :
58
+ model_list = AMLModel .list (
59
+ aml_workspace , name = model_name ,
60
+ tags = [[tag_name , tag_value ]], latest = True
61
+ )
62
+ tag_ext = f"tag_name: { tag_name } , tag_value: { tag_value } ."
63
+ # False: Only by name
64
+ else :
65
+ model_list = AMLModel .list (
66
+ aml_workspace , name = model_name , latest = True )
59
67
60
68
# latest should only return 1 model, but if it does,
61
69
# then maybe sdk or source code changed.
62
- should_not_happen = ("Found more than one model "
63
- "for the latest with {{tag_name: {tag_name},"
64
- "tag_value: {tag_value}. "
65
- "Models found: {model_list}}}" )\
66
- .format (tag_name = tag_name , tag_value = tag_value ,
67
- model_list = model_list )
68
- no_model_found = ("No Model found with {{tag_name: {tag_name} ,"
69
- "tag_value: {tag_value}.}}" )\
70
- .format (tag_name = tag_name , tag_value = tag_value )
70
+
71
+ # define the error messages
72
+ too_many_model_message = ("Found more than one latest model. "
73
+ f"Models found: { model_list } . "
74
+ f"{ tag_ext } " )
75
+
76
+ no_model_found_message = (f"No Model found with name: { model_name } . "
77
+ f"{ tag_ext } " )
71
78
72
79
if len (model_list ) > 1 :
73
- raise ValueError (should_not_happen )
80
+ raise ValueError (too_many_model_message )
74
81
if len (model_list ) == 1 :
75
82
return model_list [0 ]
76
83
else :
77
- print (no_model_found )
84
+ print (no_model_found_message )
78
85
return None
79
86
except Exception :
80
87
raise
0 commit comments