Skip to content

Commit

Permalink
Add Speech Recognition v1p1beta1 samples (#33)
Browse files Browse the repository at this point in the history
* Add Speech Recognition v1p1beta1 samples

* Upgrade eslint-plugin-prettier to ^2.6.0
  • Loading branch information
beccasaurus authored and Ace Nassri committed Nov 17, 2022
1 parent 91bb10a commit 7cb813c
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
196 changes: 196 additions & 0 deletions speech/recognize.v1p1beta1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This application demonstrates how to perform basic recognize operations with
* with the Google Cloud Speech API.
*
* For more information, see the README.md under /speech and the documentation
* at https://cloud.google.com/speech/docs.
*/

'use strict';

function syncRecognizeModelSelection(
filename,
model,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_model_selection]
// Imports the Google Cloud client library
const fs = require('fs');
const speech = require('@google-cloud/speech').v1p1beta1;

// Creates a client
const client = new speech.SpeechClient();

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const model = 'Model to use, e.g. phone_call, video, default';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
model: model,
};
const audio = {
content: fs.readFileSync(filename).toString('base64'),
};

const request = {
config: config,
audio: audio,
};

// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: `, transcription);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END speech_transcribe_model_selection]
}

function syncRecognizeModelSelectionGCS(
gcsUri,
model,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_model_selection_gcs]
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech').v1p1beta1;

// Creates a client
const client = new speech.SpeechClient();

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const gcsUri = 'gs://my-bucket/audio.raw';
// const model = 'Model to use, e.g. phone_call, video, default';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
model: model,
};
const audio = {
uri: gcsUri,
};

const request = {
config: config,
audio: audio,
};

// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: `, transcription);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END speech_transcribe_model_selection_gcs]
}

require(`yargs`)
.demand(1)
.command(
`sync-model <filename> <model>`,
`Detects speech in a local audio file using provided model.`,
{},
opts =>
syncRecognizeModelSelection(
opts.filename,
opts.model,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.command(
`sync-model-gcs <gcsUri> <model>`,
`Detects speech in an audio file located in a Google Cloud Storage bucket using provided model.`,
{},
opts =>
syncRecognizeModelSelectionGCS(
opts.gcsUri,
opts.model,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.options({
encoding: {
alias: 'e',
default: 'LINEAR16',
global: true,
requiresArg: true,
type: 'string',
},
sampleRateHertz: {
alias: 'r',
default: 16000,
global: true,
requiresArg: true,
type: 'number',
},
languageCode: {
alias: 'l',
default: 'en-US',
global: true,
requiresArg: true,
type: 'string',
},
})
.example(
`node $0 sync-model ./resources/Google_Gnome.wav video -e LINEAR16 -r 16000`
)
.example(
`node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000`
)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
.help()
.strict().argv;
Binary file added speech/resources/Google_Gnome.wav
Binary file not shown.
59 changes: 59 additions & 0 deletions speech/system-test/recognize.v1p1beta1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require(`path`);
const storage = require(`@google-cloud/storage`)();
const test = require(`ava`);
const uuid = require(`uuid`);

const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);

const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node recognize.v1p1beta1.js`;
const cwd = path.join(__dirname, `..`);
const filename = `Google_Gnome.wav`;
const filepath = path.join(__dirname, `../resources/${filename}`);
const text = `the weather outside is sunny`;

test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await bucket.upload(filepath);
});

test.after.always(async () => {
const bucket = storage.bucket(bucketName);
await bucket.deleteFiles({force: true});
await bucket.deleteFiles({force: true}); // Try a second time...
await bucket.delete();
});

test(`should run sync recognize with model selection`, async t => {
const model = `video`;
const output = await runAsync(`${cmd} sync-model ${filepath} ${model}`, cwd);
t.true(output.includes(`Transcription:`));
t.true(output.includes(text));
});

test(`should run sync recognize on a GCS file with model selection`, async t => {
const model = `video`;
const output = await runAsync(
`${cmd} sync-model-gcs gs://${bucketName}/${filename} ${model}`,
cwd
);
t.true(output.includes(`Transcription:`));
t.true(output.includes(text));
});

0 comments on commit 7cb813c

Please sign in to comment.