This is a simple sample that shows how to:
- Create a Google App Engine service on Google Cloud Platform that loads a pickled scikit-learn model from Google Cloud Storage, and use it to serve prediction requests through Google Cloud Endpoints. See the accompanying blog post.
The benefits of this configuration include:
- App Engine's autoscaling and load balancing.
- Cloud Endpoints' monitoring and access control.
-
Install Python. The version (2.7 or 3) used for local development of the model should match the version used in the service, which is specified in the file
app.yaml
. -
Install Google Cloud Platform SDK. The SDK includes the command-line tools
gcloud
for deploying the service andgsutil
for managing files on Cloud Storage. -
Create a Google Cloud Platform project which as the following products enabled:
-
git clone https://github.com/GoogleCloudPlatform/ml-on-gcp
-
cd ml-on-gcp/tutorials/sklearn/gae_serve
-
This sample demonstrates how to deploy an App Engine service named
modelserve
. If you prefer to deploy to thedefault
service (for example, if this is the first App Engine service in your project, it must be nameddefault
), use the yaml files in thedefault/
subdirectory by copying them over the yaml files in the root directory of this sample.- Note that App Engine does not allow deleting the
default
service from your project.
- Note that App Engine does not allow deleting the
-
Update
modelserve.yaml
: ReplacePROJECT_ID
with your Google Cloud Platform project's id in this line:host: "modelserve-dot-PROJECT_ID.appspot.com"
- Note that this file defines the API specifying the input
X
to be an array of arrays of floats, and outputy
to be an array of floats. The model included in this sample codelr.pkl
is a pickled linear regression model with 2-dimensional inputs.
- Note that this file defines the API specifying the input
-
Deploy the service endpoint:
gcloud endpoints services deploy modelserve.yaml
This step deploys a Cloud Endpoint service, which allows us to monitor the API usage on the Endpoints console page.
-
If the deployment is successful, get the deployment's config id either from the Endpoints console page under the service's Deployment history tab, or you can find all the configuration IDs by running the following:
gcloud endpoints configs list --service="modelserve-dot-PROJECT_ID.appspot.com"
The configuration ID should look like
2017-08-03r0
. Ther0, r1, ...
part in the configuration IDs indicate the revision numbers, and you should use the highest (most recent) revision number. -
Create a Cloud Storage bucket with your choice of a
BUCKET_NAME
, and copy the sample model file over:gsutil mb gs://BUCKET_NAME gsutil cp lr.pkl gs://BUCKET_NAME
-
Update
app.yaml
:-
If you already have at least one App Engine service in your Google Cloud Platform project:
-
Replace
PROJECT_ID
with your Google Cloud Platform project's id. -
Replace
BUCKET_NAME
with the name of the bucket you created on Cloud Storage above. -
Replace
CONFIG_ID
with the configuration ID you got from the service endpoint deployment.
-
See the documentation for more information about
app.yaml
. -
-
Deploy the backend service:
gcloud app deploy
This step could take several minutes to complete.
-
If the deployment is successful, you can access it by first creating an API key with the "Create credentials" button on the Credentials page. Make sure you switch to the correct Google Cloud Platform project first.
-
You can access the deployed service in a few different ways: (Remember to replace
PROJECT_ID
andAPI_KEY
with their actual values below.)-
From the command line:
curl -H "Content-Type: application/json" -X POST -d '{"X": [[1, 2], [5, -1], [1, 0]]}' "https://modelserve-dot-PROJECT_ID.appspot.com/predict?key=API_KEY"
(Change the host URL to
PROJECT_ID.appspot.com
if you deployed the service asdefault
.)You should get the following response:
{"y": [0.6473534912754967, -0.7187842827829021, 0.3882338314071392]}
The deployed model
lr.pkl
is a simple linear regression model with 2-dimensional inputs. -
With the simple python client included in this sample:
from client import ModelServiceClient model_service_client = ModelServiceClient(host='https://modelserve-dot-PROJECT_ID.appspot.com', api_key='API_KEY') model_service_client.predict([[1, 2], [5, -1], [1, 0]]) # => [0.6473534912754967, -0.7187842827829021, 0.3882338314071392]
-
With the automatically generated swagger client (instructions):
import swagger_client swagger_client.configuration.api_key['key'] = 'API_KEY' api = swagger_client.DefaultApi() body = swagger_client.X([[1, 2], [5, -1], [1, 0]]) response = api.predict(body) # response = {"y": [0.6473534912754967, -0.7187842827829021, 0.3882338314071392]}
-
Delete service by running:
gcloud app services delete modelserve
gcloud service-management delete modelserve-dot-PROJECT_ID.appspot.com
(If the service was deployed as the default
service, it cannot be deleted.)
For information about configuring the service's health check, see the documentation.
For information about configuring the service's autoscaling, see the documentation.
For information about configuring the service's quota, see the documentation.