Keras is used by CERN, NASA, NIH, and many more scientific organizations around the world (and yes, Keras is used at the LHC). Keras has the low-level flexibility to implement arbitrary research ideas while offering optional high-level convenience features to speed up experimentation cycles. Show
This tutorial describes how to use the Google API Client Library for Python to call the AI Platform Prediction REST APIs in your Python applications. The code snippets and examples in the rest of this documentation use this Python client library. You will be creating a model in your Google Cloud project in this tutorial. That is a simple task that's easily contained to a small example. ObjectivesThis is a basic tutorial designed to familiarize you with this Python client library. When you're finished you should be able to:
CostsYou will not be charged for the operations in this tutorial. Refer to the pricing page for more information.Before you beginSet up your GCP project
Set up authenticationTo set up authentication, you need to create a service account key and set an environment variable for the file path to the service account key.
Set up a Python development environmentChoose 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 AI Platform Prediction, but isn't suitable for ongoing development work. Check Python installation Check You can upgrade export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"5 using the following command: pip install -U pip See the for more details. Install export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1 export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1 is a tool to create isolated Python environments. Check if you already have export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1 installed by running export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"4. If not, install export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1: pip install --user --upgrade virtualenv To create an isolated development environment for this guide, create a new virtual environment in export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1. For example, the following command activates an environment named export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"7: gcloud init0 For the purposes of this tutorial, run the rest of the commands within your virtual environment. See more information about usingexport GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1. To exit export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"1, run $env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"0. Cloud Shell
Install the Google API Client Library for PythonInstall the Google API Client Library for Python. This is a basic tutorial designed to familiarize you with this Python client library. When you're finished you should be able to:
Importing the required modulesWhen you want to use the Google API Client Library for Python to call the AI Platform Prediction REST APIs in your code, you must import its package and the OAuth2 package. For this tutorial (and for most standard uses of AI Platform Prediction) you only need to import specific modules:
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: gcloud init2 Building a Python representation of the APIGet your Python representation of the REST API. The method you call is $env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"6 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 $env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"7: gcloud init3 Configuring your parameters and request bodyTo 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:
Creating your requestMaking 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 requestUse the built client objects that you created earlier (if you followed the code snippet exactly, it's called $env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"7) 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 AI Platform Prediction APIs is $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"1, so your call begins with $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"2. Use this code to form your request: gcloud init7 Send the requestThe request that you constructed in the last step exposes an $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"3 method that you call to send the request to the service: gcloud init8 It's common for developers to combine this step with the last one: gcloud init9 Handle simple errorsA 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 $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"4 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 $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"5 class. To catch these errors, you'll use the $env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"5 module from the $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"7 package. Wrap your $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"8 call in a $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"4 block. Also put a set GOOGLE_APPLICATION_CREDENTIALS=[PATH]0 statement in the block, so that you will try to print the response only if the call succeeds: export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"0 Add a catch block to handle HTTP errors. You can use set GOOGLE_APPLICATION_CREDENTIALS=[PATH]1 to get the reason text fields from the response: export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"1 Of course, a simple print statement might not be the right approach for your application. Putting it all togetherHere is the complete example: export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"2 Generalizing to other methodsYou 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:
|