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.
You must have
In this lab, you:
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>
In Cloud Shell (NOT Datalab), navigate to the folder containing the starter code for this lab
If this directory doesn't exist, you may need to git clone the repository first:
cd ~ git clone https://github.com/GoogleCloudPlatform/training-data-analyst.git
what_to_fix.sh script to see a list of items you need to add/modify to existing code to run your app:
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
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
application/templates/form.html files and replace #TODOs with code. For hints, see the following section.
Set the credentials to use Google Application Default Credentials and specify the ML Engine API with version.
Specify the name of your trained model deployed on Cloud MLE using the parent variable.
Build the call request with the prediction variable.
Cast the gestation_weeks feature into a float within the features array.
Add more values for the drop down option for Plurality.
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)
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)
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()
The final #TODO (scroll towards bottom) is to get gestation_weeks from the form data and cast into a float within the features array:
features['gestation_weeks'] = float(data['gestation_weeks'])
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.
In code editor, navigate to the
application/templates directory and click to open the
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">Twins</md-option> <md-option value="3">Triplets</md-option>
Save the changes you made using the File > Save button on the top left of your code editor window
In Cloud Shell, run the deploy.sh script to install required dependencies and deploy your app engine app to the cloud.
Go to the url
https://<PROJECT-ID>.appspot.com and start making predictions.
Note: Replace <PROJECT-ID> with your Project ID.