RUN WATSON NLP FOR EMBED ON IBM CLOUD CODE ENGINE
Tags: ibmcloud, watsonnlp, bashscripting, codeengine
Published: 2023-01-04 18:41:25

---
title: RUN WATSON NLP FOR EMBED ON IBM CLOUD CODE ENGINE
published: true
description: This blog post is about using the [IBM Watson Natural Language Processing Library for Embed](https://www.ibm.com/docs/en/watson-libraries?topic=watson-natural-language-processing-library-embed-home) on [IBM Cloud Code Engine](https://www.ibm.com/cloud/code-engine) and is related to my blog post [Run Watson NLP for Embed on your local computer with Docker](https://suedbroecker.net/2022/12/15/run-watson-nlp-for-embed-on-your-local-computer-with-docker/).
tags: #ibmcloud #watsonnlp #bashscripting #codeengine
# cover_image: https://suedbroecker.files.wordpress.com/2022/12/artificial-intelligence-2167835_1920.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-01-04 18:29 +0000
---

This blog post is about using the [IBM Watson Natural Language Processing Library for Embed](https://www.ibm.com/docs/en/watson-libraries?topic=watson-natural-language-processing-library-embed-home) on [IBM Cloud Code Engine](https://www.ibm.com/cloud/code-engine) and is related to my blog post [Run Watson NLP for Embed on your local computer with Docker](https://suedbroecker.net/2022/12/15/run-watson-nlp-for-embed-on-your-local-computer-with-docker/). [IBM Cloud Code Engine](https://www.ibm.com/cloud/code-engine) is a fully managed, serverless platform where you can run container images or [batch jobs](https://www.ibm.com/cloud/blog/running-batch-jobs-in-ibm-cloud-code-engine).

The [IBM Watson Libraries for Embed](https://www.ibm.com/docs/en/watson-libraries?topic=watson-natural-language-processing-library-embed-home) are made for IBM Business Partners. Partners can get additional details about `embeddable AI` on the [IBM Partner World](https://www.ibm.com/partnerworld/program/embeddableai) page. If you are an IBM Business Partner you can get a free access to the [`IBM Watson Natural Language Processing Library for Embed`](https://www.ibm.com/account/reg/us-en/signup?formid=urx-51726).


To get started with the libraries you can use the link [`Watson Natural Language Processing Library for Embed home`](https://www.ibm.com/docs/en/watson-libraries?topic=watson-natural-language-processing-library-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`](https://github.com/thomassuedbroecker/watson-nlp-example-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](https://www.ibm.com/docs/en/watson-libraries?topic=models-catalog), the catalog is public available.

There are runtime containers for the AI functionalities like **NLP**, **Speech 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](https://www.ibm.com/cloud/container-registry). The image below shows a simplified overview.

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-02-1.png?w=541)



The next gif shows a simplified overview of the dependencies and steps we need to execute when we using [IBM Cloud Code Engine](https://www.ibm.com/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](https://www.ibm.com/docs/en/watson-libraries?topic=installing-running-containers).

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-01.gif?w=619)



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**](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/Dockerfile) and we use the **IBM entitlement key** to access the runtime image and the models on the [IBM Cloud Container Registry](https://www.ibm.com/cloud/container-registry).
2. We use the [IBM Cloud Container Registry](https://www.ibm.com/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](https://cloud.ibm.com/docs/Registry?topic=Registry-troubleshoot-add-namespace).
3. We create a **Code Engine project**. [The project must be unique in the IBM Cloud region of Code Engine](https://cloud.ibm.com/docs/codeengine?topic=codeengine-manage-project). 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](https://cloud.ibm.com/docs/account?topic=account-userapikey&interface=ui) to do that task.
4. Now we configure a [**Code Engine application**](https://cloud.ibm.com/docs/codeengine?topic=codeengine-application-workloads). 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](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L201)** 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](https://developer.ibm.com/apis/catalog/embeddableai--watson-natural-language-processing-apis/Introduction/).

## 2. SOME SCREEN SHOTS FROM A RUNNING EXAMPLE ON CODE ENGINE

- [IBM Cloud Container Registry namespace](https://cloud.ibm.com/registry/namespaces) and the uploaded container image:

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-02.png?w=1024)



- The created [IBM Cloud API key](https://cloud.ibm.com/iam/apikeys):

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-03.png?w=1024)



- The created [Code Engine project](https://cloud.ibm.com/codeengine/projects):

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-04.png?w=1024)



- The created [Code Engine application](https://cloud.ibm.com/docs/codeengine?topic=codeengine-application-workloads):

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-05.png?w=1024)



- The created [Code Engine `Registry Access`](https://cloud.ibm.com/docs/codeengine?topic=codeengine-add-registry):

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-06.png?w=1024)



- The created [Envionment variable](https://cloud.ibm.com/docs/codeengine?topic=codeengine-cli#cli-application-create):

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-07.png?w=1024)



- The mapped custom Watson NLP container image:

![img](https://suedbroecker.files.wordpress.com/2022/12/watson-nlp-ce-08.png?w=1024)



## 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

```bash
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

```bash
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`.

```bash
# 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`](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh) BASH SCRIPT

> The bash script execution can take several minutes.

```bash
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

- [Connect to IBM Container Registry for the Watson NLP resources](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L42)
- [Load the models from the IBM Container Registry](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L69)
- [Log on to IBM Cloud](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L104)
- [Log on to IBM Cloud Container Registry](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L117)
- [Push the container image to the IBM Cloud Container Registry](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L131)
- [Create and configure a IBM Cloud Code Engine project to use the IBM Cloud Container Registry](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L151)
- [Create a Code Engine application with the needed secret and environment variable](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L187)
- [Verify the running custom Watson NLP image](https://github.com/thomassuedbroecker/watson-nlp-example-code-engine/blob/main/code/run-watson-nlp-with-code-engine.sh#L211)

#### EXAMPLE OUTPUT

```bash
# ******
# 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`](https://www.ibm.com/docs/en/watson-libraries?topic=watson-natural-language-processing-library-embed-home) 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

Original Blog Post: [RUN WATSON NLP FOR EMBED ON IBM CLOUD CODE ENGINE](https://suedbroecker.net/2022/12/21/run-watson-nlp-for-embed-on-ibm-cloud-code-engine/)