diff --git a/automl/test/automlTranslation.v1beta1.test.js b/automl/test/automlTranslation.v1beta1.test.js new file mode 100644 index 00000000000..aafc5a7ebe8 --- /dev/null +++ b/automl/test/automlTranslation.v1beta1.test.js @@ -0,0 +1,138 @@ +/** + * Copyright 2018, 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 cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const cmdDataset = 'node automlTranslationDataset.js'; +const cmdModel = 'node automlTranslationModel.js'; +const cmdPredict = 'node automlTranslationPredict.js'; + +const testDataSetName = 'testDataSet'; +const dummyDataSet = 'dummyDataSet'; +const testModelName = 'dummyModel'; +const sampleText = './resources/testInput.txt'; +const donotdeleteModelId = 'TRL188026453969732486'; + +describe.skip('automl sample tests', () => { + it(`should create a create, list, and delete a dataset`, async () => { + // Check to see that this dataset does not yet exist + let output = execSync(`${cmdDataset} list-datasets`); + assert.match(output, new RegExp(testDataSetName)); + + // Create dataset + output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`); + const dataSetId = output + .split(`\n`)[1] + .split(`:`)[1] + .trim(); + assert.match( + output, + new RegExp(`Dataset display name: ${testDataSetName}`) + ); + + // Delete dataset + output = execSync(`${cmdDataset} delete-dataset -i "${dataSetId}"`); + assert.match(output, /Dataset deleted./); + }); + + // We make two models running this test, see hard-coded workaround below + it(`should create a dataset, import data, and start making a model`, async () => { + // Check to see that this dataset does not yet exist + let output = execSync(`${cmdDataset} list-datasets`); + assert.notMatch(output, new RegExp(dummyDataSet)); + + // Create dataset + output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`); + const dataSetId = output + .split(`\n`)[1] + .split(`:`)[1] + .trim(); + assert.match(output, new RegExp(`Dataset display name: ${dummyDataSet}`)); + + // Import Data + output = execSync( + `${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv"` + ); + assert.match(output, /Data imported./); + + // Check to make sure model doesn't already exist + output = execSync(`${cmdModel} list-models`); + assert.notMatch(output, testModelName); + + // Begin training dataset, getting operation ID for next operation + output = execSync( + `${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"` + ); + const operationName = output + .split(`\n`)[0] + .split(`:`)[1] + .trim(); + assert.match(output, `Training started...`); + + // Poll operation status, here confirming that operation is not complete yet + output = execSync( + `${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"` + ); + assert.match(output, /done: false/); + }); + + it(`should run get model (from a prexisting model)`, async () => { + // Confirm dataset exists + let output = execSync(`${cmdDataset} list-datasets`); + assert.match(output, /me_do_not_delete/); + + // List model evaluations, confirm model exists + output = execSync( + `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` + ); + assert.match(output, /translationEvaluationMetrics:/); + + // Get model evaluation + output = execSync(`${cmdModel} get-model -a "${donotdeleteModelId}"`); + assert.match(output, /Model deployment state: DEPLOYED/); + }); + + it(`should run Prediction from prexisting model`, async () => { + // Confirm dataset exists + let output = execSync(`${cmdDataset} list-datasets`); + assert.match(output, /me_do_not_delete/); + + // List model evaluations, confirm model exists + output = execSync( + `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` + ); + assert.match(output, `translationEvaluationMetrics:`); + + // Run prediction on 'testImage.jpg' in resources folder + output = execSync( + `${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -t "False"` + ); + assert.match( + output, + /Translated Content: {2}これがどのように終わるか教えて/ + ); + }); + + // List datasets + it(`should list datasets`, async () => { + const output = execSync(`${cmdDataset} list-datasets`); + assert.match(output, /List of datasets:/); + }); +}); diff --git a/automl/translate/datasets/create-dataset.v1beta1.js b/automl/translate/datasets/create-dataset.v1beta1.js new file mode 100644 index 00000000000..ca006f856d7 --- /dev/null +++ b/automl/translate/datasets/create-dataset.v1beta1.js @@ -0,0 +1,71 @@ +/** + * 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 + * + * 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(projectId = 'YOUR_PROJECT_ID') { + // [START automl_translation_create_dataset] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + const computeRegion = 'us-central1'; + const datasetName = 'myDataset'; + const source = 'en'; + const target = 'ja'; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Specify the source and target language. + const datasetSpec = { + sourceLanguageCode: source, + targetLanguageCode: target, + }; + + // Set dataset name and dataset specification. + const datasetInfo = { + displayName: datasetName, + translationDatasetMetadata: datasetSpec, + }; + + // Create a dataset with the dataset specification in the region. + const [dataset] = await client.createDataset({ + parent: projectLocation, + dataset: datasetInfo, + }); + + // Display the dataset information + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log(`Translation dataset specification:`); + console.log( + `\tSource language code: ${ + dataset.translationDatasetMetadata.sourceLanguageCode + }` + ); + console.log( + `\tTarget language code: ${ + dataset.translationDatasetMetadata.targetLanguageCode + }` + ); + console.log(`Dataset create time:`); + console.log(`\tseconds: ${dataset.createTime.seconds}`); + console.log(`\tnanos: ${dataset.createTime.nanos}`); + // [END automl_translation_create_dataset] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/datasets/delete-dataset.v1beta1.js b/automl/translate/datasets/delete-dataset.v1beta1.js new file mode 100644 index 00000000000..38dc933ef4d --- /dev/null +++ b/automl/translate/datasets/delete-dataset.v1beta1.js @@ -0,0 +1,46 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + datasetId = 'YOUR_DATASET' +) { + // [START automl_translation_delete_dataset] + const automl = require(`@google-cloud/automl`); + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Delete a dataset. + const [operations] = await client.deleteDataset({name: datasetFullId}); + const operationResponses = await operations.promise(); + // The final result of the operation. + if (operationResponses[2].done === true) console.log(`Dataset deleted.`); + + // [END automl_translation_delete_dataset] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/datasets/get-dataset.v1beta1.js b/automl/translate/datasets/get-dataset.v1beta1.js new file mode 100644 index 00000000000..4d9174e0297 --- /dev/null +++ b/automl/translate/datasets/get-dataset.v1beta1.js @@ -0,0 +1,63 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + datasetId = 'YOUR_DATASET' +) { + // [START automl_translation_get_dataset] + const automl = require(`@google-cloud/automl`); + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Get complete detail of the dataset. + const [dataset] = await client.getDataset({name: datasetFullId}); + + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log(`Translation dataset specification:`); + console.log( + `\tSource language code: ${ + dataset.translationDatasetMetadata.sourceLanguageCode + }` + ); + console.log( + `\tTarget language code: ${ + dataset.translationDatasetMetadata.targetLanguageCode + }` + ); + console.log(`Dataset create time:`); + console.log(`\tseconds: ${dataset.createTime.seconds}`); + console.log(`\tnanos: ${dataset.createTime.nanos}`); + + // [END automl_translation_get_dataset] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/datasets/import-data.v1beta1.js b/automl/translate/datasets/import-data.v1beta1.js new file mode 100644 index 00000000000..eece7fb9c09 --- /dev/null +++ b/automl/translate/datasets/import-data.v1beta1.js @@ -0,0 +1,63 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + datasetId = 'YOUR_DATASET', + path = 'YOUR_PATH' +) { + // [START automl_translation_import_data] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + // const path = `string or array of .csv paths in AutoML Vision CSV format, e.g. “gs://myproject/mytraindata.csv”;` + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Get the multiple Google Cloud Storage URIs. + const inputUris = path.split(`,`); + const inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + + // Import data from the input URI. + const [operation] = await client.importData({ + name: datasetFullId, + inputConfig: inputConfig, + }); + console.log(`Processing import...`); + const operationResponses = await operation.promise(); + // The final result of the operation. + if (operationResponses[2].done === true) { + console.log(`Data imported.`); + } + + // [END automl_translation_import_data] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/datasets/list-datasets.v1beta1.js b/automl/translate/datasets/list-datasets.v1beta1.js new file mode 100644 index 00000000000..a8e3048073a --- /dev/null +++ b/automl/translate/datasets/list-datasets.v1beta1.js @@ -0,0 +1,72 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'YOUR_FILTER' +) { + // [START automl_translation_list_datasets] + const automl = require(`@google-cloud/automl`); + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const filter = `filter expressions, must specify field e.g. “imageClassificationModelMetadata:*”`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the datasets available in the region by applying filter. + const [datasets] = await client.listDatasets({ + parent: projectLocation, + filter: filter, + }); + + // Display the dataset information. + if (datasets.length === 0) { + console.log('No datasets found!'); + return; + } + console.log(`List of datasets:`); + datasets.forEach(dataset => { + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log(`Translation dataset specification:`); + console.log( + `\tSource language code: ${ + dataset.translationDatasetMetadata.sourceLanguageCode + }` + ); + console.log( + `\tTarget language code: ${ + dataset.translationDatasetMetadata.targetLanguageCode + }` + ); + console.log(`Dataset create time:`); + console.log(`\tseconds: ${dataset.createTime.seconds}`); + console.log(`\tnanos: ${dataset.createTime.nanos}`); + }); + // [END automl_translation_list_datasets] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/create-model.v1beta1.js b/automl/translate/models/create-model.v1beta1.js new file mode 100644 index 00000000000..2349930c289 --- /dev/null +++ b/automl/translate/models/create-model.v1beta1.js @@ -0,0 +1,78 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + datasetId = 'YOUR_DATASET', + modelName = 'YOUR_MODEL' +) { + // [START automl_translation_create_model] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + // const modelName = `Name of the model, e.g. "myModel"`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Set model name and dataset. + const myModel = { + displayName: modelName, + datasetId: datasetId, + translationModelMetadata: {}, + }; + + // Create a model with the model metadata in the region. + const [operation, response] = await client.createModel({ + parent: projectLocation, + model: myModel, + }); + const initialApiResponse = response; + console.log(`Training operation name: `, initialApiResponse.name); + console.log(`Training started...`); + const [model] = await operation.promise(); + // The final result of the operation. + console.log(model); + // Retrieve deployment state. + let deploymentState = ``; + if (model.deploymentState === 1) { + deploymentState = `deployed`; + } else if (model.deploymentState === 2) { + deploymentState = `undeployed`; + } + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model id: ${model.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Model create time:`); + console.log(`\tseconds: ${model.createTime.seconds}`); + console.log(`\tnanos: ${model.createTime.nanos}`); + console.log(`Model deployment state: ${deploymentState}`); + + // [END automl_translation_create_model] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/delete-model.v1beta1.js b/automl/translate/models/delete-model.v1beta1.js new file mode 100644 index 00000000000..218569de6cf --- /dev/null +++ b/automl/translate/models/delete-model.v1beta1.js @@ -0,0 +1,47 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + modelId = 'YOUR_MODEL' +) { + // [START automl_translation_delete_model] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Delete a model. + const [operation] = await client.deleteModel({name: modelFullId}); + const operationResponse = await operation.promise(); + // The final result of the operation. + if (operationResponse[2].done === true) console.log(`Model deleted.`); + + // [END automl_translation_delete_model] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/get-model-evaluation.v1beta1.js b/automl/translate/models/get-model-evaluation.v1beta1.js new file mode 100644 index 00000000000..a86c8c292ae --- /dev/null +++ b/automl/translate/models/get-model-evaluation.v1beta1.js @@ -0,0 +1,53 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + modelId = 'YOUR_MODEL', + modelEvaluationId = 'YOUR_EVALUATION_ID' +) { + // [START automl_translation_get_model_evaluation] + const automl = require(`@google-cloud/automl`); + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const modelEvaluationId = `Id of your model evaluation, e.g “ICN12345” + + // Get the full path of the model evaluation. + const modelEvaluationFullId = client.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get complete detail of the model evaluation. + const [response] = await client.getModelEvaluation({ + name: modelEvaluationFullId, + }); + console.log(response); + + // [END automl_translation_get_model_evaluation] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/get-model.v1beta1.js b/automl/translate/models/get-model.v1beta1.js new file mode 100644 index 00000000000..e9993ab136d --- /dev/null +++ b/automl/translate/models/get-model.v1beta1.js @@ -0,0 +1,91 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + modelId = 'YOUR_MODEL' +) { + // [START automl_translation_get_model] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get complete detail of the model. + const [model] = await client.getModel({name: modelFullId}); + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model id: ${model.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Model dataset id: ${model.datasetId}`); + if (model.modelMetadata === `translationModelMetadata`) { + console.log(`Translation model metadata:`); + console.log(`\tBase model: ${model.translationModelMetadata.baseModel}`); + console.log( + `\tSource language code: ${ + model.translationModelMetadata.sourceLanguageCode + }` + ); + console.log( + `\tTarget language code: ${ + model.translationModelMetadata.targetLanguageCode + }` + ); + } else if (model.modelMetadata === `textClassificationModelMetadata`) { + console.log( + `Text classification model metadata: ${ + model.textClassificationModelMetadata + }` + ); + } else if (model.modelMetadata === `imageClassificationModelMetadata`) { + console.log(`Image classification model metadata:`); + console.log( + `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}` + ); + console.log( + `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}` + ); + console.log( + `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}` + ); + console.log( + `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}` + ); + } + console.log(`Model create time:`); + console.log(`\tseconds: ${model.createTime.seconds}`); + console.log(`\tnanos: ${model.createTime.nanos}`); + console.log(`Model update time:`); + console.log(`\tseconds: ${model.updateTime.seconds}`); + console.log(`\tnanos: ${model.updateTime.nanos}`); + console.log(`Model deployment state: ${model.deploymentState}`); + + // [END automl_translation_get_model] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/get-operation-status.v1beta1.js b/automl/translate/models/get-operation-status.v1beta1.js new file mode 100644 index 00000000000..f3c1d24d444 --- /dev/null +++ b/automl/translate/models/get-operation-status.v1beta1.js @@ -0,0 +1,37 @@ +/** + * 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 + * + * 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 = 'YOUR_OPERATION_ID') { + // [START automl_translation_get_operation_status] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const operationFullId = `Full name of an operation, eg. “Projects//locations/us-central1/operations/ + + // Get the latest state of a long-running operation. + const [responses] = await client.operationsClient.getOperation( + operationFullId + ); + console.log(`Operation status: ${responses}`); + // [END automl_translation_get_operation_status] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/list-model-evaluations.v1beta1.js b/automl/translate/models/list-model-evaluations.v1beta1.js new file mode 100644 index 00000000000..17e693016f8 --- /dev/null +++ b/automl/translate/models/list-model-evaluations.v1beta1.js @@ -0,0 +1,53 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + modelId = 'YOUR_MODEL', + filter = 'YOUR_FILTER' +) { + // [START automl_translation_list_model_evaluations] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; + + // Get the full path of the model. + const modelFullId = await client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + const [elements] = client.listModelEvaluations({ + parent: modelFullId, + filter: filter, + }); + console.log(`List of model evaluations:`); + elements.forEach(element => { + console.log(element); + }); + + // [END automl_translation_list_model_evaluations] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/models/list-models.v1beta1.js b/automl/translate/models/list-models.v1beta1.js new file mode 100644 index 00000000000..af7671021cb --- /dev/null +++ b/automl/translate/models/list-models.v1beta1.js @@ -0,0 +1,64 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + filter = 'YOUR_FILTER' +) { + // [START automl_translation_list_models] + const automl = require(`@google-cloud/automl`); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const filter = `filter expressions, must specify field, e.g. "translationDatasetMetadata:*”`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the models available in the region by applying filter. + const [models] = await client.listModels({ + parent: projectLocation, + filter: filter, + }); + + // Display the model information. + console.log(`List of models:`); + models.forEach(model => { + console.log(`Model name: ${model.name}`); + console.log(`Model id: ${model.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Model dataset id: ${model.datasetId}`); + console.log(`Model create time:`); + console.log(`\tseconds: ${model.createTime.seconds}`); + console.log(`\tnanos: ${model.createTime.nanos}`); + console.log(`Model update time:`); + console.log(`\tseconds: ${model.updateTime.seconds}`); + console.log(`\tnanos: ${model.updateTime.nanos}`); + console.log(`Model deployment state: ${model.deploymentState}`); + console.log(`\n`); + }); + + // [END automl_translation_list_models] +} + +main(...process.argv.slice(2)).catch(err => console.error(err)); diff --git a/automl/translate/prediction/predict.v1beta1.js b/automl/translate/prediction/predict.v1beta1.js new file mode 100644 index 00000000000..05cdc3be706 --- /dev/null +++ b/automl/translate/prediction/predict.v1beta1.js @@ -0,0 +1,78 @@ +/** + * 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 + * + * 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION', + modelId = 'YOUR_MODEL', + filePath = 'YOUR_FILE', + translationAllowFallback = false +) { + // [START automl_translation_predict] + const automl = require(`@google-cloud/automl`); + const fs = require(`fs`); + + // Create client for prediction service. + const client = new automl.PredictionServiceClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const filePath = `local text file path of content to be classified, e.g. "./resources/test.txt"`; + // const translationAllowFallback = `use Google translation model as fallback, e.g. "False" or "True"`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Read the file content for translation. + const content = fs.readFileSync(filePath, `utf8`); + + // Set the payload by giving the content of the file. + const payload = { + textSnippet: { + content: content, + }, + }; + + // Params is additional domain-specific parameters. + // TranslationAllowFallback allows to use Google translation model. + let params = {}; + if (translationAllowFallback) { + params = { + translationAllowFallback: true, + }; + } + + const responses = await client.predict({ + name: modelFullId, + payload: payload, + params: params, + }); + + const response = responses[0]; + console.log( + `Translated Content: `, + response.payload[0].translation.translatedContent.content + ); + + // [END automl_translation_predict] +} + +main(...process.argv.slice(2)).catch(err => console.error(err));