Run Watson NLP for Embed on IBM Cloud Code Engine

This blog post is about using the IBM Watson Natural Language Processing Library for Embed on IBM Cloud Code Engine and is related to my blog post Run Watson NLP for Embed on your local computer with DockerIBM Cloud Code Engine is a fully managed, serverless platform where you can run container images or batch jobs.

The IBM Watson Libraries for Embed are made for IBM Business Partners. Partners can get additional details about embeddable AI on the IBM Partner World page. If you are an IBM Business Partner you can get a free access to the IBM Watson Natural Language Processing Library for Embed.

To get started with the libraries you can use the link Watson Natural Language Processing Library for Embed home. It is an awesome documentation and it is public available.

I used parts of the IBM Watson documentation in my Code Engine example and I created a GitHub project with some additional example bash scripting. The project is called Run Watson NLP for Embed on your IBM Cloud Code Engine.

The blog post is structured in:

  • Some simplified technical basics about IBM Watson Libraries for Embed
  • Some screen shots from a running example using IBM Cloud Code Engine
  • Execute the example on IBM Cloud Code Engine
  • Summary

1. Some simplified technical basics about IBM Watson Libraries for Embed

The IBM Watson Libraries for Embed do provide a lot of pre-trained models you can find in the related model catalog for the Watson Libraries. Here is a link to the model catalog for Watson NLP, the catalog is public available.

There are runtime containers for the AI functionalities like NLPSpeech to Text and so on. You can integrate the container into your application and customize them, for example existing pre-trained models, but it’s also possible to use own created models. By separating the container runtime and the models to be loaded into the container, we are free to include the right models we need to integrate in our application to fulfil the needs of our business use case.

The resources are available in a IBM Cloud Container Registry. The image below shows a simplified overview.

The next gif shows a simplified overview of the dependencies and steps we need to execute when we using IBM Cloud Code Engine to run a custom Watson NLP runtime container. You can find the detailed instructions in the official IBM documentation installing running containers.

These are the simplified steps which are automated with bash scripting, which we see in the image above.

  1. We create a custom Watson NLP container locally, by using the Watson NLP runtime image and select models for the example. Therefor we define a Dockerfile and we use the IBM entitlement key to access the runtime image and the models on the IBM Cloud Container Registry.
  2. We use the IBM Cloud Container Registry to save and protect our custom container image. In the container image registry we create a namespace where we upload the image. The namespace name must be unique in the IBM Cloud region of the container registry.
  3. We create a Code Engine projectThe project must be unique in the IBM Cloud region of Code Engine. We also create a secret to access our IBM Cloud container registry and in this context the automation also need to create an IBM Cloud API key to do that task.
  4. Now we configure a Code Engine application. For the configuration we use the container registry secret to load the container image from the IBM Cloud Container Registry and we configure an environment variable that we are going to accept the license when we run our custom Watson NLP container image.
  5. To verify the running container image is working, we invoke a curl command to execute a SyntaxPredict from the Watson NLP API.

2. Some screen shots from a running example on Code Engine

  • The mapped custom Watson NLP container image:

3. Execute the example on IBM Cloud Code Engine

These are the steps you can follow along to run the example with custom Watson NLP container image on IBM Cloud Code Engine.

Step 1: Clone the example project to your local computer

git clone https://github.com/thomassuedbroecker/watson-nlp-example-code-engine
cd watson-nlp-example-code-engine/code

Step 2: Set needed environment variables in the .env file

cat .env-template > .env

Edit the .env file.

We need …

  • … an IBM_ENTITLEMENT_KEY to get the Watson NLP resources.
  • … an IBMCLOUD_APIKEY that the bash script can automate all tasks.
  • … an unique IBM Cloud Container Registry namespace name CR_NAMESPACE.
  • … to configure an email CE_EMAIL related to our container image registry access.
  • … an unique Code Engine project name CE_PROJECT_NAME.
# used as 'environment' variables
IBM_ENTITLEMENT_KEY="YOUR_KEY"
IBMCLOUD_APIKEY="YOUR_KEY"
CR_NAMESPACE="custom-watson-nlp-YOUR_NAME"
CE_EMAIL="YOUR_EMAIL"
CE_PROJECT_NAME="custom-watson-nlp-YOURNAME"

