-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds media translation file, mic samples
* feat: adds streaming samples * fix: linter errors * fix: per reviewer * fix: per reviewer * fix: updates package.json
- Loading branch information
1 parent
518e44b
commit 28ee4a0
Showing
7 changed files
with
355 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,101 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Copyright 2020, Google LLC. | ||
// 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 | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// https://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'; | ||
|
||
async function quickstart() { | ||
// parent = 'projects/my-project' // Project to list dashboards for. | ||
/** | ||
* Translate text from an audio file. | ||
* @param {string} filename local path to | ||
* @param {string} encoding the encoding of the audio rate, e.g. Linear16 | ||
* @param {string} sourceLanguage language translating from, as BCP-47 code | ||
* @param {string} targetLanguage languate translating to, as BCP-47 code | ||
*/ | ||
function main(filename, encoding, sourceLanguage, targetLanguage) { | ||
// [START media_translation_quickstart] | ||
// Imports the Google Cloud client library | ||
const fs = require('fs'); | ||
|
||
// Imports the CLoud Media Translation client library | ||
const { | ||
SpeechTranslationServiceClient, | ||
} = require('@google-cloud/media-translation'); | ||
|
||
// Creates a client | ||
const translate = new SpeechTranslationServiceClient(); | ||
const client = new SpeechTranslationServiceClient(); | ||
|
||
async function quickstart() { | ||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; | ||
// const encoding = 'Encoding of the audio file, e.g. LINEAR16'; | ||
// const sourceLanguage = 'BCP-47 source language code, e.g. en-US'; | ||
// const targetLangauge = 'BCP-47 target language code, e.g. es-ES'; | ||
|
||
const config = { | ||
audioConfig: { | ||
audioEncoding: encoding, | ||
sourceLanguageCode: sourceLanguage, | ||
targetLanguageCode: targetLanguage, | ||
}, | ||
}; | ||
|
||
// First request needs to have only a streaming config, no data. | ||
const initialRequest = { | ||
streamingConfig: config, | ||
audioContent: null, | ||
}; | ||
|
||
const readStream = fs.createReadStream(filename, { | ||
highWaterMark: 4096, | ||
encoding: 'base64', | ||
}); | ||
|
||
// parent = 'projects/my-project', // Project to list dashboards for. | ||
const chunks = []; | ||
readStream | ||
.on('data', chunk => { | ||
const request = { | ||
streamingConfig: config, | ||
audioContent: chunk.toString(), | ||
}; | ||
chunks.push(request); | ||
}) | ||
.on('close', () => { | ||
// Config-only request should be first in stream of requests | ||
stream.write(initialRequest); | ||
for (let i = 0; i < chunks.length; i++) { | ||
stream.write(chunks[i]); | ||
} | ||
stream.end(); | ||
}); | ||
|
||
// TODO: add an actual sample. | ||
console.info(translate); | ||
const stream = client.streamingTranslateSpeech().on('data', response => { | ||
const {result} = response; | ||
if (result.textTranslationResult.isFinal) { | ||
console.log( | ||
`\nFinal translation: ${result.textTranslationResult.translation}` | ||
); | ||
console.log(`Final recognition result: ${result.recognitionResult}`); | ||
} else { | ||
console.log( | ||
`\nPartial translation: ${result.textTranslationResult.translation}` | ||
); | ||
console.log(`Partial recognition result: ${result.recognitionResult}`); | ||
} | ||
}); | ||
|
||
// [END media_translation_quickstart] | ||
// [END media_translation_quickstart] | ||
} | ||
quickstart(); | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
quickstart(...args).catch(console.error); | ||
main(...process.argv.slice(2)); |
Binary file not shown.
25 changes: 10 additions & 15 deletions
25
media-translation/test/quickstart.js → media-translation/test/quickstart.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
// Copyright 2020 Google LLC | ||
// Copyright 2017 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// https://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 {assert} = require('chai'); | ||
const cp = require('child_process'); | ||
const {describe, it} = require('mocha'); | ||
const execSync = require('child_process').execSync; | ||
const cmd = 'node quickstart.js'; | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const cwd = path.join(__dirname, '..'); | ||
|
||
const PROJECT_ID = process.env.GCLOUD_PROJECT; | ||
const filePath = path.join(__dirname, '..', 'resources/audio.raw'); | ||
const exec = cmd => execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
describe('Sample Integration Tests', () => { | ||
it('should run quickstart.js', async () => { | ||
const stdout = execSync(`node ./quickstart.js projects/${PROJECT_ID}`, { | ||
cwd, | ||
}); | ||
assert(stdout); | ||
describe('Quickstart', () => { | ||
it('should translate from a streamed file', async () => { | ||
const stdout = exec(`${cmd} ${filePath} linear16 en-US es-ES`); | ||
assert.include(stdout, 'Partial translation'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2017 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 {assert} = require('chai'); | ||
const {describe, it} = require('mocha'); | ||
const execSync = require('child_process').execSync; | ||
const cmd = 'node translate_from_file.js'; | ||
|
||
const filePath = path.join(__dirname, '..', 'resources/audio.raw'); | ||
const exec = cmd => execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
describe('MediaTranslation', () => { | ||
it('should translate from a streamed file', async () => { | ||
const stdout = exec(`${cmd} ${filePath} linear16 en-US es-ES`); | ||
assert.include(stdout, 'Partial translation'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2020, Google LLC. | ||
// 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 | ||
// | ||
// https://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'; | ||
|
||
/** | ||
* Translate text from an audio file. | ||
* @param {string} filename local path to | ||
* @param {string} encoding the encoding of the audio rate, e.g. Linear16 | ||
* @param {string} sourceLanguage language translating from, as BCP-47 code | ||
* @param {string} targetLanguage languate translating to, as BCP-47 code | ||
*/ | ||
function main(filename, encoding, sourceLanguage, targetLanguage) { | ||
// [START media_translation_translate_from_file] | ||
const fs = require('fs'); | ||
|
||
// Imports the CLoud Media Translation client library | ||
const { | ||
SpeechTranslationServiceClient, | ||
} = require('@google-cloud/media-translation'); | ||
|
||
// Creates a client | ||
const client = new SpeechTranslationServiceClient(); | ||
|
||
async function translate_from_file() { | ||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; | ||
// const encoding = 'Encoding of the audio file, e.g. LINEAR16'; | ||
// const sourceLanguage = 'BCP-47 source language code, e.g. en-US'; | ||
// const targetLangauge = 'BCP-47 target language code, e.g. es-ES'; | ||
|
||
const config = { | ||
audioConfig: { | ||
audioEncoding: encoding, | ||
sourceLanguageCode: sourceLanguage, | ||
targetLanguageCode: targetLanguage, | ||
}, | ||
}; | ||
|
||
// First request needs to have only a streaming config, no data. | ||
const initialRequest = { | ||
streamingConfig: config, | ||
audioContent: null, | ||
}; | ||
|
||
const readStream = fs.createReadStream(filename, { | ||
highWaterMark: 4096, | ||
encoding: 'base64', | ||
}); | ||
|
||
const chunks = []; | ||
readStream | ||
.on('data', chunk => { | ||
const request = { | ||
streamingConfig: config, | ||
audioContent: chunk.toString(), | ||
}; | ||
chunks.push(request); | ||
}) | ||
.on('close', () => { | ||
// Config-only request should be first in stream of requests | ||
stream.write(initialRequest); | ||
for (let i = 0; i < chunks.length; i++) { | ||
stream.write(chunks[i]); | ||
} | ||
stream.end(); | ||
}); | ||
|
||
const stream = client.streamingTranslateSpeech().on('data', response => { | ||
const {result} = response; | ||
if (result.textTranslationResult.isFinal) { | ||
console.log( | ||
`\nFinal translation: ${result.textTranslationResult.translation}` | ||
); | ||
console.log(`Final recognition result: ${result.recognitionResult}`); | ||
} else { | ||
console.log( | ||
`\nPartial translation: ${result.textTranslationResult.translation}` | ||
); | ||
console.log(`Partial recognition result: ${result.recognitionResult}`); | ||
} | ||
}); | ||
|
||
// [END media_translation_translate_from_file] | ||
} | ||
translate_from_file(); | ||
} | ||
|
||
main(...process.argv.slice(2)); |
Oops, something went wrong.