Tutorial
Set up a UI that allows for rapid deployment and testing of Watson Natural Language Understanding models
Create a web app that takes in text and analyzes it for sentiment using the IBM Watson Natural Language Understanding APIsIn this tutorial, learn how to create a web app that takes in text and analyzes the text for sentiment by using the IBM Watson Natural Language Understanding APIs. After you enter text, the text is analyzed based on the latest loaded model into a Natural Language Understanding Service. You also learn how to deploy this application on a Red Hat OpenShift cluster by using the Developer tools.
The main benefit of using the Natural Language Understanding Service is its powerful analytics engine that provides cognitive enrichments and insights into data. Watson Natural Language Understanding uses deep learning to extract meaning and metadata from unstructured text data. It uses text analytics to extract categories, classification, entities, keywords, sentiment, emotion, relations, and syntax.
Note: The React front end was created from the Create React App project.

In the demo:
- You add text to the application (running locally or in IBM Cloud).
- The application requests the latest model from the IBM Watson Natural Language Understanding Service.
- The application requests the user inputted text to be analyzed using the latest model.
- The Watson Natural Language Understanding Service processes the text and extracts features such as keywords, concepts, and categories.
- The results are output to the application.
Prerequisites
To follow this tutorial, you need:
- An IBM Cloud account
- The IBM Cloud CLI
Estimated time
It should take you approximately 2 hours to complete the tutorial.
Steps
Step 1. Provision services and set up the model
First, you set up the services on IBM Cloud and use IBM Watson Studio to create a model to load into the Watson Natural Language Understanding Service.
You can follow the Build a recommendation engine with Watson Natural Language Understanding tutorial to learn how to do this.
After you get to the "Deploy model to Watson Natural Language Understanding" heading, you can use the demo in this tutorial to analyze text instead of the curl commands.
Step 2. Create credentials
- Open your Natural Language Understanding Service console.
- Select Service Credentials.
- Click New Credential and Add on the dialog box if you don't already have them. Be sure they have Manager authority.
- Click the v next to the credential to open and see the JSON file.
- Copy the
apikeyvalue to use in the next step. - Copy the
urlvalue to use in the next step.
After the credentials are created, they are available on the Natural Language Understanding Service Manage page.

Step 3. Run the application
Run locally
Note: The node version must be at 12.22.12 or higher for the app to run.
Clone the following directory.
git clone https://github.com/ibm-build-lab/NLU-demo-with-ui cd nlu-demo-with-uiSet the environment variables. Create a
.envfile in this top-level directory and add the following code to it.REACT_APP_apikey=<nlu-service-api-key> REACT_APP_nlu_url=<nlu-service-url> PORT=3000Compile and start the application.
npm install npm startThe application runs on
localhost:3000.
Deploy to IBM Cloud
Provision a Red Hat OpenShift cluster in your IBM Cloud account.
Open the Red Hat OpenShift console by selecting OpenShift web console at the top of the cluster detail page.

Create a new project called
nlu-demo.
From the Red Hat OpenShift console, go into the Developer view, select the
nlu-demoproject, select +Add, and chooseImport From Git.
Be sure to use
Dockerfileas the method of Import strategy. Note that on Red Hat OpenShift Version 8, this choice is made from the Git Repository card. On later versions, it is a selection from within the Import from Git page.
On the Import from Git page, enter this repo under Git Repo URL. You can keep all the other defaults or change the name.
- Select Create.
Go to Topology, and open the newly created app icon. You can watch the progress.

Under Builds, you should see a Build Config. Open it, navigate to the Environment tab, add the
REACT_APP_apikeyandREACT_APP_nlu_urlenvironment variables, and start a new build.
After the build is complete, select the Location link under the Routes section to see the application.
Usage
Simply insert text into the text box, click Submit, and you see the sentiment of the text.

Next Steps
To change what is displayed from the analyzation, simply edit the src/App.js file.
Locate the following line (approximately line 82).
document.getElementById("results").textContent = JSON.stringify(body.sentiment.document, null, 4);Change
body.sentiment.documenttobodyto see the entire response.Note: If you deployed to the cloud and want to change the code, you must fork the repo and push your updated code to your new repo, then deploy that repo to the Red Hat OpenShift cluster. Every time you change the code in the GitHub repo, you must restart a new build.
If you want to test another model, simply load the model into the Natural Language Understanding Service and use the application to analyze text.
Summary
Using Watson Studio, you can create models and load them into your Watson Natural Language Understanding Service. This application lets you quickly analyze text based on the models you load. You can also look into the code to see how to call the Natural Language Understanding APIs from a React application.