Kubernetes with OpenShift World Tour: Get hands-on experience and build applications fast! Find a workshop!

Use multiple AI services with Cloud Functions

Artificial intelligence (AI) is becoming the technology that we use in our daily routines. Each day, we benefit from AI tools with search recommendations, image detection, and more. We are on the verge of an era where every individual interacts with AI, and to keep up with this movement, we must learn and implement AI on our own.

Serverless is the concept where developers focus only on writing their code regardless of server maintenance, upgrades, or the operating system. They implement functions in any programming language and call them on demand.

In this tutorial, with the help of Serverless technology, I explain how to use multiple IBM® Cloud Watson™ Services to quickly apply AI technology. I show how to input text in different languages to IBM Watson Language Translator to retrieve the English translation of the text, and then send the English text to the IBM Watson Natural Language Understanding API to detect the emotions inside the text.

Flow

Flow diagram

Prerequisites

To complete this tutorial, you need:

Estimated Time

It should take you approximately 35 minutes to complete this tutorial.

Steps

Follow these steps to complete the tutorial.

Install Watson Language translator

To begin, you must install Watson Language Translator and modify the codes and parameters.

  1. Log in to IBM Cloud with your credentials.
  2. From the search bar at the top, search for Functions.

    Searching for Functions

  3. Click Start Creating.

    Cloud Functions page

  4. Choose Install Packages.

    Selecting install packages

  5. From the Installable Packages, choose Watson.

    Install package screen

  6. Choose Language Translator, and click Install. You are directed to the Actions page where you can find predefined templates for Watson Language Translator Actions.

    Selecting Language Translator action

  7. Click identify under language-translator-v3 to identify the language from the given text.

    Selecting Identify

  8. Copy all of the code inside the identify.js file. Then, paste the code into the identify action that you opened in the previous step. Note the code in the comments beside the function main function. It indicates that you use two parameters inside the function. The text parameter is the text that we investigate, and the version parameter is the Language Translator version that we use. Click Save.

    Code inside identify file

  9. Now we must add these two parameters.

    1. From the left sidebar, click Parameters.
    2. Click Add Parameter.
    3. Enter text in the empty field under Parameter Name.
    4. Enter some text that you want to investigate in the empty field under Parameter Value. In this case, it is “Hello! This is the sample text.”
    5. Click Add from the upper right, and enter version in the empty field under Parameter Name.
    6. Enter 2018-05-01 in the empty field under Parameter Value.
    7. Click Save.

      Parameters

  10. Go back to the Actions main page, and click translate under language-translator-v3.

  11. Copy all of the code inside the translate.js file. Then, paste the code into the translate action that you opened in the previous step. Note the code in the comments beside the function main function. You first extract the result from an identify action, and then check whether the language is already in English. If the language is not English, we make an API call to Language Translator with the source and target language parameters. Otherwise, we send the English text directly to the Natural Language Understanding Function that we are going to implement without making any changes. Click Save.

Install the Watson Natural Language Understanding Package

Next, you must install the Watson Natural Language Understanding Package and modify the code and parameters.

  1. Go back to the Cloud Functions main page.

  2. Choose Install Packages.

  3. From the Installable Packages, choose Watson.

  4. Choose Natural Language Understanding, and click Install. You are directed to the Actions page where you can find predefined templates for Watson Natural Language Understanding Actions.

  5. Click analyze under natural-language-understanding-v1 to analyze the translated text.

  6. Copy all of the code inside the analyze.js file. Then, paste the code into the analyze action that you opened in the previous step. Note the code in the comment fields (there are two fields) beside the function main function. First, we must give the credentials. Then, we check whether the text is in English (it is just for parsing purposes). Finally, between the second comment field, we alter the code in such a way that it gives no bugs. Click Save.

  7. Now we need to add the two parameters, version and features to analyze.

    1. From the left sidebar, click Parameters.
    2. Click Add Parameter.
    3. Enter version in the empty field under Parameter Name.
    4. Enter 2019-07-12 in the empty field under Parameter Value.
    5. Click Add at the upper right.
    6. Enter features in the empty field under Parameter Name.
    7. Enter {"emotion":{"limit":2}} in the empty field under Parameter Value.
    8. Click Save.

      Adding version

Create a sequence

Now, it’s time to connect all three functions by making them a sequence where the output of one function is the input of the next function.

  1. Go back to the Cloud Functions main page.

  2. Click Start Creating.

  3. Click Create Sequence. Name the sequence anything you like. Select Default Package, then Select Existing, and choose language-translator-v3/identify to start the sequence with the first function. Click Create.

  4. From your sequence page, click Add in the upper right. Click Select Existing, and choose language-translator-v3/translate. Click Add.

  5. From your sequence page, click Add in the upper right. Click Select Existing, and choose natural-language-understanding-v1/analyze. Click Add, then click Save.

It is time to create the Language Translator and Natural Language Understanding Services. You do this from the command-line interface tools.

  1. Open your terminal.

  2. Log in to IBM Cloud from terminal by entering the following code.

     ibmcloud login
    
  3. Set your region and space automatically.

     ibmcloud target --cf
    
  4. Create a free Language Translator instance.

     ibmcloud service create language_translator lite demo-language-translator
    
  5. Create a free Natural Language Understanding instance.

     ibmcloud service create natural-language-understanding free demo-natural-language-understanding
    
  6. Create a credential for your Language Translator service to use in Cloud Functions.

     ibmcloud service key-create demo-natural-language-understanding demo-natural-language-understanding-key
    
  7. Create a credential for your Natural Language Understanding service to use in Cloud Functions.

     ibmcloud service key-create demo-language-translator demo-language-translator-key
    
  8. Copy your Language Translator and Natural Language Understanding package name to your clipboard by listing your packages.

     ibmcloud fn list
    
  9. Bind your related service to the related packages.

     ibmcloud fn service bind language_translator <your-language-translator-package-name>
    
     ibmcloud fn service bind natural-language-understanding <your-natural-language-understanding-package-name>
    

Adjust credentials and test

  1. Go to your analyze action. From the left sidebar, click Parameters. You see a new parameter, __bx_cred. Change its name to credentials.

  2. Go to your sequence page, and click Invoke. You see different emotions detected from your text with different certainty values.

Conclusion

Artificial intelligence is getting stronger every day, and it’s exciting to see the outcomes that it serves to us. By quickly experiencing this technology with IBM Cloud Functions, you get the ability and agility to innovate on your own.

Note: We constructed the sequence but we did not open it as a web endpoint to call from outside. Therefore, from the left sidebar of the sequence page, click Endpoints, and select Enable as Web Action. Click Save. Copy the URL under Web Action and put .json at the end of the URL. You can now use this endpoint to call the sequence.

Guray Baydur