Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/nodejs-media-translation #2808

Merged
merged 30 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5e3c0d6
feat!: initial library generation (#1)
bcoe Mar 19, 2020
fb08a99
chore: release 1.0.0 (#4)
release-please[bot] Mar 24, 2020
bad953f
feat: adds media translation file, mic samples
telpirion Mar 24, 2020
49a3b71
chore: release 2.0.0 (#18)
release-please[bot] Apr 7, 2020
2cfc3fb
feat: adds 'single_utterance' to stream from file sample (#20)
telpirion Apr 13, 2020
a89f6ac
fix: streaming mic sample
telpirion May 27, 2020
52e7a58
chore: release 2.1.0 (#27)
release-please[bot] Jun 4, 2020
5163fde
chore(deps): update dependency mocha to v8 (#49)
renovate-bot Jun 11, 2020
41ee3ad
feat(samples): demonstrate real-time conversation translation (#48)
telpirion Jun 16, 2020
d5617ba
chore: release 2.2.0 (#47)
release-please[bot] Jun 29, 2020
05e2b56
chore: release 2.2.1 (#95)
release-please[bot] Nov 12, 2020
eee3583
chore: release 2.2.2 (#98)
release-please[bot] Dec 2, 2020
cda9db5
fix: Added check to see if stream has been destroyed before writing r…
awoehler Feb 3, 2021
850a88f
chore: release 2.2.3 (#109)
release-please[bot] Mar 2, 2021
2d52a66
samples: fixes a typo (#154)
telpirion Jun 18, 2021
fcb5d38
samples: another typo in comments (#155)
telpirion Jun 22, 2021
a98aec7
chore: release 2.3.0 (#159)
release-please[bot] Jun 25, 2021
e07d9c1
chore: release 2.3.1 (#163)
release-please[bot] Jun 30, 2021
4a8eaa5
chore: release 2.3.2 (#165)
release-please[bot] Jul 12, 2021
a848ad3
chore: release 2.3.3 (#167)
release-please[bot] Jul 21, 2021
de19fee
chore: release 2.3.4 (#175)
release-please[bot] Aug 17, 2021
3b19dd4
chore: release 2.4.0 (#177)
release-please[bot] Sep 8, 2021
e96e2fa
build!: update library to use Node 12 (#225)
sofisl May 18, 2022
1842574
chore(main): release 3.0.0 (#227)
release-please[bot] Jun 20, 2022
ca9d240
chore(main): release 3.0.1 (#234)
release-please[bot] Jun 30, 2022
8a711a1
chore(main): release 3.0.2 (#245)
release-please[bot] Aug 26, 2022
ca62be6
chore(main): release 3.0.3 (#250)
release-please[bot] Sep 7, 2022
cad25b1
chore(main): release 3.0.4 (#252)
release-please[bot] Sep 22, 2022
1a2b7a4
Merge remote-tracking branch 'migration/main' into nodejs-media-trans…
kweinmeister Nov 8, 2022
4e10b57
add CI workflow and remove hyphen
kweinmeister Nov 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/mediatranslation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: mediatranslation
on:
push:
branches:
- main
paths:
- 'mediatranslation/**'
pull_request:
paths:
- 'mediatranslation/**'
pull_request_target:
types: [labeled]
schedule:
- cron: '0 0 * * 0'
jobs:
test:
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: 'write'
pull-requests: 'write'
id-token: 'write'
steps:
- uses: actions/checkout@v3.1.0
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- uses: 'google-github-actions/auth@v0.8.3'
with:
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com'
create_credentials_file: 'true'
access_token_lifetime: 600s
- uses: actions/setup-node@v3.5.1
with:
node-version: 16
- run: npm install
working-directory: mediatranslation
- run: npm test
working-directory: mediatranslation
env:
MOCHA_REPORTER_SUITENAME: mediatranslation
MOCHA_REPORTER_OUTPUT: mediatranslation_sponge_log.xml
MOCHA_REPORTER: xunit
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
await github.rest.issues.removeLabel({
name: 'actions:force-run',
owner: 'GoogleCloudPlatform',
repo: 'nodejs-docs-samples',
issue_number: context.payload.pull_request.number
});
} catch (e) {
if (!e.message.includes('Label does not exist')) {
throw e;
}
}
- if: ${{ github.event_name == 'schedule'}}
run: |
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
chmod +x ./flakybot
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
1 change: 1 addition & 0 deletions .github/workflows/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"healthcare/dicom",
"healthcare/fhir",
"healthcare/hl7v2",
"mediatranslation",
"monitoring/opencensus",
"monitoring/prometheus",
"datacatalog/cloud-client",
Expand Down
24 changes: 24 additions & 0 deletions mediatranslation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mediatranslation-samples",
"private": true,
"license": "Apache-2.0",
"author": "Google LLC",
"engines": {
"node": ">=12.0.0"
},
"files": [
"*.js"
],
"scripts": {
"test": "c8 mocha --timeout 600000 test/*.js"
},
"dependencies": {
"@google-cloud/media-translation": "^3.0.4",
"node-record-lpcm16": "1.0.1"
},
"devDependencies": {
"c8": "^7.0.0",
"chai": "^4.2.0",
"mocha": "^8.0.0"
}
}
101 changes: 101 additions & 0 deletions mediatranslation/quickstart.js
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 mediatranslation_quickstart]
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 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 targetLanguage = '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 mediatranslation_quickstart]
}
quickstart();
}

main(...process.argv.slice(2));
Binary file added mediatranslation/resources/audio.raw
Binary file not shown.
31 changes: 31 additions & 0 deletions mediatranslation/test/quickstart.test.js
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 quickstart.js';

const filePath = path.join(__dirname, '..', 'resources/audio.raw');
const exec = cmd => execSync(cmd, {encoding: 'utf-8'});

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');
});
});
31 changes: 31 additions & 0 deletions mediatranslation/test/translate_from_file.test.js
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');
});
});
102 changes: 102 additions & 0 deletions mediatranslation/translate_from_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 mediatranslation_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 targetLanguage = 'BCP-47 target language code, e.g. es-ES';

const config = {
audioConfig: {
audioEncoding: encoding,
sourceLanguageCode: sourceLanguage,
targetLanguageCode: targetLanguage,
},
single_utterance: true,
};

// 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 mediatranslation_translate_from_file]
}
translate_from_file();
}

main(...process.argv.slice(2));
Loading