1111from mlflow_export_import import utils , click_doc
1212
1313class ModelExporter ():
14- def __init__ (self , mlflow_client , export_source_tags = False , notebook_formats = None , stages = None , export_run = True ):
14+ def __init__ (self , mlflow_client , export_source_tags = False , notebook_formats = None , stages = None , versions = None , export_run = True ):
1515 """
1616 :param mlflow_client: MLflow client or if None create default client.
1717 :param export_source_tags: Export source run metadata tags.
1818 :param notebook_formats: List of notebook formats to export. Values are SOURCE, HTML, JUPYTER or DBC.
1919 :param stages: Stages to export. Default is all stages. Values are Production, Staging, Archived and None.
20+ :param versions: Versions to export. Default is all versions. Values are valid integer numbers.
2021 :param export_run: Export the run that generated a registered model's version.
2122 """
2223 self .mlflow_client = mlflow_client
2324 self .http_client = MlflowHttpClient ()
2425 self .run_exporter = RunExporter (self .mlflow_client , export_source_tags = export_source_tags , notebook_formats = notebook_formats )
2526 self .stages = self ._normalize_stages (stages )
27+ self .versions = self ._normalize_versions (versions )
2628 self .export_run = export_run
2729
2830 def export_model (self , model_name , output_dir ):
@@ -50,6 +52,8 @@ def _export_model(self, model_name, output_dir):
5052 for vr in versions :
5153 if len (self .stages ) > 0 and not vr .current_stage .lower () in self .stages :
5254 continue
55+ if len (self .versions ) > 0 and vr .version not in self .versions :
56+ continue
5357 run_id = vr .run_id
5458 opath = os .path .join (output_dir ,run_id )
5559 opath = opath .replace ("dbfs:" , "/dbfs" )
@@ -89,14 +93,26 @@ def _normalize_stages(self, stages):
8993 print (f"WARNING: stage '{ stage } ' must be one of: { model_version_stages .ALL_STAGES } " )
9094 return stages
9195
96+ def _normalize_versions (self , versions ):
97+ if versions is None :
98+ return []
99+ if isinstance (versions , str ):
100+ versions = versions .split ("," )
101+ for version in versions :
102+ try :
103+ int (version )
104+ except ValueError :
105+ print (f"WARNING: version '{ version } ' must be a valid number" )
106+ return versions
107+
92108@click .command ()
93- @click .option ("--model" ,
94- help = "Registered model name." ,
109+ @click .option ("--model" ,
110+ help = "Registered model name." ,
95111 type = str ,
96112 required = True
97113)
98- @click .option ("--output-dir" ,
99- help = "Output directory." ,
114+ @click .option ("--output-dir" ,
115+ help = "Output directory." ,
100116 type = str ,
101117 required = True
102118)
@@ -106,24 +122,29 @@ def _normalize_stages(self, stages):
106122 default = False ,
107123 show_default = True
108124)
109- @click .option ("--notebook-formats" ,
110- help = click_doc .notebook_formats ,
125+ @click .option ("--notebook-formats" ,
126+ help = click_doc .notebook_formats ,
111127 type = str ,
112- default = "" ,
128+ default = "" ,
113129 show_default = True
114130)
115- @click .option ("--stages" ,
116- help = click_doc .model_stages ,
131+ @click .option ("--stages" ,
132+ help = click_doc .model_stages ,
133+ type = str ,
134+ required = False
135+ )
136+ @click .option ("--versions" ,
137+ help = click_doc .model_versions ,
117138 type = str ,
118139 required = False
119140)
120141
121- def main (model , output_dir , export_source_tags , notebook_formats , stages ):
142+ def main (model , output_dir , export_source_tags , notebook_formats , stages , versions ):
122143 print ("Options:" )
123144 for k ,v in locals ().items ():
124145 print (f" { k } : { v } " )
125146 client = mlflow .tracking .MlflowClient ()
126- exporter = ModelExporter (client , export_source_tags = export_source_tags , notebook_formats = utils .string_to_list (notebook_formats ), stages = stages )
147+ exporter = ModelExporter (client , export_source_tags = export_source_tags , notebook_formats = utils .string_to_list (notebook_formats ), stages = stages , versions = versions )
127148 exporter .export_model (model , output_dir )
128149
129150if __name__ == "__main__" :
0 commit comments