Step 3: Execute the run-watson-nlp-with-code-engine.sh bash script

The bash script execution can take several minutes.

sh run-watson-nlp-with-code-engine.sh

Example output and the links to the related functions in the bash script code

Links to the related functions in the bash script code

Example output

# ******
# Create custom Watson NLP container image
# ******


# ******
# Connect to IBM Cloud Container Image Registry: cp.icr.io/cp/ai
# ******

...
Login Succeeded

# ******
# List model array content
# ******

Model 0 : watson-nlp_syntax_izumo_lang_en_stock:1.0.7
Model 1 : watson-nlp_syntax_izumo_lang_fr_stock:1.0.7

# ******
# Download the models
# ******

...

# 2. Put models into the file share
Archive:  /app/model.zip
  inflating: config.yml              
...

# ******
# Create container image
# ******

Image name: watson-nlp-runtime-with-models
[+] Building 0.1s (7/7) FINISHED                                                
 => [internal] load build definition from Dockerfile                       0.0s

...

# ******
# Upload image to IBM Cloud container registry 
# ******

...

# ******
# Configure IBM Cloud Registry
# ******

The region is set to 'us-south', the registry is 'us.icr.io'.

OK
Adding namespace 'custom-watson-nlp-tsued' in resource group 'default' for account XXXX Account in registry us.icr.io ...

OK
Logging 'docker' in to 'us.icr.io'...
Logged in to 'us.icr.io'.

OK
Container image: us.icr.io/custom-watson-nlp-tsued/watson-nlp-runtime-with-models:1.0.0
The push refers to repository [us.icr.io/custom-watson-nlp-tsued/watson-nlp-runtime-with-models]
2de7f9fb3378: Pushed 
...
9aaca8eae7c0: Pushed 
1.0.0: digest: sha256:8ad1b4bdd6a89b0686c48fc2325900018a1ddfec0ffb1689457b5c433300d61c size: 4290

# ******
# Create Code Engine project
# ******

**********************************
 Create Code Engine project: custom-watson-nlp-tsued
**********************************

...

Creating project 'custom-watson-nlp-tsued'...
ID for project 'custom-watson-nlp-tsued' is 'b879a212-XXXX'.
Waiting for project 'custom-watson-nlp-tsued' to be active...
Now selecting project 'custom-watson-nlp-tsued'.
OK
**********************************
 Configure IBM Cloud Container Registry Access (us.icr.io) for (custom-watson-nlp-tsued)
**********************************
**********************************
 Create Code Engine project: custom-watson-nlp-tsued
**********************************
API key: 
Creating API key cliapikey_custom-watson-nlp-tsued under 641ebfXXXXXXb45279e as XXXXX...
OK
API key cliapikey_custom-watson-nlp-tsued was created
Successfully save API key information to cli_key.json
Creating image registry access secret 'custom.watson.nlp.cr.sec'...
OK

# ******
# Create Code Engine application custom-watson-nlp-application
# ******

Creating application 'custom-watson-nlp-application'...
Configuration 'custom-watson-nlp-application' is waiting for a Revision to become ready.
Ingress has not yet been reconciled.
Waiting for load balancer to be ready.
Run 'ibmcloud ce application get -n custom-watson-nlp-application' to check the application status.
OK

Set CE_APPLICATION_NAME URL: https://custom-watson-nlp-application.wqtmqy9e03u.us-south.codeengine.appdomain.cloud

# ******
# Verify the custom image
# ******

...

{"text":"This is a test sentence.", "producerId":{"name":"Izumo Text Processing", "version":"0.0.1"}, "tokens":[{"span":{"begin":0, "end":4, "text":"This"}, 
...
"features":[]}], "sentences":[{"span":{"begin":0, "end":24, "text":"This is a test sentence."}}], "paragraphs":[{"span":{"begin":0, "end":24, "text":"This is a test sentence."}}]}

4. Summary

It is easy to create a customize Watson Natural Language Processing Library for Embed container image and run it on IBM Cloud Code Engine.


I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

#ibmcloud, #watsonnlp, #ai, #bashscripting, #codeengine, #container

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: