Using the Python Client Library

This tutorial describes how to use the Google API client library to easily make calls to the Cloud Machine Learning Engine REST APIs in your Python applications. The code snippets and examples in the rest of this documentation use the Python client library.

You will be creating a model in your Google Cloud Platform project in this tutorial. That is a simple task that's easily contained to a small example. You can learn more about managing models in the model and job management overview.

Objectives

This is a basic tutorial designed to familiarize you with the Python client library. When you're finished you should be able to:

  • Get a Python representation of the Cloud ML Engine services.
  • Use that representation to create a model in your project, which should help you understand how to call the other model and job management APIs.

Costs

You will not be charged for the operations in this tutorial. Refer to the pricing page for more information.

Before you begin

Complete the following steps to set up a GCP account, activate the Cloud ML Engine API, and install and activate the Cloud SDK.

Set up your GCP project

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the Cloud Machine Learning Engine and Compute Engine APIs.

    Enable the APIs

  5. Set up authentication:
    1. Go to the Create service account key page in the GCP Console.

      Go to the Create Service Account Key page
    2. From the Service account drop-down list, select New service account.
    3. Enter a name into the Service account name field.
    4. From the Role drop-down list, select Project > Owner.

      Note: The Role field authorizes your service account to access resources. You can view and change this field later using GCP Console. If you are developing a production application, specify more granular permissions than Project > Owner. For more information, see granting roles to service accounts.
    5. Click Create. A JSON file that contains your key downloads to your computer.
  6. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

  7. Install and initialize the Cloud SDK.

Set up your environment

Choose one of the options below to set up your environment locally on macOS or in a remote environment on Cloud Shell.

For macOS users, we recommend that you set up your environment using the MACOS tab below. Cloud Shell, shown on the CLOUD SHELL tab, is available on macOS, Linux, and Windows. Cloud Shell provides a quick way to try Cloud Machine Learning Engine, but isn’t suitable for ongoing development work.

macOS

  1. Install a virtual environment to create an isolated Python development environment for this guide. For example, the following commands install virtualenv and activate an environment named cmle-env.

    virtualenv cmle-env
    source cmle-env/bin/activate
  2. Confirm that you have Python installed and, if necessary, install it.

    python -V
  3. Install pip, Python’s package manager. Check if you already have pip installed by running pip --version. Ensure that you have the latest version of pip and if not, upgrade it using the following command:

    pip install -U pip

Cloud Shell

  1. Open the Google Cloud Platform Console.

    Google Cloud Platform Console

  2. Click the Activate Google Cloud Shell button at the top of the console window.

    Activate Google Cloud Shell

    A Cloud Shell session opens inside a new frame at the bottom of the console and displays a command-line prompt. It can take a few seconds for the shell session to be initialized.

    Cloud Shell session

    Your Cloud Shell session is ready to use.

  3. Configure the gcloud command-line tool to use your selected project.

    gcloud config set project [selected-project-id]

    where [selected-project-id] is your project ID. (Omit the enclosing brackets.)

Verify the Google Cloud SDK components

To verify that the Google Cloud SDK components are installed:

  1. List your models:

    gcloud ml-engine models list
  2. If you have not created any models before, the command returns an empty list:

    Listed 0 items.

    After you start creating models, you can see them listed by using this command.

  3. If you are an existing user, update gcloud.

    If you're using TensorFlow:

    gcloud components update

    If you're using scikit-learn or XGBoost:

    gcloud beta components update

Install TensorFlow

To install TensorFlow, run the following command:

pip install --upgrade tensorflow

For more information about installing TensorFlow, see the TensorFlow documentation.

To troubleshoot your TensorFlow installation, see TensorFlow's guide to common installation problems.

Run a simple TensorFlow Python program

Run a simple Python program to test your installation of TensorFlow. If you are using Cloud Shell, note that TensorFlow is already installed.

  1. Start a Python interactive shell.

    python
    
  2. Import TensorFlow.

    >>> import tensorflow as tf

  3. Create a constant that contains a string.

    >>> hello = tf.constant('Hello, TensorFlow!')

  4. Create a TensorFlow session.

    >>> sess = tf.Session()

    You can ignore the warnings that the TensorFlow library wasn't compiled to use certain instructions.

  5. Display the value of hello.

    >>> print(sess.run(hello))

    If successful, the system outputs:

    Hello, TensorFlow!

  6. Stop the Python interactive shell.

    >>> exit()

Install the Google Python library

Install the Google APIs Client Library for Python.

Importing the required modules

When you want to use the API client library to use the Cloud ML Engine REST APIs in your Python code, you must import its package and the OAuth2 package. For this tutorial (and for most standard uses of Cloud ML Engine) you only need to import specific modules from both packages: GoogleCredentials from OAuth2 and discovery from Google API Client Library. Refer to the documentation for those packages to learn about the other available modules.

