Firebase ML Model Downloader
Notice
This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:
https://firebase.google.com/docs/ml/flutter/use-custom-models
Once installed, you can access the firebase_ml_model_downloader
plugin by importing it in your Dart code:
Before using the package, you must first have ensured you have initialized FlutterFire.
To create a new downloader instance, call the instance
getter on FirebaseModelDownloader
:
By default, this allows you to interact with the downloader using the default Firebase App used whilst installing FlutterFire on your
platform. If however you'd like to use a secondary Firebase App, use the instanceFor
method:
#
List downloaded modelsTo access any locally downloaded models on the device, call the listDownloadedModels
method. The response contains a list of any locally
downloaded custom models:
#
Custom modelA custom model contains information such as the name, size and unique hash of the model. It also contains access to the local File
on the device,
allowing you to provide it to an interpreter plugin.
Once a custom model has been downloaded, you can access it using the FirebaseCustomModel
class:
If you're working with a 3rd party interpreter such as tflite_flutter
,
use the returned file to access the model:
#
Downloading a modelOnce you have deployed a TensorFlow model, you can download them to your
device using the getModel
method, specifying the model name and download options.
The above code will download the latest available myModel
model to the device, since the FirebaseModelDownloadType.latestModel
has been specified.
The method accepts 3 download types for differing behavior:
Type | Description |
---|---|
FirebaseModelDownloadType.localModel | Returns the current model if present, otherwise triggers new download (or finds one in progress) and only completes when download is finished. |
FirebaseModelDownloadType.localModelUpdateInBackground | Returns the current model if present and triggers an update to fetch a new version in the background. If no local model is present triggers a new download (or finds one in progress) and only completes when download is finished. |
FirebaseModelDownloadType.latestModel | Returns the latest model. Checks if latest model is different from local model. If the models are the same, returns the current model. Otherwise, triggers a new model download and returns when this download finishes. |
You can also provide specific conditions for the download, such as whether the model should be downloaded whilst the device has access to cellular data, or if the device is charging.
To provide conditions, provide a FirebaseModelDownloadConditions
instance to the getModel
method:
#
Deleting a download modelTo delete a model from the device, call the deleteDownloadedModel
method:
Note; this does not delete the model from your Firebase project, only the local copy on the device. You can download it again at any time.