Skip to content

Commit 5efb865

Browse files
Add AutoML model example (firebase#19)
* Add AutoML model example * Remove NL from example * Update version
1 parent 2665445 commit 5efb865

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

machine-learning/README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,31 @@ Also see the [Firebase ML API Tutorial Colab/Jupyter notebook][colab].
5757
5858
```
5959
$ ./manage-ml.py list
60-
fish_detector 8716935 vision
61-
barcode_scanner 8716959 vision
62-
smart_reply 8716981 natural_language
63-
$ ./manage-ml.py new ~/yak.tflite yak_detector --tags vision,experimental
64-
Uploading model to Cloud Storage...
60+
Name ID Tags
61+
---------------------- ---------- ------------------
62+
fish_recognizer 12990533 vision
63+
barcode_scanner 12990544 vision
64+
$ ./manage-ml.py new yak_detector -f model.tflite -t vision,experimental
65+
Uploading to Cloud Storage...
6566
Model uploaded and published:
66-
yak_detector 8717019 experimental, vision
67-
$ ./manage-ml.py update 8717019 --remove_tags experimental
68-
$ ./manage-ml.py delete 8716959
67+
yak_detector 12990577 experimental, vision
68+
$ ./manage-ml.py new flower_classifier -a projects/12345/locations/us-central1/models/ICN12345
69+
Model uploaded and published:
70+
flower_classifier 12990597
71+
$ ./manage-ml.py list
72+
Name ID Tags
73+
---------------------- ---------- ----------------------
74+
fish_recognizer 12990533 vision
75+
barcode_scanner 12990544 vision
76+
yak_detector 12990577 experimental, vision
77+
flower_classifier 12990597
78+
$ ./manage-ml.py update 12990577 --remove_tags experimental
79+
$ ./manage-ml.py delete 12990544
6980
$ ./manage-ml.py list
70-
fish_detector 8716935 vision
71-
smart_reply 8716981 natural_language
72-
yak_detector 8717019 vision
81+
Name ID Tags
82+
---------------------- ---------- ------------------
83+
fish_recognizer 12990533 vision
84+
yak_detector 12990577 vision
85+
flower_classifier 12990597
7386
$
7487
```

machine-learning/manage-ml.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4564
def 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':

machine-learning/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
beautifultable~=1.0.0
2-
firebase-admin~=4.3.0
2+
firebase-admin~=4.4.0

0 commit comments

Comments
 (0)