Skip to content

Commit 9d4d447

Browse files
committed
Merge pull request #118 from chughts/sttbugfix
Fix to STT model fetch
2 parents 996baa0 + 09b4515 commit 9d4d447

5 files changed

Lines changed: 45 additions & 33 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Node-RED Watson Nodes for IBM Bluemix
33

44
[![Codacy Badge](https://api.codacy.com/project/badge/grade/e157cf8407f2442396789dc78075340a)](https://www.codacy.com/app/rezgui-y/node-red-node-watson)
55

6+
### New in version 0.4.4
7+
- Bugfixes to Speech to Text
8+
69
### New in version 0.4.3
710
- New Visual Recognition V3 node to support the V3 GA API. This incorporates the features that were previously
811
available as part of AlchemyAPI Vision.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.4.3",
3+
"version": "0.4.4",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"alchemy-api": "^1.3.0",

services/language_translation/v2.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
var first = true;
189189

190190
langs.forEach(function(language){
191-
txt += (LANGUAGES[language] ? LANGUAGES[language] : lang);
191+
txt += (LANGUAGES[language] ? LANGUAGES[language] : language);
192192
if (first) {
193193
txt += ' - ';
194194
first = false;
@@ -307,7 +307,7 @@
307307
return a.domain;
308308
});
309309
}
310-
if (domains) {
310+
if (domains && domains.length) {
311311
$('select#node-input-domain').empty();
312312
var unique_domains = domains.filter(checkUnique);
313313

services/speech_to_text/v1.html

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<input type="hidden" id="node-input-bandhidden"/>
5858
</div>
5959

60-
6160
<div class="form-row">
6261
<label for="node-input-continuous"><i class="fa fa-audio-o"></i> Continuous</label>
6362
<input type="checkbox" id="node-input-continuous"></input>
@@ -90,12 +89,15 @@
9089
</script>
9190

9291
<script type="text/javascript">
93-
var models = null;
94-
var languages = null;
95-
var bands = null;
9692

97-
var language_selected = '';
98-
var band_selected = '';
93+
// Need to simulate a namespace, as some of the variables had started to leak across nodes
94+
function STT () {
95+
var models = null;
96+
}
97+
98+
// This is the namespace for stt. Currently only contains models, but more vars and functions may need to be
99+
// moved in if there is a clash with other nodes.
100+
var stt = new STT();
99101

100102
var LANGUAGES = { 'en-US' : 'US English',
101103
'pt-BR': 'Portuguese Braziilian',
@@ -106,6 +108,12 @@
106108
'ja-JP': 'Japanese'
107109
};
108110

111+
var languages = null;
112+
var bands = null;
113+
114+
var language_selected = '';
115+
var band_selected = '';
116+
109117

110118
// sorting functions
111119
function onlyUnique(value, index, self) {
@@ -115,7 +123,7 @@
115123
// Function to be used at the start, as don't want to expose any fields, unless the models are
116124
// available. The models can only be fetched if the credentials are available.
117125
function hideEverything() {
118-
if (!models) {
126+
if (!stt.models) {
119127
$('#credentials-not-found').show();
120128
$('label#node-label-message').parent().hide();
121129
$('input#node-input-continuous').parent().hide();
@@ -127,7 +135,7 @@
127135
// Check if there is a model then can show the fields.
128136
// available. The models can only be fetched if the credentials are available.
129137
function visibilityCheck() {
130-
if (models) {
138+
if (stt.models) {
131139
$('label#node-label-message').parent().hide();
132140
$('input#node-input-continuous').parent().show();
133141
$('select#node-input-lang').parent().show();
@@ -142,22 +150,22 @@
142150

143151

144152
// Simple check that is only invoked if the service is not bound into bluemix. In this case the
145-
// user has to provide credentials. Once there are credentials, then the models are retrieved.
153+
// user has to provide credentials. Once there are credentials, then the stt.models are retrieved.
146154
function checkCredentials() {
147155
var u = $('#node-input-username').val();
148156
var p = $('#node-input-password').val();
149157

150158
if (u && u.length && p) {
151-
if (!models) {
159+
if (!stt.models) {
152160
getModels();
153161
}
154162
}
155163
}
156164

157165
// Populate the quality select field
158166
function populateBands() {
159-
if (!bands && models) {
160-
bands = models.map(function(m) {
167+
if (!bands && stt.models) {
168+
bands = stt.models.map(function(m) {
161169
return m.name.split('_')[1];
162170
});
163171
var unique_bands = bands.filter(onlyUnique);
@@ -215,12 +223,12 @@
215223
var u = $('#node-input-username').val();
216224
var p = $('#node-input-password').val();
217225

218-
$.getJSON('/watson-speech-to-text/models/', {un: u, pwd: p}).done(function (data) {
226+
$.getJSON('watson-speech-to-text/models/', {un: u, pwd: p}).done(function (data) {
219227
if (data.error) {
220228
$('label#node-label-message').parent().show();
221229
$('label#node-label-message').text(data.error);
222230
} else if (data.models) {
223-
models = data.models;
231+
stt.models = data.models;
224232
//have_credentials = true;
225233
postModelCheck();
226234
}
@@ -233,8 +241,8 @@
233241

234242
// Called to complete the languages selection table
235243
function processLanguages() {
236-
if (!languages && models) {
237-
languages = models.map(function(m) {
244+
if (!languages && stt.models) {
245+
languages = stt.models.map(function(m) {
238246
return m.language;
239247
});
240248
}
@@ -261,7 +269,7 @@
261269

262270
// Called to work through the models, completing the dyanmic selection fields.
263271
function processModels() {
264-
if (models) {
272+
if (stt.models) {
265273
processLanguages();
266274
populateBands();
267275
}
@@ -293,7 +301,7 @@
293301
.done(function (service) {
294302
restoreFromHidden();
295303
$('.credentials').toggle(!service);
296-
if (!models) {getModels(service);}
304+
if (!stt.models) {getModels(service);}
297305
else {postModelCheck();}
298306
})
299307
.fail(function () {
@@ -340,4 +348,5 @@
340348
oneditsave: oneditsave
341349
});
342350
})();
351+
343352
</script>

services/speech_to_text/v1.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (RED) {
2323
var fileType = require('file-type');
2424
var watson = require('watson-developer-cloud');
2525

26-
var service = cfenv.getAppEnv().getServiceCreds(/speech to text/i)
26+
var service = cfenv.getAppEnv().getServiceCreds(/speech to text/i);
2727

2828
// Require the Cloud Foundry Module to pull credentials from bound service
2929
// If they are found then the username and password will be stored in
@@ -77,7 +77,7 @@ module.exports = function (RED) {
7777

7878
// Utility function to perform a URL validation check
7979
function urlCheck(str) {
80-
var parsed = url.parse(str)
80+
var parsed = url.parse(str);
8181

8282
return (!!parsed.hostname && !!parsed.protocol && str.indexOf(' ') < 0);
8383
}
@@ -102,7 +102,7 @@ module.exports = function (RED) {
102102
if (err) {
103103
throw err;
104104
}
105-
cb(fileType(buf).ext)
105+
cb(fileType(buf).ext);
106106
});
107107
});
108108

@@ -139,12 +139,12 @@ module.exports = function (RED) {
139139
// console.log(a.alternatives);
140140
a.alternatives.forEach(function(t){
141141
msg.transcription += t.transcript;
142-
})
142+
});
143143
});
144144
}
145145
node.send(msg);
146146
}
147-
}
147+
};
148148

149149

150150
// Utility function that performs the speech to text service call.
@@ -177,7 +177,7 @@ module.exports = function (RED) {
177177

178178
// Everything is now in place to invoke the service
179179
speech_to_text.recognize(params, function (err, res) {
180-
node.status({})
180+
node.status({});
181181
cbdone(err,res);
182182
if (cbcleanup) {
183183
cbcleanup();
@@ -205,21 +205,21 @@ module.exports = function (RED) {
205205
if (!username || !password) {
206206
var message_err_credentials = 'Missing Speech To Text service credentials';
207207

208-
node.error(message_err_credentials, msg)
208+
node.error(message_err_credentials, msg);
209209
return;
210210
}
211211

212212
if (!config.lang) {
213213
var message_err_lang = 'Missing audio language configuration, unable to process speech.';
214214

215-
node.error(message_err_lang, msg)
215+
node.error(message_err_lang, msg);
216216
return;
217217
}
218218

219219
if (!config.band) {
220220
var message_err_band = 'Missing audio quality configuration, unable to process speech.';
221221

222-
node.error(message_err_band, msg)
222+
node.error(message_err_band, msg);
223223
return;
224224
}
225225

@@ -228,7 +228,7 @@ module.exports = function (RED) {
228228
if (!config.continuous) {
229229
var message_err_continuous = 'Missing continuous details, unable to process speech.';
230230

231-
node.error(message_err_continuous, msg)
231+
node.error(message_err_continuous, msg);
232232
return;
233233
}
234234

@@ -237,7 +237,7 @@ module.exports = function (RED) {
237237
if (!msg.payload instanceof Buffer || !typeof msg.payload === 'string') {
238238
message = 'Invalid property: msg.payload, can only be a URL or a Buffer.';
239239

240-
node.error(message, msg)
240+
node.error(message, msg);
241241
return;
242242
}
243243

@@ -262,7 +262,7 @@ module.exports = function (RED) {
262262
var message_err_format
263263
= 'Audio format (' + f + ') not supported, must be encoded as WAV, FLAC or OGG.';
264264

265-
node.error(message_err_format, msg)
265+
node.error(message_err_format, msg);
266266
return;
267267
}
268268
}

0 commit comments

Comments
 (0)