visit
I’ll walk you through building a simple web app to generate predictions on your trained model. It makes use of Firebase and Cloud Functions so it’s entirely serverless (yes, I put serverless and ML in the same blog post 🙄). Here’s the app architecture:
Want to skip to the code? It’s all available in .
For this blog post I’ll assume you already have a trained AutoML Vision model that’s ready for predictions. The next step is to associate this project with Firebase. Head over to the and click Add project. Then click on the dropdown and select the Cloud project where you’ve created your AutoML model. If you’ve never used Firebase before, you’ll also need to .
Next, clone the code from and cd
into the directory where you’ve downloaded it. To initialize Firebase in that directory run firebase init
and select Firestore, Functions, Hosting, and Storage when prompted (this demo uses all four):
Now we’re ready to go. In the next step we’ll set up and deploy the Cloud Function that calls AutoML.
You can use Cloud Functions independently of Firebase, but since I’m using so many Firebase features in my app already, I’ll make use of the handy . Take a look at the [functions/index.js](//github.com/sararob/automl-api-demo/blob/master/functions/index.js)
file and update the 3 variables at the top to reflect the info for your project:
Our Cloud Function is defined in exports.callCustomModel
. To trigger this function whenever a file is added to our Storage bucket we use: functions.storage.object().onFinalize()
. Here’s what’s happening in the function:
tmp/
dir to do this)All we need to do to send this to the AutoML API is created a prediction client and call predict()
:
Time to deploy the function. From the root directory of this project, run firebase deploy --only functions
. When the deploy completes you can test it out by navigating to the Storage section of your Firebase console and uploading an image:
Uploading an image to Firebase Storage
Then, head over to the Functions part of the console to look at the logs. If the prediction request completed successfully, you should see the prediction response JSON in the logs:
Function logs
Inside the function, we also write the prediction metadata to Firestore so that our app can display this data on the client. In the Firestore console, you should see the metadata saved in a images/ collection:
Prediction metadata in Cloud Firestore With the function working, it’s time to set up the app frontend.
To test the frontend locally, run the command firebase serve
from the root directory of your project and navigate to localhost:5000
. Click on the Upload a cloud photo button in the top right. If the image you uploaded returned a prediction from your model, you should see that displayed in the app. Remember that this app is configured for my cloud detector model, but you can easily modify the code to make it work for your own domain. When you upload a photo, check your Functions, Firestore, and Storage dashboards to ensure everything is working.
Finally, let’s make use of Firebase Hosting to deploy the frontend so we can share it with others! Deploying the app is as simple as running the command firebase deploy --only hosting
. When the deploy finishes your app will be live at your own firebaseapp.com
domain.