In this lab, you build an AppEngine app to serve your ML predictions. The steps in this lab get you familiar with the user-facing form and the python script that makes the call to your deployed ML model by letting you modify certain code snippets before you deploy your App Engine app.

What you need

You must have

What you learn

In this lab, you:

Step 1

Launch Cloud Shell from GCP console and run the following commands to confirm that user account and project are correctly set

gcloud auth list
gcloud config list project

If it is not, you can set it with this command:

gcloud config set <PROJECT_ID>

Step 2

In Cloud Shell, navigate to the folder containing the starter code for this lab

cd ~/asl-ml-immersion/2.5_servingml_lab

If this directory doesn't exist, you may need to git clone the repository first:

cd ~
gcloud init
gcloud source repos clone asl-ml-immersion --project=asl-ml-immersion
cd ~/asl-ml-immersion/2.5_servingml_lab

Step 1

Run the what_to_fix.sh script to see a list of items you need to add/modify to existing code to run your app:

./what_to_fix.sh

As a result of this, you will a list of filenames and lines within those files marked with "TODO". These are the lines where you have to add/modify code. For this lab, you will focus on #TODO items for main.py and form.html only.

Step 2

You may use the Cloud Shell code editor to view and edit the contents of these files.

Click on the icon on the top right of your Cloud Shell window to launch Code Editor

Once launched, navigate to the ~/asl-ml-immersion/2.5_servingml_lab directory.

Step 3

Open the main.py and form.html files and replace #TODOs with code. For hints, see the following section.

Step 1

Set the credentials to use Google Application Default Credentials and specify the ML Engine API with version.

Step 2

Specify the name of your trained model deployed on Cloud MLE using the parent variable.

Step 3

Build the call request with the prediction variable.

Step 4

Cast the plurality feature into a float within the features array.

Step 1

Add more values for the drop down option for Plurality.

Step 1

Open the main.py file by clicking on it. Notice the lines with # TODO for setting credentials and the api to use.

Set the credentials to use Google Application Default Credentials (recommended way to authorize calls to our APIs when building apps deployed on AppEngine):

credentials = GoogleCredentials.get_application_default()

Specify the api name (ML Engine API) and version to use:

api = discovery.build('ml', 'v1', credentials=credentials)

Step 2

Scroll further down in main.py and look for the next #TODO in the method get_prediction(). In there, specify, using the parent variable, the name of your trained model deployed on Cloud MLE:

parent = 'projects/%s/models/%s' % (project, model_name)

Step 3

Now that you have all the pieces for making the call to your model, build the call request by specifying it in the prediction variable:

prediction = api.projects().predict(body=input_data, name=parent).execute()

Step 4

The final #TODO (scroll towards bottom) is to get plurality from the form data and cast into a float within the features array:

features['plurality'] = float(data['plurality'])

Step 5

Save the changes you made using the File > Save button on the top left of your code editor window.

form.html is the front-end of your app. The user fills in data (features) about the mother based on which we will make the predictions using our trained model.

Step 1

In code editor, navigate to the application/templates directory and click to open the form.html file

Step 2

There is one #TODO item here. Look for the div segment for Plurality and add options for other plurality values (2, 3, etc)

<md-option value="2">Two</md-option>
<md-option value="3">Three</md-option>

Step 3

Save the changes you made using the File > Save button on the top left of your code editor window

Step 1

In Cloud Shell, run the deploy.sh script to install required dependencies and deploy your app engine app to the cloud.

./deploy.sh

Step 2

Go to the url https://<PROJECT-ID>.appspot.com and start making predictions.

Note: Replace <PROJECT-ID> with your Project ID.