Create a new Python file using your favorite editor, and add these lines:

from oauth2client.client import GoogleCredentials
from googleapiclient import discovery

Building a Python representation of the API

Get your Python representation of the REST API. The method you call is build because the API client library uses service discovery to dynamically set up connections to the services as they exist when you make the call. Call your object that encapsulates the services ml:

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

Configuring your parameters and request body

To make a call to a service, you must create the parameters and request body that will be passed to the REST API. You pass parameters as regular Python parameters to the method that represents the call. The body is a JSON resource just as you would use if calling the API with an HTTP request directly.

Take a look at the REST API for creating a model in a new browser tab, projects.models.create:

  • Notice the path parameter parent, which is the part of the URI of the request that identifies the project. If you were making the HTTP POST request directly, you would use the following URI:

    https://ml.googleapis.com/v1/projects/your_project_ID/models
    

    When using the API client library, the variable part of the URI is represented as a string-typed parameter to the API call. You'll set it to 'projects/<var>your_project_ID</var>'. Store your project in a variable to make API calls cleaner:

    projectID = 'projects/{}'.format('your_project_ID')
    

  • The body of the request is a JSON resource representing the model information. You can see in the model resource definition that it has two values for input: name and (optionally) description. You can pass a Python dictionary in the place of JSON and the API client library will perform the necessary conversion.

    Create your Python dictionary:

    requestDict = {'name': 'your-model-name',
                   'description': 'This is a machine learning model entry.'}
    

Creating your request

Making calls to APIs with the Python client library has two steps: first you create a request, then you make the call using that request.

Create the request

Use the built client objects that you created earlier (if you followed the code snippet exactly, it's called ml) as the root of the API hierarchy and specify the API you want to use. Each collection in the API path behaves like a function that returns a list of the collections and methods within it. For example, the root of all the Cloud ML Engine APIs is projects, so your call begins with ml.projects().

Use this code to form your request:

request = ml.projects().models().create(parent=projectID, body=requestDict)

Send the request

The request that you constructed in the last step exposes an execute method that you call to send the request to the service:

response = request.execute()

It's common for developers to combine this step with the last one:

response = ml.projects().models().create(parent=projectID,
                                         body=requestDict).execute()

Handle simple errors

A lot of things can go wrong when you make API calls over the Internet. It's a good idea to handle common errors. The simplest way to deal with errors is to put your request in a try block and catch likely errors. Most of the errors you're likely to get from the service are HTTP errors, which are encapsulated in the HttpError class. To catch these errors, you first need to get the errors module from the googleapiclient package. Add this statement with the rest of your imports.

from googleapiclient import errors

Then wrap your request.execute() call in a try block. Also put a print statement in the block, so that you will try to print the response only if the call succeeds:

try:
    response = request.execute()
    print(response)

Finally, add a catch block to handle HTTP errors. You can use HttpError._get_reason() to get the reason text fields from the response:

except errors.HttpError, err:
    # Something went wrong, print out some information.
    print('There was an error creating the model. Check the details:')
    print(err._get_reason())

Of course, a simple print statement might not be the right approach for your application.

Putting it all together

Here is the complete example:

from googleapiclient import discovery
from googleapiclient import errors

# Store your full project ID in a variable in the format the API needs.
projectID = 'projects/{}'.format('your_project_ID')

# Build a representation of the Cloud ML API.
ml = discovery.build('ml', 'v1')

# Create a dictionary with the fields from the request body.
requestDict = {'name': 'your_model_name',
               'description': 'your_model_description'}

# Create a request to call projects.models.create.
request = ml.projects().models().create(
              parent=projectID, body=requestDict)

# Make the call.
try:
    response = request.execute()
    print(response)
except errors.HttpError, err:
    # Something went wrong, print out some information.
    print('There was an error creating the model. Check the details:')
    print(err._get_reason())

Generalizing to other methods

You can use the procedure you learned here to make any of the other REST API calls. Some of the APIs require much more complicated JSON resources than creating a model does, but the principles are the same:

  1. Import googleapiclient.discovery and googleapiclient.errors.

  2. Use the discovery module to build a Python representation of the API.

  3. Use the API representation as a series of nested objects to get to the API you want and create a request. For example,

    request = ml.projects().models().versions().delete(
        name='projects/myproject/models/mymodel/versions/myversion')
    

  4. Call request.execute() to send the request, handling exceptions in an appropriate way for your application.

  5. When there is a response body, you can treat it like a Python dictionary to get at the JSON objects specified in the API reference. Note that many of the objects in responses have fields that are present only in some circumstances. You should always check to avoid key errors:

    response = request.execute()

    some_value = response.get('some_key') or 'default_value'

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud ML Engine for TensorFlow