Compute Services

Serverless computing and Watson service chaining via OpenWhisk : Part 2 of 3 chaining the services

Share this post:

In Part 1 of this series, you learned the basics of Serverless computing and the building blocks behind OpenWhisk. In this post, you will create Watson Services  and add them to an OpenWhisk Sequence on IBM Bluemix.

As our post is all about chaining Watson Services using OpenWhisk, in this section you will create three Watson Services namely

  • Language Translator.
  • Speech to Text.
  • Text to Speech.

Creating Watson Services on Bluemix

  • Log into Bluemix account with your registered Email or IBMid.
  • Click on Catalog on the top bar. On the left pane click on Watson under Services

  • Select Language Translator and Click to create a new service.
  • Provide a unique Service name. If you are not sure what to mention leave the default name provided.
  • Leave the Credential name as it is.
  • (Optional) Read the Features and Pricing Plans.
  • Click on Create.

Note: Repeat the above steps to create Speech to Text and Text to Speech Watson Services.

Develop an OpenWhisk Sequence on a browser

In this section of the post, you will chain the Watson Services along with other actions to create a sequence that translates an input in en to fr.

  • Click on the hamburger menu   and select OpenWhisk.
  • You will see the landing page with two options – Develop in a Browser or Download OpenWhisk CLI.

  • Click on Develop in your Browser and you should see the Develop tab selected.
  • On the left pane, you will see tabs grouping your Actions, Sequences, Rules and Triggers.
  • Under My Actions, you should see two sample actions created for your reference namely Hello World and Hello World with Params. Both these actions are Node.JS 6 Actions.
  • Click on Create an Action. You are creating an OpenWhisk actionmyMessageSwiftAction. Choose SwiftV3 execution runtime, and, optionally, a sample from which to start, Choose Hello World in Swift. You can also choose to change the default quota settings, for this lab lets go with default Memory and Time Limit.

  •  Click on Create Action and you should see your action under MY ACTIONS tab. Select myMessageSwiftAction and replace the existing function code with the code below
func main(args: [String:Any]) -> [String:Any] {

if let message = args["message"] as? String {

return [ "payload" : "\(message)!" ]

} else {

return [ "payload" : "Hello stranger!" ]

}}

Note: As output of one action is input to the next action, The JSON with payload will be an input to the next action you create in the sequence.

  •   Hit Make it Live to save your changes. Now Click Run the Action on the top and change the “message” JSON input. Run with this Value should invocate the action in a console and show the output with your input as a Payload output. Also, you can see the logs, Completed in and how many seconds are you Billed for.
  • Close the console and Click Link into a Sequence. You should see Configure a New Action Sequence with a variety of Packages to select from

Note: In IBM® Bluemix® OpenWhisk, you can use packages to bundle together a set of related actions, and share them with others.

  • Click on Watson Text to Speech tile and  Click on to create a new Package binding.

Note: Although you can use the entities in a package directly, you might find yourself passing the same parameters to the action every time. You can avoid this by binding to a package and specifying default parameters. These parameters are inherited by the actions in the package.

For example, in the /whisk.system/cloudant package, you can set default username, password, and dbname values in a package binding and these values are automatically passed to any actions in the package.

  • Let’s call this myWatsonTextToSpeech (Name for our configuration) and from the dropdown select the Text to Speech service which we created earlier.
  • (Optional) You can click on View Source to see the code.
  • Click on Add to Sequence to connect this to Swift Action which we created.
  • Click on This Looks Good to save your Watson Sequence and name it MyWatsonSequence. Also, run the sequence to see the output.
  • As you are not done with the chaining yet, Click on Extend and repeat the steps for adding Speech to Text package to the chain.
    • Name of the binding: myWatsonSpeechToText
    • In the dropdown, Choose the Speech to Text service which you created.
    • Click on Add to Sequence.
    • Click on This Looks Good to save your Watson Sequence.
  • Click on Actions and create a new action called myLanguageTranslator Which will convert the Speech to Text output to the JSON format of Watson Translator Package.
    • Execution Runtime – Node.JS 6
    • Start with a Blank State.
    • Create Action.
    • Replace the existing code with the code snippet below.
function main(params) {

return { payload: params.data };

}

  • Click Make it Live to save your Action.
  • On the left pane under MY SEQUENCES, select MyWatsonSequence -> Extend -> My Actions Tile -> Select myLanguageTranslator and Click Add to Sequence
  • Click on Extend and Select Watson Translator
    • Select Translator as your Action.
    • Name of the Binding: myWatsonTranslator
    • Select the Language Translator Service which we created earlier.
    • Click on Add to Sequence.
  • Save your changes and Click on Run this Sequence to test your Watson Sequence.

Test: Json Input

{

"message": "hello"

}

Expected Output:

In this part, you completed the Watson service chaining via an OpenWhisk Sequence. You can chain other packages as well following the same steps.

Technical Offering Manager & Polyglot Programmer | IBM Cloud

More Compute Services stories
February 21, 2019

IBM Cloud Kubernetes Service Available in Mexico City

IBM Cloud Kubernetes Service is a managed Kubernetes offering that delivers powerful management tools, an intuitive user experience, and built-in security and isolation. Today, we are excited to announce the availability of the IBM Cloud Kubernetes Service in Mexico City, Mexico.

Continue reading

February 18, 2019

Build a Container Image Inside a Kubernetes Cluster and Push it to IBM Cloud Container Registry

We're going to show you how to build a source into a container image from a Dockerfile inside a Kubernetes cluster and push the image to IBM Cloud Container Registry with Google's Kaniko tool.

Continue reading

February 12, 2019

A “Kubernetes Everywhere” Approach: Build and Deploy Enterprise-Scale Modern Applications for Hybrid Cloud

We are excited to introduce two optional cloud-managed services and capabilities designed to enable clients to quickly build and deploy enterprise-scale container-based applications across hybrid environments: Managed Istio and Managed Knative for IBM Cloud Kubernetes Service.

Continue reading