Skip to content

Commit 31b5997

Browse files
Merge pull request #146 from watson-developer-cloud/examples
chore: Update examples
2 parents 6380e7a + 21a8ee9 commit 31b5997

37 files changed

+3656
-4974
lines changed

.travis.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
language: node_js
2-
node_js: 10
2+
node_js:
3+
- "10"
34
sudo: required
4-
before_install:
5-
- export CHROME_BIN=chromium-browser
6-
- export DISPLAY=:99.0
7-
- sh -e /etc/init.d/xvfb start
5+
services:
6+
- xvfb
7+
addons:
8+
firefox: "latest"
9+
chrome: "stable"
810
script:
911
- npm run build
1012
- npm test
@@ -22,8 +24,7 @@ before_deploy: npm install -g bx-blue-green
2224
deploy:
2325
- provider: script
2426
skip_cleanup: true
25-
script:
26-
- cd examples/ && bx-blue-green-travis
27+
script: cd examples/ && bx-blue-green-travis
2728
on:
2829
branch: master
2930
repo: watson-developer-cloud/speech-javascript-sdk

examples/package-lock.json

Lines changed: 3061 additions & 3955 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
"dotenv": "^6.2.0",
1616
"express": "^4.17.1",
1717
"express-browserify": "^1.0.3",
18-
"express-rate-limit": "^2.6.0",
18+
"express-rate-limit": "^5.0.0",
1919
"express-secure-only": "^0.2.1",
20+
"ibm-watson": "^4.4.0",
2021
"jquery": "^3.4.1",
2122
"object.assign": "^4.0.4",
22-
"vcap_services": "^0.7.0",
23-
"watson-developer-cloud": "^4.0.1",
23+
"vcap_services": "^0.7.1",
2424
"watson-speech": "*",
25-
"webpack": "^3.5.5",
26-
"webpack-dev-middleware": "^1.10.0",
25+
"webpack": "^4.39.3",
26+
"webpack-dev-middleware": "^3.7.1",
2727
"whatwg-fetch": "^3.0.0"
2828
},
2929
"engines": {

examples/readme.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
Watson Speech JavaScript SDK Examples
22
=====================================
33

4-
This folder has example Node.js and Python servers to generate auth tokens and
5-
several html files (in `static/`) with different examples of using the Speech SDK.
4+
This folder has example Node.js and Python servers to generate auth tokens and
5+
several html files (in `static/`) with different examples of using the Speech SDK.
66

77
There are also a few audio files to test with in the `static/` folder.
88

99

1010
Prerequisite
1111
------------
1212

13-
* IBM Watson Speech to Text service credentials - see [Service credentials for Watson services](https://www.ibm.com/watson/developercloud/doc/common/getting-started-credentials.html)
13+
* IBM Watson Speech to Text service credentials - see [Service credentials for Watson services](https://cloud.ibm.com/docs/services/watson?topic=watson-creating-credentials)
1414
* Node.js OR Python
1515

1616

@@ -22,28 +22,15 @@ Setup - Node.js
2222
3. run `npm start`
2323
4. Open your browser to http://localhost:3000/ to see the examples.
2424

25-
26-
Setup - Python
27-
--------------
28-
29-
1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask python-dotenv pyopenssl` (or `easy_install...`) to install python dependencies.
30-
2. run `./scripts/postinstall.sh` to install client-side dependencies
31-
3. edit `server.py` to include your service credentials (or create a `.env` file)
32-
4. run `python server.py`
33-
5. Open your browser to http://localhost:5000/ to see the examples.
34-
35-
3625
Notes
3726
-----
3827

3928
* The examples all use [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (a modern promise-based replacement for XMLHttpRequest) to retrieve auth tokens. Most supported browsers include a native fetch implementation, but a pollyfill is included in the top-level module for older browsers.
40-
* The examples use a Node.js server to generate tokens. It doesn't have to be written in Node.js, but *some server-side token generator is required*. The SDK will not accept your service credentials directly, and you can not use them to generate a token client-side. SDKs are available for [Node.js](https://github.com/watson-developer-cloud/node-sdk#authorization), [Java](https://github.com/watson-developer-cloud/java-sdk), [Python](https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/authorization_v1.py), and there is a [REST API](https://www.ibm.com/watson/developercloud/doc/common/getting-started-tokens.html) for use with other languages (or `curl`).
29+
* The examples use a Node.js server to generate tokens. It doesn't have to be written in Node.js, but *some server-side token generator is required*. The SDK will not accept your service credentials directly, and you can not use them to generate a token client-side. SDKs are available for [Node.js](https://github.com/watson-developer-cloud/node-sdk#authorization), [Java](https://github.com/watson-developer-cloud/java-sdk), [Python](https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/authorization_v1.py), and there is a [REST API](https://cloud.ibm.com/docs/services/watson?topic=watson-gs-tokens-watson-tokens) for use with other languages (or `curl`).
4130
* The Speech SDK may be used in browserify, Webpack, or as a standalone library. Most of the examples use the standalone version either installed via NPM or symlinked to the root directory when developing locally.
4231

4332

4433
More Examples
4534
-------------
4635

47-
* Speech to Text: [Demo](https://speech-to-text-demo.mybluemix.net/), [Code](https://github.com/watson-developer-cloud/speech-to-text-nodejs)
48-
* Speech & Dialog services together: [Demo](https://speech-dialog.mybluemix.net/), [Code](https://github.com/nfriedly/speech-dialog)
49-
* Text to Speech w/ word timings: [Demo](http://watson-tts-timing.mybluemix.net/), [Code](https://github.com/nfriedly/tts-timing)
36+
* Speech to Text: [Demo](https://speech-to-text-demo.ng.bluemix.net/), [Code](https://github.com/watson-developer-cloud/speech-to-text-nodejs)

examples/server.js

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
const express = require('express');
2222
const app = express();
23-
const AuthorizationV1 = require('watson-developer-cloud/authorization/v1');
24-
const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
25-
const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
23+
const AuthorizationV1 = require('ibm-watson/authorization/v1');
24+
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
25+
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
26+
2627
const vcapServices = require('vcap_services');
2728
const expressBrowserify = require('express-browserify');
2829
const webpackDevMiddleware = require('webpack-dev-middleware');
@@ -40,8 +41,7 @@ if (process.env.VCAP_SERVICES) {
4041

4142
const limiter = new RateLimit({
4243
windowMs: 15 * 60 * 1000, // 15 minutes
43-
max: 100, // limit each IP to 100 requests per windowMs
44-
delayMs: 0 // disable delaying - full speed until the max limit is reached
44+
max: 100 // limit each IP to 100 requests per windowMs
4545
});
4646

4747
// apply to /api/*
@@ -64,13 +64,6 @@ app.get(
6464
debug: isDev
6565
})
6666
);
67-
app.get(
68-
'/audio-video-deprecated/bundle.js',
69-
expressBrowserify('static/audio-video-deprecated/audio-video-app.js', {
70-
watch: isDev,
71-
debug: isDev
72-
})
73-
);
7467

7568
// set up webpack-dev-middleware to serve Webpack bundles for examples
7669
const compiler = webpack(webpackConfig);
@@ -80,52 +73,58 @@ app.use(
8073
})
8174
);
8275

83-
// token endpoints
84-
// **Warning**: these endpoints should probably be guarded with additional authentication & authorization for production use
76+
const sttCredentials = Object.assign(
77+
{
78+
username: process.env.SPEECH_TO_TEXT_USERNAME, // or hard-code credentials here
79+
password: process.env.SPEECH_TO_TEXT_PASSWORD,
80+
iam_apikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY, // if using an RC service
81+
url: process.env.SPEECH_TO_TEXT_URL ? process.env.SPEECH_TO_TEXT_URL : SpeechToTextV1.URL
82+
},
83+
vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
84+
);
8585

8686
// speech to text token endpoint
87-
var sttAuthService = new AuthorizationV1(
88-
Object.assign(
89-
{
90-
username: process.env.SPEECH_TO_TEXT_USERNAME, // or hard-code credentials here
91-
password: process.env.SPEECH_TO_TEXT_PASSWORD,
92-
iam_apikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY, // if using an RC service
93-
url: process.env.SPEECH_TO_TEXT_URL ? process.env.SPEECH_TO_TEXT_URL : SpeechToTextV1.URL
94-
},
95-
vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
96-
)
97-
);
9887
app.use('/api/speech-to-text/token', function(req, res) {
99-
sttAuthService.getToken(function(err, token) {
88+
const sttAuthService = new AuthorizationV1(sttCredentials);
89+
sttAuthService.getToken(function(err, response) {
10090
if (err) {
10191
console.log('Error retrieving token: ', err);
10292
res.status(500).send('Error retrieving token');
10393
return;
10494
}
105-
res.send(token.token || token);
95+
const token = response.token || response;
96+
if (process.env.SPEECH_TO_TEXT_IAM_APIKEY) {
97+
res.json({ access_token: token, url: sttCredentials.url });
98+
} else {
99+
res.json({ token: token, url: sttCredentials.url });
100+
}
106101
});
107102
});
108103

109104
// text to speech token endpoint
110-
var ttsAuthService = new AuthorizationV1(
111-
Object.assign(
112-
{
113-
username: process.env.TEXT_TO_SPEECH_USERNAME, // or hard-code credentials here
114-
password: process.env.TEXT_TO_SPEECH_PASSWORD,
115-
iam_apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY, // if using an RC service
116-
url: process.env.TEXT_TO_SPEECH_URL ? process.env.TEXT_TO_SPEECH_URL : TextToSpeechV1.URL
117-
},
118-
vcapServices.getCredentials('text_to_speech') // pulls credentials from environment in bluemix, otherwise returns {}
119-
)
105+
const ttsCredentials = Object.assign(
106+
{
107+
username: process.env.TEXT_TO_SPEECH_USERNAME, // or hard-code credentials here
108+
password: process.env.TEXT_TO_SPEECH_PASSWORD,
109+
iam_apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY, // if using an RC service
110+
url: process.env.TEXT_TO_SPEECH_URL ? process.env.TEXT_TO_SPEECH_URL : TextToSpeechV1.URL
111+
},
112+
vcapServices.getCredentials('text_to_speech') // pulls credentials from environment in bluemix, otherwise returns {}
120113
);
121114
app.use('/api/text-to-speech/token', function(req, res) {
122-
ttsAuthService.getToken(function(err, token) {
115+
const ttsAuthService = new AuthorizationV1(ttsCredentials);
116+
ttsAuthService.getToken(function(err, response) {
123117
if (err) {
124118
console.log('Error retrieving token: ', err);
125119
res.status(500).send('Error retrieving token');
126120
return;
127121
}
128-
res.send(token.token || token);
122+
const token = response.token || response;
123+
if (process.env.TEXT_TO_SPEECH_IAM_APIKEY) {
124+
res.json({ access_token: token, url: ttsCredentials.url });
125+
} else {
126+
res.json({ token: token, url: ttsCredentials.url });
127+
}
129128
});
130129
});
131130

examples/server.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

examples/static/audio-video-deprecated/README.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/static/audio-video-deprecated/audio-video-app.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/static/audio-video-deprecated/index.html

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)