-
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.
docs: add translate ga samples (#296)
* docs: add translate ga samples * update package.json * add missing file * Add license headers
- Loading branch information
Showing
7 changed files
with
336 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Tell me how this ends |
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,56 @@ | ||
// Copyright 2019 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 {assert} = require('chai'); | ||
const {AutoMlClient} = require('@google-cloud/automl').v1; | ||
|
||
const cp = require('child_process'); | ||
const uuid = require('uuid'); | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const CREATE_DATASET_REGION_TAG = 'translate_create_dataset'; | ||
const LOCATION = 'us-central1'; | ||
|
||
describe('Automl Translate Create Dataset Test', () => { | ||
const client = new AutoMlClient(); | ||
let datasetId; | ||
|
||
it('should create a dataset', async () => { | ||
const projectId = await client.getProjectId(); | ||
const displayName = `test_${uuid | ||
.v4() | ||
.replace(/-/g, '_') | ||
.substring(0, 26)}`; | ||
|
||
// create | ||
const create_output = execSync( | ||
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` | ||
); | ||
assert.match(create_output, /Dataset id:/); | ||
|
||
datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; | ||
}); | ||
|
||
after('delete created dataset', async () => { | ||
const projectId = await client.getProjectId(); | ||
const request = { | ||
name: client.datasetPath(projectId, LOCATION, datasetId), | ||
}; | ||
const [operation] = await client.deleteDataset(request); | ||
await operation.promise(); | ||
}); | ||
}); |
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,48 @@ | ||
// Copyright 2019 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 {assert} = require('chai'); | ||
const {AutoMlClient} = require('@google-cloud/automl').v1; | ||
|
||
const cp = require('child_process'); | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const CREATE_MODEL_REGION_TAG = 'translate_create_model'; | ||
const LOCATION = 'us-central1'; | ||
const DATASET_ID = 'TRL8522556519449886720'; | ||
|
||
describe('Automl Translate Create Model Tests', () => { | ||
const client = new AutoMlClient(); | ||
let operationId; | ||
|
||
it('should create a model', async () => { | ||
const projectId = await client.getProjectId(); | ||
const create_output = execSync( | ||
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} translation_test_create_model` | ||
); | ||
|
||
assert.match(create_output, /Training started/); | ||
|
||
operationId = create_output | ||
.split('Training operation name: ')[1] | ||
.split('\n')[0]; | ||
}); | ||
|
||
after('cancel model training', async () => { | ||
await client.operationsClient.cancelOperation({name: operationId}); | ||
}); | ||
}); |
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,39 @@ | ||
// Copyright 2019 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 {assert} = require('chai'); | ||
const {AutoMlClient} = require('@google-cloud/automl').v1; | ||
|
||
const cp = require('child_process'); | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const MODEL_ID = 'TRL1218052175389786112'; | ||
const PREDICT_REGION_TAG = 'translate_predict'; | ||
const LOCATION = 'us-central1'; | ||
|
||
describe('Automl Translate Predict Test', () => { | ||
const client = new AutoMlClient(); | ||
|
||
it('should predict', async () => { | ||
const projectId = await client.getProjectId(); | ||
|
||
const list_output = execSync( | ||
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} resources/input.txt` | ||
); | ||
assert.match(list_output, /Translated content:/); | ||
}); | ||
}); |
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,68 @@ | ||
// Copyright 2019 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'; | ||
|
||
function main( | ||
projectId = 'YOUR_PROJECT_ID', | ||
location = 'us-central1', | ||
displayName = 'YOUR_DISPLAY_NAME' | ||
) { | ||
// [START automl_translate_create_dataset] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'us-central1'; | ||
// const displayName = 'YOUR_DISPLAY_NAME'; | ||
|
||
// Imports the Google Cloud AutoML library | ||
const {AutoMlClient} = require(`@google-cloud/automl`).v1; | ||
|
||
// Instantiates a client | ||
const client = new AutoMlClient(); | ||
|
||
async function createDataset() { | ||
// Construct request | ||
const request = { | ||
parent: client.locationPath(projectId, location), | ||
dataset: { | ||
displayName: displayName, | ||
translationDatasetMetadata: { | ||
sourceLanguageCode: 'en', | ||
targetLanguageCode: 'ja', | ||
}, | ||
}, | ||
}; | ||
|
||
// Create dataset | ||
const [operation] = await client.createDataset(request); | ||
|
||
// Wait for operation to complete. | ||
const [response] = await operation.promise(); | ||
|
||
console.log(`Dataset name: ${response.name}`); | ||
console.log(` | ||
Dataset id: ${ | ||
response.name | ||
.split('/') | ||
[response.name.split('/').length - 1].split('\n')[0] | ||
}`); | ||
} | ||
|
||
createDataset(); | ||
// [END automl_translate_create_dataset] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
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,59 @@ | ||
// Copyright 2019 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'; | ||
|
||
function main( | ||
projectId = 'YOUR_PROJECT_ID', | ||
location = 'us-central1', | ||
datasetId = 'YOUR_DATASET_ID', | ||
displayName = 'YOUR_DISPLAY_NAME' | ||
) { | ||
// [START automl_translate_create_model] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'us-central1'; | ||
// const dataset_id = 'YOUR_DATASET_ID'; | ||
// const displayName = 'YOUR_DISPLAY_NAME'; | ||
|
||
// Imports the Google Cloud AutoML library | ||
const {AutoMlClient} = require(`@google-cloud/automl`).v1; | ||
|
||
// Instantiates a client | ||
const client = new AutoMlClient(); | ||
|
||
async function createModel() { | ||
// Construct request | ||
const request = { | ||
parent: client.locationPath(projectId, location), | ||
model: { | ||
displayName: displayName, | ||
datasetId: datasetId, | ||
translationModelMetadata: {}, // Leave unset, to use the default base model | ||
}, | ||
}; | ||
|
||
// Don't wait for the LRO | ||
const [operation] = await client.createModel(request); | ||
console.log(`Training started...`); | ||
console.log(`Training operation name: ${operation.name}`); | ||
} | ||
|
||
createModel(); | ||
// [END automl_translate_create_model] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
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,65 @@ | ||
// Copyright 2019 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'; | ||
|
||
function main( | ||
projectId = 'YOUR_PROJECT_ID', | ||
location = 'us-central1', | ||
modelId = 'YOUR_MODEL_ID', | ||
filePath = 'path_to_local_file.txt' | ||
) { | ||
// [START automl_translate_predict] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'us-central1'; | ||
// const modelId = 'YOUR_MODEL_ID'; | ||
// const filePath = 'path_to_local_file.txt'; | ||
|
||
// Imports the Google Cloud AutoML library | ||
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1; | ||
const fs = require(`fs`); | ||
|
||
// Instantiates a client | ||
const client = new PredictionServiceClient(); | ||
|
||
// Read the file content for translation. | ||
const content = fs.readFileSync(filePath, `utf8`); | ||
|
||
async function predict() { | ||
// Construct request | ||
const request = { | ||
name: client.modelPath(projectId, location, modelId), | ||
payload: { | ||
textSnippet: { | ||
content: content, | ||
}, | ||
}, | ||
}; | ||
|
||
const [response] = await client.predict(request); | ||
|
||
console.log( | ||
`Translated content: `, | ||
response.payload[0].translation.translatedContent.content | ||
); | ||
} | ||
|
||
predict(); | ||
// [END automl_translate_predict] | ||
} | ||
|
||
main(...process.argv.slice(2)); |