Using Google Cloud from Colab

Laurent Picard - Jun 16 '23 - - Dev Community

Google Colab is an amazing tool for Pythonistas. It can be used for a variety of tasks, from quickly experimenting with Python code to sharing extensive data processing notebooks with the world. Colab runs on Google Cloud, which gives you a boost when accessing cloud services because your code is running inside Google’s high performance network, and also offers a simple way to use Google Cloud services, letting you run powerful cloud workloads from your browser.


📦️ Install dependencies

Colab comes with hundreds of preinstalled packages, including pandas, numpy, matplotlib, Flask, Pillow, tensorflow, and pytorch. You can install additional required dependencies as needed.

For example, to analyze text with the power of machine learning using the Natural Language API, install the google-cloud-language client library:

!pip install google-cloud-language>=2.9.1
Enter fullscreen mode Exit fullscreen mode

🔑 Authenticate

To authenticate with your Google Cloud account within the Colab notebook, use the authenticate_user method. A new parameter lets you specify your project ID:

from google.colab import auth

PROJECT_ID = ""  # @param {type:"string"}

auth.authenticate_user(project_id=PROJECT_ID)
Enter fullscreen mode Exit fullscreen mode

After this step:

  • 👍 You are authenticated for gcloud CLI commands.
  • 👍 You are also authenticated when using Google Cloud Python client libraries. Note that you generally won’t need to create a service account.
  • 👍 Your default project is set.
  • 👍 Your notebook can also access your Google Drive, letting you ingest or generate your own files.

🔓 Enable APIs

Ensure required APIs are enabled. In our example, that’s the service language.googleapis.com:

!gcloud services enable language.googleapis.com
Enter fullscreen mode Exit fullscreen mode

🤯 Use Google Cloud services

That’s it! You can now directly use the service by calling its Python client library:

from google.cloud import language

def analyze_text_sentiment(text: str) -> language.AnalyzeSentimentResponse:
    client = language.LanguageServiceClient()
    document = language.Document(
        content=text,
        type_=language.Document.Type.PLAIN_TEXT,
    )
    return client.analyze_sentiment(document=document)

# Input
text = "Python is a very readable language, ..."  # @param {type:"string"}

# Send a request to the API
response = analyze_text_sentiment(text)

# Use the results
print(response)
Enter fullscreen mode Exit fullscreen mode

📊 Benefit from notebook advanced features

Colab is hosting a Jupyter notebook. I personally depict Colab as the “Google Drive of notebooks”. As notebooks have additional superpowers, this lets you nicely process and visualize your data, such as tables, images, or charts.

In our example, the function show_text_sentiment gathers results in a pandas DataFrame, which renders as a table:

Screencast showing successive text sentiment analyses with results displayed in a table


✨ Check it out

In these examples, click the “Open in Colab” link:

💡 You can directly create a new notebook by opening the colab.new URL.


👋 Have fun!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .