-
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.
samples: moving autoML samples from nodejs-translate and refactored t…
…hem (#432)
- Loading branch information
1 parent
1374ff1
commit 7ff7665
Showing
7 changed files
with
362 additions
and
129 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,51 @@ | ||
// 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 | ||
// | ||
// 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'; | ||
|
||
async function main(operationFullId) { | ||
// [START automl_cancel_operation_beta] | ||
|
||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const operationFullId = 'projects/YOUR_PROJECT_ID/locations/YOUR_LOCATIOIN/operations/OPERATION_ID'; | ||
|
||
// Imports the Google Cloud AutoML library | ||
const {AutoMlClient} = require('@google-cloud/automl').v1beta1; | ||
|
||
// Instantiates a client | ||
const client = new AutoMlClient(); | ||
|
||
async function cancelOperation() { | ||
client.operationsClient.cancelOperation({ | ||
name: operationFullId, | ||
}); | ||
|
||
// Wait for operation to complete. | ||
console.log('Cancelled operation'); | ||
} | ||
|
||
cancelOperation(); | ||
// [END automl_cancel_operation_beta] | ||
} | ||
|
||
main(...process.argv.slice(2)).catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); |
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 | ||
// | ||
// 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 {assert} = require('chai'); | ||
const {describe, it} = require('mocha'); | ||
const cp = require('child_process'); | ||
const uuid = require('uuid'); | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const automl = require('@google-cloud/automl'); | ||
|
||
const cmdDataset = 'node translate/automlTranslateCreateDataset.js'; | ||
const cmdModel = 'node translate/automlTranslateCreateModel.js'; | ||
const cmdPredict = 'node translate/automlTranslatePredict.js'; | ||
|
||
const projectId = process.env.AUTOML_PROJECT_ID; | ||
const datasetId = process.env.TRANSLATION_DATASET_ID; | ||
const modelId = process.env.TRANSLATION_MODEL_ID; | ||
|
||
const samplePredictionText = './translate/resources/testInput.txt'; | ||
|
||
describe('Translate AutoML sample tests', () => { | ||
it('should create and delete a dataset', async () => { | ||
const datasetDisplayName = `test_${uuid | ||
.v4() | ||
.replace(/-/g, '_') | ||
.substring(0, 20)}`; | ||
|
||
// Create dataset | ||
let output = execSync( | ||
`${cmdDataset} "${projectId}" "${datasetDisplayName}"` | ||
); | ||
|
||
//extract dataset id from the output | ||
const newDatasetId = output.split('\n')[1].split(':')[1].trim(); | ||
assert.match(output, /Dataset id:/); | ||
|
||
// Delete the created dataset | ||
output = execSync( | ||
`node delete_dataset.js ${projectId} us-central1 ${newDatasetId}` | ||
); | ||
assert.match(output, /Dataset deleted/); | ||
}); | ||
|
||
it('should create model and cancel the training operation', async () => { | ||
// create a model with pre-existing dataset | ||
let output = execSync( | ||
`${cmdModel} ${projectId} us-central1 ${datasetId} translate_test_model` | ||
); | ||
assert.match(output, /Training started../); | ||
const operationFullId = output | ||
.split('Training operation name:')[1] | ||
.split('\n')[0] | ||
.trim(); | ||
|
||
assert.include(output, operationFullId); | ||
|
||
// cancel the training LRO. | ||
output = execSync(`node beta/cancel_operation.js ${operationFullId}`); | ||
assert.match(output, /Cancelled/); | ||
}); | ||
|
||
it('should run Prediction from translation model', async () => { | ||
// Verify the model is deployed before trying to predict | ||
const client = new automl.AutoMlClient(); | ||
|
||
const modelFullId = { | ||
name: client.modelPath(projectId, 'us-central1', modelId), | ||
}; | ||
|
||
const [response] = await client.getModel(modelFullId); | ||
if (response.deploymentState !== 'DEPLOYED') { | ||
// Deploy model if it is not deployed | ||
const [operation] = await client.deployModel(modelFullId); | ||
|
||
// Wait for operation to complete. | ||
const [response] = await operation.promise(); | ||
console.log(`Model deployment finished. ${response}`); | ||
} | ||
|
||
// Run prediction on 'testInput.txt' in resources folder | ||
const output = execSync( | ||
`${cmdPredict} "${projectId}" us-central1 "${modelId}" "${samplePredictionText}" "False"` | ||
); | ||
assert.match(output, /Translated Content:/); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
// 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'; | ||
|
||
async function main(projectId, displayName) { | ||
// [START automl_translation_create_dataset] | ||
|
||
/** | ||
* Demonstrates using the AutoML client to request to create dataset for | ||
* automl translation. | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; | ||
// const displayName = '[DATASET_DISPLAY_NAME]' e.g., "my-dataset-name"; | ||
|
||
const automl = require('@google-cloud/automl'); | ||
|
||
// Create client for automl service. | ||
const client = new automl.AutoMlClient(); | ||
const computeRegion = 'us-central1'; | ||
const source = 'en'; | ||
const target = 'ja'; | ||
|
||
// A resource that represents Google Cloud Platform location. | ||
const projectLocation = client.locationPath(projectId, computeRegion); | ||
|
||
async function createDataset() { | ||
// Specify the source and target language. | ||
const datasetSpec = { | ||
sourceLanguageCode: source, | ||
targetLanguageCode: target, | ||
}; | ||
|
||
// Set dataset name and dataset specification. | ||
const datasetInfo = { | ||
displayName: displayName, | ||
translationDatasetMetadata: datasetSpec, | ||
}; | ||
|
||
// Create a dataset with the dataset specification in the region. | ||
const [operation] = await client.createDataset({ | ||
parent: projectLocation, | ||
dataset: datasetInfo, | ||
}); | ||
|
||
// wait for lro to finish | ||
const [dataset] = await operation.promise(); | ||
// Display the dataset information | ||
console.log(`Dataset name: ${dataset.name}`); | ||
console.log(`Dataset id: ${dataset.name.split('/').pop(-1)}`); | ||
} | ||
|
||
createDataset(); | ||
// [END automl_translation_create_dataset] | ||
} | ||
|
||
main(...process.argv.slice(2)).catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); |
Oops, something went wrong.