@@ -42,6 +42,25 @@ def upload_model(model_file, name, tags=None):
4242 print_models ([new_model ], headers = False )
4343
4444
45+ def add_automl_model (model_ref , name , tags = None ):
46+ """Add an AutoML tflite model file to the project and publish it."""
47+ # Create the model object
48+ model_source = ml .TFLiteAutoMlSource (model_ref )
49+ model = ml .Model (
50+ display_name = name ,
51+ model_format = ml .TFLiteFormat (model_source = model_source ))
52+ if tags is not None :
53+ model .tags = tags
54+
55+ # Add the model to your Firebase project and publish it
56+ new_model = ml .create_model (model )
57+ new_model .wait_for_unlocked ()
58+ ml .publish_model (new_model .model_id )
59+
60+ print ('Model uploaded and published:' )
61+ print_models ([new_model ], headers = False )
62+
63+
4564def list_models (filter_exp = '' ):
4665 """List the models in the project."""
4766 models = ml .list_models (list_filter = filter_exp ).iterate_all ()
@@ -123,11 +142,17 @@ def main():
123142 dest = 'command' , required = True , metavar = 'command' )
124143
125144 new_parser = subparsers .add_parser (
126- 'new' , help = 'upload a tflite model to your project' )
127- new_parser .add_argument (
128- 'model_file' , type = str , help = 'path to the tflite file' )
145+ 'new' , help = 'upload a tflite model file or AutoML model reference to'
146+ ' your project' )
129147 new_parser .add_argument (
130148 'name' , type = str , help = 'display name for the new model' )
149+ new_source_group = new_parser .add_mutually_exclusive_group (required = True )
150+ new_source_group .add_argument (
151+ '-f' , '--file' , type = str , help = 'path to the tflite file' )
152+ new_source_group .add_argument (
153+ '-a' , '--automl' , type = str , help = 'AutoML model reference (e.g. projects/'
154+ '12345678/locations/us-central1/models/'
155+ 'ICN1234567890)' )
131156 new_parser .add_argument (
132157 '-t' , '--tags' , type = str , help = 'comma-separated list of tags' )
133158
@@ -165,9 +190,12 @@ def main():
165190
166191 args = main_parser .parse_args ()
167192 try :
168- if args .command == 'new' :
193+ if args .command == 'new' and args .file is not None :
194+ tags = args .tags .split (',' ) if args .tags is not None else None
195+ upload_model (args .file .strip (), args .name .strip (), tags )
196+ if args .command == 'new' and args .automl is not None :
169197 tags = args .tags .split (',' ) if args .tags is not None else None
170- upload_model (args .model_file , args .name , tags )
198+ add_automl_model (args .automl . strip () , args .name . strip () , tags )
171199 elif args .command == 'list' :
172200 list_models (args .filter )
173201 elif args .command == 'info' :
0 commit comments