@@ -22,9 +22,10 @@ def get_current_workspace() -> Workspace:
2222 return experiment .workspace
2323
2424
25- def _get_model_by_build_id (
25+ def get_model_by_tag (
2626 model_name : str ,
27- build_id : str ,
27+ tag_name : str ,
28+ tag_value : str ,
2829 aml_workspace : Workspace = None
2930) -> AMLModel :
3031 """
@@ -34,52 +35,46 @@ def _get_model_by_build_id(
3435 Parameters:
3536 aml_workspace (Workspace): aml.core Workspace that the model lives.
3637 model_name (str): name of the model we are looking for
37- build_id (str): the build id the model was registered under.
38+ tag (str): the tag value the model was registered under.
3839
3940 Return:
4041 A single aml model from the workspace that matches the name and tag.
4142 """
42- # Validate params. cannot be None.
43- if model_name is None :
44- raise ValueError ("model_name[:str] is required" )
45- if build_id is None :
46- raise ValueError ("build_id[:str] is required" )
47- if aml_workspace is None :
48- aml_workspace = get_current_workspace ()
43+ try :
44+ # Validate params. cannot be None.
45+ if model_name is None :
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" )
51+ if aml_workspace is None :
52+ aml_workspace = get_current_workspace ()
4953
50- # get model by tag.
51- model_list = AMLModel .list (
52- aml_workspace , name = model_name ,
53- tags = [["BuildId" , build_id ]], latest = True
54- )
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+ )
5559
56- # latest should only return 1 model, but if it does, then maybe
57- # internal code was accidentally changed or the source code has changed.
58- should_not_happen = ("THIS SHOULD NOT HAPPEN: found more than one model "
59- "for the latest with {{model_name: {model_name},"
60- "BuildId: {build_id}. Models found: {model_list}}}" )\
61- .format (model_name = model_name , build_id = build_id ,
62- model_list = model_list )
63- if len (model_list ) > 1 :
64- raise ValueError (should_not_happen )
60+ # latest should only return 1 model, but if it does,
61+ # 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 )
6571
66- return model_list
67-
68-
69- def get_model_by_build_id (
70- model_name : str ,
71- build_id : str ,
72- aml_workspace : Workspace = None
73- ) -> AMLModel :
74- """
75- Wrapper function for get_model_by_id that throws an error if model is none
76- """
77- model_list = _get_model_by_build_id (model_name , build_id , aml_workspace )
78-
79- if model_list :
80- return model_list [0 ]
81-
82- no_model_found = ("Model not found with model_name: {model_name} "
83- "BuildId: {build_id}." )\
84- .format (model_name = model_name , build_id = build_id )
85- raise Exception (no_model_found )
72+ if len (model_list ) > 1 :
73+ raise ValueError (should_not_happen )
74+ if len (model_list ) == 1 :
75+ return model_list [0 ]
76+ else :
77+ print (no_model_found )
78+ return None
79+ except Exception :
80+ raise
0 commit comments