Skip to content
This repository has been archived by the owner on Sep 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #60 from watson-developer-cloud/updates
Browse files Browse the repository at this point in the history
Add support for IAM and update dependencies
  • Loading branch information
germanattanasio authored Jul 18, 2018
2 parents 5d0de8f + 6681ab1 commit 0c0c48d
Show file tree
Hide file tree
Showing 14 changed files with 1,038 additions and 3,133 deletions.
12 changes: 9 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=<username>
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=<password>
CLASSIFIER_ID=<classifier-id>
# Environment variables
CLASSIFIER_ID=
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api
# You need to provide either username and password
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=
# OR IAM API key and URL
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=
NATURAL_LANGUAGE_CLASSIFIER_IAM_URL=
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
dist: trusty
sudo: required
node_js: 6
node_js: 8
script:
- npm run test

Expand All @@ -16,12 +16,15 @@ env:
- BX_SPACE=demos
before_deploy: npm install -g bx-blue-green
deploy:
provider: script
- provider: script
skip_cleanup: true
script:
- bx-blue-green-travis
on:
branch: master
repo: watson-developer-cloud/natural-language-classifier-nodejs
- provider: script
skip_cleanup: true
notifications:
email: false
script: npx semantic-release
on:
node: 8
61 changes: 54 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
# Questions

