Skip to content

Latest commit

 

History

History
132 lines (95 loc) · 3.7 KB

09-translationeventing.md

File metadata and controls

132 lines (95 loc) · 3.7 KB

Integrate with Translation API

In the previous lab, our Knative service simply logged out the received Pub/Sub event. While this might be useful for debugging, it's not terribly exciting.

Cloud Translation API is one of Machine Learning APIs of Google Cloud. It can dynamically translate text between thousands of language pairs. In this lab, we will use translation requests sent via Pub/Sub messages and use Translation API to translate text between languages.

Since we're making calls to Google Cloud services, you need to make sure that the outbound network access is enabled, as described in the previous lab.

You also want to make sure that the Translation API is enabled:

gcloud services enable translate.googleapis.com

Define translation protocol

Let's first define the translation protocol we'll use in our sample. The body of Pub/Sub messages will include text and the languages to translate from and to as follows:

{"text":"Hello World", "from":"en", "to":"es"} => English to Spanish
{"text":"Hello World", "from":"", "to":"es"} => Detected language to Spanish
{"text":"Hello World", "from":"", "to":""} => Error

Create a Translation Handler

Follow the instructions for your preferred language to create a service to handle translation messages:

Build and push Docker image

Build and push the Docker image (replace {username} with your actual DockerHub):

docker build -t {username}/translation:v1 .

docker push {username}/translation:v1

Deploy the service and trigger

Create a trigger.yaml file.

apiVersion: serving.knative.dev/v1beta1
kind: Service
metadata:
  name: translation
  namespace: default
spec:
  template:
    spec:
      containers:
        # Replace {username} with your actual DockerHub
        - image: docker.io/{username}/translation:v1
---
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: translation
spec:
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1beta1
      kind: Service
      name: translation

This defines the Knative Service that will run our code and Trigger to connect to Pub/Sub messages.

kubectl apply -f trigger.yaml

Check that the service and trigger are created:

kubectl get ksvc,trigger

Test the service

We can now test our service by sending a translation request message to Pub/Sub topic:

gcloud pubsub topics publish testing --message='{"text":"Hello World", "from":"en", "to":"es"}'

Wait a little and check that a pod is created:

kubectl get pods --selector serving.knative.dev/service=translation

You can inspect the logs of the subscriber (replace <podid> with actual pod id):

kubectl logs --follow -c user-container <podid>

You should see something similar to this:

  • C#

    info: translation.Startup[0]
          Decoded data: {"text":"Hello World", "from":"en", "to":"es"}
    info: translation.Startup[0]
          Calling Translation API
    info: translation.Startup[0]
          Translated text: Hola Mundo
    info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
          Request finished in 621.0973ms 200
    
  • Python

    [INFO] Starting gunicorn 19.9.0
    [INFO] Listening at: http://0.0.0.0:8080 (1)
    [INFO] Using worker: threads
    [INFO] Booting worker with pid: 8
    [INFO] Decoded data: {'text': 'Hello World', 'from': 'en', 'to': 'es'}
    [INFO] Translated text: Hola Mundo
    

What's Next?

Integrate with Vision API