mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
gather now supports aws for transcribe as well as google
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
const Task = require('./task');
|
const Task = require('./task');
|
||||||
const {TaskName, TaskPreconditions, TranscriptionEvents} = require('../utils/constants');
|
const {
|
||||||
|
TaskName,
|
||||||
|
TaskPreconditions,
|
||||||
|
GoogleTranscriptionEvents,
|
||||||
|
AwsTranscriptionEvents
|
||||||
|
} = require('../utils/constants');
|
||||||
|
|
||||||
const makeTask = require('./make_task');
|
const makeTask = require('./make_task');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
@@ -18,10 +24,15 @@ class TaskGather extends Task {
|
|||||||
this.interim = this.partialResultCallback;
|
this.interim = this.partialResultCallback;
|
||||||
if (this.data.recognizer) {
|
if (this.data.recognizer) {
|
||||||
const recognizer = this.data.recognizer;
|
const recognizer = this.data.recognizer;
|
||||||
|
this.vendor = recognizer.vendor;
|
||||||
this.language = recognizer.language;
|
this.language = recognizer.language;
|
||||||
if (recognizer.hints && recognizer.hints.length > 0) {
|
this.hints = recognizer.hints || [];
|
||||||
this.hints = recognizer.hints.join(',');
|
this.altLanguages = recognizer.altLanguages || [];
|
||||||
}
|
|
||||||
|
/* aws options */
|
||||||
|
this.vocabularyName = recognizer.vocabularyName;
|
||||||
|
this.vocabularyFilterName = recognizer.vocabularyFilterName;
|
||||||
|
this.filterMethod = recognizer.filterMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.digitBuffer = '';
|
this.digitBuffer = '';
|
||||||
@@ -41,6 +52,8 @@ class TaskGather extends Task {
|
|||||||
async exec(cs, ep) {
|
async exec(cs, ep) {
|
||||||
await super.exec(cs);
|
await super.exec(cs);
|
||||||
this.ep = ep;
|
this.ep = ep;
|
||||||
|
if ('default' === this.vendor || !this.vendor) this.vendor = cs.speechRecognizerVendor;
|
||||||
|
if ('default' === this.language || !this.language) this.language = cs.speechRecognizerLanguage;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.sayTask) {
|
if (this.sayTask) {
|
||||||
@@ -70,8 +83,9 @@ class TaskGather extends Task {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(err, 'TaskGather:exec error');
|
this.logger.error(err, 'TaskGather:exec error');
|
||||||
}
|
}
|
||||||
ep.removeCustomEventListener(TranscriptionEvents.Transcription);
|
ep.removeCustomEventListener(GoogleTranscriptionEvents.Transcription);
|
||||||
ep.removeCustomEventListener(TranscriptionEvents.EndOfUtterance);
|
ep.removeCustomEventListener(GoogleTranscriptionEvents.EndOfUtterance);
|
||||||
|
ep.removeCustomEventListener(AwsTranscriptionEvents.Transcription);
|
||||||
}
|
}
|
||||||
|
|
||||||
kill(cs) {
|
kill(cs) {
|
||||||
@@ -91,33 +105,52 @@ class TaskGather extends Task {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _initSpeech(ep) {
|
async _initSpeech(ep) {
|
||||||
const opts = {
|
const opts = {};
|
||||||
GOOGLE_SPEECH_USE_ENHANCED: true,
|
|
||||||
GOOGLE_SPEECH_SINGLE_UTTERANCE: true,
|
if ('google' === this.vendor) {
|
||||||
GOOGLE_SPEECH_MODEL: 'command_and_search'
|
Object.assign(opts, {
|
||||||
};
|
GOOGLE_SPEECH_USE_ENHANCED: true,
|
||||||
if (this.hints) {
|
GOOGLE_SPEECH_SINGLE_UTTERANCE: true,
|
||||||
Object.assign(opts, {'GOOGLE_SPEECH_HINTS': this.hints.join(',')});
|
GOOGLE_SPEECH_MODEL: 'command_and_search'
|
||||||
|
});
|
||||||
|
if (this.hints.length > 1) opts.GOOGLE_SPEECH_HINTS = this.hints.join(',');
|
||||||
|
if (this.altLanguages.length > 1) opts.GOOGLE_SPEECH_ALTERNATIVE_LANGUAGE_CODES = this.altLanguages.join(',');
|
||||||
|
if (this.profanityFilter === true) {
|
||||||
|
Object.assign(opts, {'GOOGLE_SPEECH_PROFANITY_FILTER': true});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (this.profanityFilter === true) {
|
else {
|
||||||
Object.assign(opts, {'GOOGLE_SPEECH_PROFANITY_FILTER': true});
|
if (this.vocabularyName) opts.AWS_VOCABULARY_NAME = this.vocabularyName;
|
||||||
|
if (this.vocabularyFilterName) {
|
||||||
|
opts.AWS_VOCABULARY_NAME = this.vocabularyFilterName;
|
||||||
|
opts.AWS_VOCABULARY_FILTER_METHOD = this.filterMethod || 'mask';
|
||||||
|
}
|
||||||
|
Object.assign(opts, {
|
||||||
|
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
|
||||||
|
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
|
||||||
|
AWS_REGION: process.env.AWS_REGION
|
||||||
|
});
|
||||||
}
|
}
|
||||||
this.logger.debug(`setting freeswitch vars ${JSON.stringify(opts)}`);
|
this.logger.debug(`setting freeswitch vars ${JSON.stringify(opts)}`);
|
||||||
await ep.set(opts)
|
await ep.set(opts)
|
||||||
.catch((err) => this.logger.info(err, 'Error set'));
|
.catch((err) => this.logger.info(err, 'Error setting channel variables'));
|
||||||
ep.addCustomEventListener(TranscriptionEvents.Transcription, this._onTranscription.bind(this, ep));
|
|
||||||
ep.addCustomEventListener(TranscriptionEvents.EndOfUtterance, this._onEndOfUtterance.bind(this, ep));
|
ep.addCustomEventListener(GoogleTranscriptionEvents.Transcription, this._onTranscription.bind(this, ep));
|
||||||
|
ep.addCustomEventListener(AwsTranscriptionEvents.Transcription, this._onTranscription.bind(this, ep));
|
||||||
|
ep.addCustomEventListener(GoogleTranscriptionEvents.EndOfUtterance, this._onEndOfUtterance.bind(this, ep));
|
||||||
}
|
}
|
||||||
|
|
||||||
_startTranscribing(ep) {
|
_startTranscribing(ep) {
|
||||||
ep.startTranscription({
|
ep.startTranscription({
|
||||||
|
vendor: this.vendor,
|
||||||
|
language: this.language,
|
||||||
interim: this.partialResultCallback ? true : false,
|
interim: this.partialResultCallback ? true : false,
|
||||||
language: this.language || this.callSession.speechRecognizerLanguage
|
|
||||||
}).catch((err) => this.logger.error(err, 'TaskGather:_startTranscribing error'));
|
}).catch((err) => this.logger.error(err, 'TaskGather:_startTranscribing error'));
|
||||||
}
|
}
|
||||||
|
|
||||||
_startTimer() {
|
_startTimer() {
|
||||||
assert(!this._timeoutTimer);
|
assert(!this._timeoutTimer);
|
||||||
|
this.logger.debug(`Gather:_startTimer: timeout ${this.timeout}`);
|
||||||
this._timeoutTimer = setTimeout(() => this._resolve('timeout'), this.timeout);
|
this._timeoutTimer = setTimeout(() => this._resolve('timeout'), this.timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,8 +173,10 @@ class TaskGather extends Task {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_onTranscription(ep, evt) {
|
_onTranscription(ep, evt) {
|
||||||
|
if ('aws' === this.vendor && Array.isArray(evt) && evt.length > 0) evt = evt[0];
|
||||||
this.logger.debug(evt, 'TaskGather:_onTranscription');
|
this.logger.debug(evt, 'TaskGather:_onTranscription');
|
||||||
if (evt.is_final) this._resolve('speech', evt);
|
const final = evt.is_final;
|
||||||
|
if (final) this._resolve('speech', evt);
|
||||||
else if (this.partialResultHook) {
|
else if (this.partialResultHook) {
|
||||||
this.cs.requestor.request(this.partialResultHook, Object.assign({speech: evt}, this.cs.callInfo))
|
this.cs.requestor.request(this.partialResultHook, Object.assign({speech: evt}, this.cs.callInfo))
|
||||||
.catch((err) => this.logger.info(err, 'GatherTask:_onTranscription error'));
|
.catch((err) => this.logger.info(err, 'GatherTask:_onTranscription error'));
|
||||||
@@ -158,7 +193,8 @@ class TaskGather extends Task {
|
|||||||
this.logger.debug(`TaskGather:resolve with reason ${reason}`);
|
this.logger.debug(`TaskGather:resolve with reason ${reason}`);
|
||||||
|
|
||||||
if (this.ep && this.ep.connected) {
|
if (this.ep && this.ep.connected) {
|
||||||
this.ep.stopTranscription().catch((err) => this.logger.error({err}, 'Error stopping transcription'));
|
this.ep.stopTranscription({vendor: this.vendor})
|
||||||
|
.catch((err) => this.logger.error({err}, 'Error stopping transcription'));
|
||||||
}
|
}
|
||||||
|
|
||||||
this._clearTimer();
|
this._clearTimer();
|
||||||
|
|||||||
@@ -329,7 +329,7 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"vendor": {
|
"vendor": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["google", "aws"]
|
"enum": ["google", "aws", "default"]
|
||||||
},
|
},
|
||||||
"language": "string",
|
"language": "string",
|
||||||
"hints": "array",
|
"hints": "array",
|
||||||
|
|||||||
@@ -16,9 +16,7 @@ class TaskTranscribe extends Task {
|
|||||||
|
|
||||||
const recognizer = this.data.recognizer;
|
const recognizer = this.data.recognizer;
|
||||||
this.vendor = recognizer.vendor;
|
this.vendor = recognizer.vendor;
|
||||||
if ('default' === this.vendor || !this.vendor) this.vendor = this.callSession.speechRecognizerVendor
|
|
||||||
this.language = recognizer.language;
|
this.language = recognizer.language;
|
||||||
if ('default' === this.language || !this.language) this.language = this.callSession.speechRecognizerLanguage;
|
|
||||||
this.interim = !!recognizer.interim;
|
this.interim = !!recognizer.interim;
|
||||||
this.separateRecognitionPerChannel = recognizer.separateRecognitionPerChannel;
|
this.separateRecognitionPerChannel = recognizer.separateRecognitionPerChannel;
|
||||||
|
|
||||||
@@ -47,6 +45,8 @@ class TaskTranscribe extends Task {
|
|||||||
async exec(cs, ep, parentTask) {
|
async exec(cs, ep, parentTask) {
|
||||||
super.exec(cs);
|
super.exec(cs);
|
||||||
this.ep = ep;
|
this.ep = ep;
|
||||||
|
if ('default' === this.vendor || !this.vendor) this.vendor = cs.speechRecognizerVendor;
|
||||||
|
if ('default' === this.language || !this.language) this.language = cs.speechRecognizerLanguage;
|
||||||
try {
|
try {
|
||||||
await this._startTranscribing(ep);
|
await this._startTranscribing(ep);
|
||||||
await this.awaitTaskDone();
|
await this.awaitTaskDone();
|
||||||
|
|||||||
Reference in New Issue
Block a user