If you are having difficulties running the app or have a question about the service, please ask a question on [dW Answers](https://developer.ibm.com/answers/questions/ask/?topics=watson) or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson).
If you are having problems using the APIs or have a question about the IBM
Watson Services, please ask a question on
[dW Answers](https://developer.ibm.com/answers/questions/ask/?topics=watson)
or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson).

# Code

* Our style guide is based on [Google's](https://google.github.io/styleguide/jsguide.html), most of it is automaticaly enforced (and can be automatically applied with `npm run autofix`)
* Commits should follow the [Angular commit message guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines). This is because our release tool uses this format for determining release versions and generating changelogs. To make this easier, we recommend using the [Commitizen CLI](https://github.com/commitizen/cz-cli) with the `cz-conventional-changelog` adapter.

# Issues

If you encounter an issue with this sample app, you are welcome to submit a [bug report](https://github.com/watson-developer-cloud/natural-language-classifier-nodejs/issues). Before that, please search for similar issues. It's possible somebody has encountered this issue already.
If you encounter an issue with the Node.js library, you are welcome to submit
a [bug report](https://github.com/watson-developer-cloud/natural-language-classifier-nodejs/issues).
Before that, please search for similar issues. It's possible somebody has
already encountered this issue.

# Pull Requests

If you want to contribute to the repository, here's a quick guide:
If you want to contribute to the repository, follow these steps:

1. Fork the repo.
1. develop your code changes: `npm install -d`
1. Test your changes: `npm test`
1. Commit your changes
1. Push to your fork and submit a pull request
2. Develop and test your code changes: `npm install -d && npm test`.
3. Travis-CI will run the tests for all services once your changes are merged.
4. Add a test for your changes. Only refactoring and documentation changes require no new tests.
5. Make the test pass.
6. Commit your changes.
7. Push to your fork and submit a pull request.

# Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or

(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.

(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.

## Tests

Ideally, we'd like to see both unit and innervation tests on each method.
(Unit tests do not actually connect to the Watson service, integration tests do.)

Out of the box, `npm test` runs linting and unit tests, but skips the integration tests,
because they require credentials.
166 changes: 88 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,123 @@
# Natural Language Classifier Demo [![Build Status](https://travis-ci.org/watson-developer-cloud/natural-language-classifier-nodejs.svg?branch=master)](http://travis-ci.org/watson-developer-cloud/natural-language-classifier-nodejs)

The IBM Watson&trade; Natural Language Classifier service applies deep learning techniques to make predictions about the best predefined classes for short sentences or phrases. The classes can trigger a corresponding action in an application, such as directing a request to a location or person, or answering a question. After training, the service returns information for texts that it hasn't seen before. The response includes the name of the top classes and confidence values.
<h1 align="center" style="border-bottom: none;">🚀 Natural Language Classifier Sample Application</h1>
<h3 align="center">This Node.js app demonstrates some of the Natural Language Classifier service features.</h3>
<p align="center">
<a href="http://travis-ci.org/watson-developer-cloud/natural-language-classifier">
<img alt="Travis" src="https://travis-ci.org/watson-developer-cloud/natural-language-classifier.svg?branch=master">
</a>
<a href="#badge">
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
</a>
</p>
</p>

The IBM Watson&trade; Natural Language Classifier service applies deep learning techniques to make predictions about the best predefined classes for short sentences or phrases. The classes can trigger a corresponding action in an application, such as directing a request to a location or person, or answering a question. After training, the service returns information for texts that it hasn't seen before. The response includes the name of the top classes and confidence values.

![demo](public/demo.gif)

## Getting started

1. You need a Bluemix account. If you don't have one, [sign up][sign_up]. Experimental Watson Services are free to use.

2. Download and install the [Cloud-foundry CLI][cloud_foundry] tool if you haven't already.

3. Edit the `manifest.yml` file and change `<application-name>` to something unique. The name you use determines the URL of your application. For example, `<application-name>.mybluemix.net`.

```yaml
applications:
- services:
- my-service-instance
name: <application-name>
command: npm start
path: .
memory: 512M
```
You can view a [demo](https://natural-language-classifier-demo.ng.bluemix.net/) of this app.

4. Connect to Bluemix with the command line tool.

```sh
cf api https://api.ng.bluemix.net
cf login
```
## Prerequisites

5. Create and retrieve service keys to access the [Natural Language Classifier][nlc_docs] service:
1. Sign up for an [IBM Cloud account](https://console.bluemix.net/registration/).
1. Download the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview).
1. Create an instance of the Natural Language Classifier service and get your credentials:
- Go to the [Natural Language Classifier](https://console.bluemix.net/catalog/services/natural-language-classifier) page in the IBM Cloud Catalog.
- Log in to your IBM Cloud account.
- Click **Create**.
- Click **Show** to view the service credentials.
- Copy the `apikey` value, or copy the `username` and `password` values if your service instance doesn't provide an `apikey`.
- Copy the `url` value.

```none
cf create-service natural_language_classifier standard my-nlc-service
cf create-service-key my-nlc-service myKey
cf service-key my-nlc-service myKey
```
## Configuring the application

6. The Natural Language Classifier service must be trained before you can successfully use this application. The training data is provided in the file `training/weather_data_train.csv`.
Train a classifier by using the following command:
1. The Natural Language Classifier service must be trained before you can successfully use this application. The training data is provided in the file `training/weather_data_train.csv`.
If you have `username` and `password` credentials, train a classifier by using the following command:

```none
curl -i -u "<username>":"<password>" \
-F training_data=@training/weather_data_train.csv \
-F training_metadata="{\"language\":\"en\",\"name\":\"TutorialClassifier\"}" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers"
"<url>/v1/classifiers"
```
Copy `<username>` and `<password>` from step 5
Make sure to replace `<username>`, `<password>` and `<url>`.
If you have `apikey` credentials, use the word "apikey" as your username and your `apikey` as the password.
After running the command, copy the value for `classifier_id`.

7. Create a `.env` file in the root directory by copying the sample `.env.example` file using the following command:
2. In the application folder, copy the *.env.example* file and create a file called *.env*

```none
cp .env.example .env
```
You will update the `.env` with the information you retrieved in steps 5 and 6
```
cp .env.example .env
```
The `.env` file will look something like the following:
7. Open the *.env* file and add the service credentials that you obtained in the previous step.
```none
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=
CLASSIFIER_ID=
```
Example *.env* file that configures the `apikey` and `url` for a Natural Language Classifier service instance hosted in the US East region:
8. Install the dependencies you application need:
```
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=X4rbi8vwZmKpXfowaS3GAsA7vdy17Qh7km5D6EzKLHL2
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api
```
```none
npm install
```
- If your service instance uses `username` and `password` credentials, add the `NATURAL_LANGUAGE_CLASSIFIER_USERNAME` and `NATURAL_LANGUAGE_CLASSIFIER_PASSWORD` variables to the *.env* file.
9. Start the application locally:
Example *.env* file that configures the `username`, `password`, and `url` for a Natural Language Classifier service instance hosted in the Sydney region:
```none
npm start
```
```
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=522be-7b41-ab44-dec3-g1eab2ha73c6
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=A4Z5BdGENrwu8
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway-syd.watsonplatform.net/natural-language-classifier/api
```
10. Point your browser to [http://localhost:3000](http://localhost:3000).
8. Add the `CLASSIFIER_ID` to the previous properties
11. **Optional:** Push the application to Bluemix:
```
CLASSIFIER_ID=522be-7b41-ab44-dec3-g1eab2ha73c6
```
```none
cf push
```
## Running locally
After completing the steps above, you are ready to test your application. Start a browser and enter the URL of your application.
1. Install the dependencies
<your application name>.mybluemix.net
```
npm install
```
1. Run the application
For more details about developing applications that use Watson Developer Cloud services in Bluemix, see [Getting started with Watson Developer Cloud and Bluemix][getting_started].
```
npm start
```
1. View the application in a browser at `localhost:3000`
## Troubleshooting
## Deploying to IBM Cloud as a Cloud Foundry Application
* The main source of troubleshooting and recovery information is the Bluemix log. To view the log, run the following command:
1. Login to IBM Cloud with the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview)
```sh
cf logs <application-name> --recent
```
```
ibmcloud login
```
1. Target a Cloud Foundry organization and space.
* For more details about the service, see the [documentation][nlc_docs] for the Natural Language Classifier.
```
ibmcloud target --cf
```
1. Edit the *manifest.yml* file. Change the **name** field to something unique.
For example, `- name: my-app-name`.
1. Deploy the application
----
```
ibmcloud app push
```
### Directory structure
1. View the application online at the app URL.
For example: https://my-app-name.mybluemix.net
## Directory structure
```none
.
Expand All @@ -125,17 +138,14 @@ For more details about developing applications that use Watson Developer Cloud s

## License

This sample code is licensed under Apache 2.0.
This sample code is licensed under Apache 2.0.
Full license text is available in [LICENSE](LICENSE).

## Contributing

See [CONTRIBUTING](.github/CONTRIBUTING.md).
See [CONTRIBUTING](CONTRIBUTING.md).

## Open Source @ IBM
Find more open source projects on the [IBM Github Page](http://ibm.github.io/)

[cloud_foundry]: https://github.com/cloudfoundry/cli
[getting_started]: https://console.bluemix.net/docs/services/watson/index.html#about
[nlc_docs]: https://console.bluemix.net/docs/services/natural-language-classifier/getting-started.html
[sign_up]: https://console.bluemix.net/registration/?target=%2Fcatalog%2Fservices%2Fnatural-language-classifier%2F

Find more open source projects on the
[IBM Github Page](http://ibm.github.io/).
28 changes: 18 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ const NaturalLanguageClassifierV1 = require('watson-developer-cloud/natural-lang
// Bootstrap application settings
require('./config/express')(app);

const classifier = new NaturalLanguageClassifierV1({
// If unspecified here, the NATURAL_LANGUAGE_CLASSIFIER_USERNAME and
// NATURAL_LANGUAGE_CLASSIFIER_PASSWORD env properties will be checked
// After that, the SDK will fall back to the bluemix-provided VCAP_SERVICES environment property
// username: '<username>',
// password: '<password>',
});
// Create the service wrapper

app.get('/', (req, res) => {
res.render('index', {
bluemixAnalytics: !!process.env.BLUEMIX_ANALYTICS,
let classifier;

if (process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY && process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY !== '') {
classifier = new NaturalLanguageClassifierV1({
url: process.env.NATURAL_LANGUAGE_CLASSIFIER_URL || '<service-url>',
iam_apikey: process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY || '<iam_apikey>',
iam_url: process.env.ASSISTANT_IAM_URL || 'https://iam.bluemix.net/identity/token',
});
} else {
classifier = new NaturalLanguageClassifierV1({
url: process.env.NATURAL_LANGUAGE_CLASSIFIER_URL || '<service-url>',
username: process.env.NATURAL_LANGUAGE_CLASSIFIER_USERNAME || '<username>',
password: process.env.NATURAL_LANGUAGE_CLASSIFIER_PASSWORD || '<password>',
});
}

app.get('/', (req, res) => {
res.render('index');
});

/**
Expand Down
Loading

0 comments on commit 0c0c48d

Please sign in to comment.