Skip to content

Latest commit

 

History

History
99 lines (61 loc) · 2.81 KB

translationeventing.md

File metadata and controls

99 lines (61 loc) · 2.81 KB

Integrate with Translation API

In the previous lab, our 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.

Pub/Sub triggered service

We're assuming that you already went through Pub/Sub triggered service tutorial and already setup Knative with GCP & PubSub Topic and also have a CloudPubSubSource defined.

Enable Translation API

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 Translation service

Create a kservice.yaml.

This defines a Knative Service to receive messages.

kubectl apply -f kservice.yaml

Create a trigger

Last but not least, we need connect Translation service to Broker with a trigger.

Create a trigger.yaml:

This connects the testing topic to translation service.

Create the trigger:

kubectl apply -f trigger.yaml

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

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

kubectl logs --follow <podid>

You should see something similar to this:

Received content: {"text":"Hello World", "from":"en", "to":"es"}

Translated text: Hola Mundo