Google justifies its massive AI spending with a booming cloud business
Back to Tutorials
techTutorialbeginner

Google justifies its massive AI spending with a booming cloud business

July 22, 20268 views4 min read

Learn how to set up and use Google Cloud's Vertex AI service for making machine learning predictions using Python. This beginner-friendly tutorial walks you through creating a Google Cloud project, authenticating with service accounts, and making API calls to Google's AI infrastructure.

Introduction

In this tutorial, you'll learn how to interact with Google Cloud's AI services using Python. As Google's cloud business continues to thrive with AI adoption, understanding how to work with their AI infrastructure is becoming increasingly valuable. This tutorial will guide you through setting up a basic Google Cloud project and using the Vertex AI service to make predictions with machine learning models.

Vertex AI is Google's unified AI platform that helps developers build, train, and deploy machine learning models. By the end of this tutorial, you'll have created a simple Python script that can make predictions using Google's pre-trained models.

Prerequisites

Before starting this tutorial, you'll need:

  • A Google Cloud account with billing enabled
  • Python 3.7 or higher installed on your computer
  • Basic understanding of Python programming concepts
  • Google Cloud SDK installed (optional but recommended)

Why these prerequisites? The Google Cloud account is essential because we'll be using Google's infrastructure. Python is the primary language for working with Google's AI services, and having the SDK installed makes authentication easier.

Step-by-Step Instructions

1. Create a Google Cloud Project

First, you need to create a project in Google Cloud Console:

  1. Go to Google Cloud Console
  2. Click on the project selector dropdown in the top navigation bar
  3. Select "New Project"
  4. Enter a project name (e.g., "ai-tutorial-project")
  5. Click "Create"

2. Enable Required APIs

Vertex AI requires specific APIs to be enabled:

  1. In the Google Cloud Console, navigate to "APIs & Services" > "Library"
  2. Search for "Vertex AI API"
  3. Click on the API and then click "Enable"
  4. Repeat for "Cloud Storage API"

3. Create a Service Account

We need to create a service account for authentication:

  1. Navigate to "IAM & Admin" > "Service Accounts"
  2. Click "Create Service Account"
  3. Enter a name (e.g., "ai-tutorial-sa") and description
  4. Click "Create and Continue"
  5. Grant the service account the "Vertex AI User" role
  6. Click "Done"

4. Download Service Account Key

Next, download the JSON key file for authentication:

  1. From the service accounts list, find your newly created account
  2. Click the "three dots" menu and select "Manage keys"
  3. Click "Add Key" > "Create new key"
  4. Select "JSON" format and click "Create"
  5. Save the downloaded file as service-account-key.json in your project directory

5. Install Required Python Packages

Open your terminal and install the necessary Python packages:

pip install google-cloud-aiplatform

6. Create Your Python Script

Create a new file called predict.py with the following content:

import os
from google.cloud import aiplatform

# Set the path to your service account key
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account-key.json"

# Initialize the Vertex AI client
aiplatform.init(project="your-project-id", location="us-central1")

# Define the prediction endpoint
endpoint = aiplatform.Endpoint("projects/your-project-id/locations/us-central1/endpoints/your-endpoint-id")

# Prepare sample data for prediction
instances = [
    {
        "feature1": 1.0,
        "feature2": 2.0,
        "feature3": 3.0
    }
]

# Make prediction
response = endpoint.predict(instances=instances)

# Print results
print("Prediction results:")
print(response.predictions)

7. Update Your Script with Real Project Information

Replace the placeholder values in your script:

  1. Replace your-project-id with your actual Google Cloud project ID
  2. Replace your-endpoint-id with a real endpoint ID from your Vertex AI deployment

8. Run Your Script

Execute your Python script in the terminal:

python predict.py

This will authenticate using your service account and make a prediction request to Google's AI infrastructure.

9. Understanding the Code

Let's break down what each part does:

  • os.environ["GOOGLE_APPLICATION_CREDENTIALS"] - This tells the Google Cloud client libraries where to find your authentication credentials
  • aiplatform.init() - Initializes the Vertex AI client with your project and region
  • aiplatform.Endpoint() - References a specific deployed model endpoint
  • endpoint.predict() - Sends prediction requests to the deployed model

10. Testing with a Pre-trained Model

For a more practical example, you can use Google's pre-trained models. Here's a modified version that uses a sample model:

import os
from google.cloud import aiplatform

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account-key.json"

# Initialize client
aiplatform.init(project="your-project-id", location="us-central1")

# Use a sample pre-trained model
model = aiplatform.Model("projects/your-project-id/locations/us-central1/models/123456789")

# Prepare sample data
instances = [
    [1.0, 2.0, 3.0],  # Sample input features
]

# Make prediction
response = model.predict(instances=instances)

print("Prediction result:")
print(response.predictions)

Summary

In this tutorial, you've learned how to set up a Google Cloud project and use Vertex AI for making predictions. You created a service account, downloaded authentication keys, installed the necessary Python packages, and wrote a script that interacts with Google's AI infrastructure.

This hands-on experience demonstrates how companies like Google are leveraging their cloud infrastructure to provide AI services to businesses. As the cloud business continues to thrive with AI adoption, understanding these tools becomes crucial for developers and businesses looking to leverage machine learning capabilities.

Remember that to make this work with actual predictions, you'll need to deploy a model first in Vertex AI. This tutorial provides the foundation for working with Google's AI infrastructure, which is at the core of Google's growing cloud business success.

Related Articles