Skip to content

Commit 02db4ca

Browse files
callmehiphopAce Nassri
authored and
Ace Nassri
committed
docs: add translate samples (#160)
1 parent 42e987d commit 02db4ca

14 files changed

+954
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Copyright 2018, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const cp = require('child_process');
20+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
const cmdDataset = 'node automlTranslationDataset.js';
24+
const cmdModel = 'node automlTranslationModel.js';
25+
const cmdPredict = 'node automlTranslationPredict.js';
26+
27+
const testDataSetName = 'testDataSet';
28+
const dummyDataSet = 'dummyDataSet';
29+
const testModelName = 'dummyModel';
30+
const sampleText = './resources/testInput.txt';
31+
const donotdeleteModelId = 'TRL188026453969732486';
32+
33+
describe.skip('automl sample tests', () => {
34+
it(`should create a create, list, and delete a dataset`, async () => {
35+
// Check to see that this dataset does not yet exist
36+
let output = execSync(`${cmdDataset} list-datasets`);
37+
assert.match(output, new RegExp(testDataSetName));
38+
39+
// Create dataset
40+
output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
41+
const dataSetId = output
42+
.split(`\n`)[1]
43+
.split(`:`)[1]
44+
.trim();
45+
assert.match(
46+
output,
47+
new RegExp(`Dataset display name: ${testDataSetName}`)
48+
);
49+
50+
// Delete dataset
51+
output = execSync(`${cmdDataset} delete-dataset -i "${dataSetId}"`);
52+
assert.match(output, /Dataset deleted./);
53+
});
54+
55+
// We make two models running this test, see hard-coded workaround below
56+
it(`should create a dataset, import data, and start making a model`, async () => {
57+
// Check to see that this dataset does not yet exist
58+
let output = execSync(`${cmdDataset} list-datasets`);
59+
assert.notMatch(output, new RegExp(dummyDataSet));
60+
61+
// Create dataset
62+
output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);
63+
const dataSetId = output
64+
.split(`\n`)[1]
65+
.split(`:`)[1]
66+
.trim();
67+
assert.match(output, new RegExp(`Dataset display name: ${dummyDataSet}`));
68+
69+
// Import Data
70+
output = execSync(
71+
`${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv"`
72+
);
73+
assert.match(output, /Data imported./);
74+
75+
// Check to make sure model doesn't already exist
76+
output = execSync(`${cmdModel} list-models`);
77+
assert.notMatch(output, testModelName);
78+
79+
// Begin training dataset, getting operation ID for next operation
80+
output = execSync(
81+
`${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"`
82+
);
83+
const operationName = output
84+
.split(`\n`)[0]
85+
.split(`:`)[1]
86+
.trim();
87+
assert.match(output, `Training started...`);
88+
89+
// Poll operation status, here confirming that operation is not complete yet
90+
output = execSync(
91+
`${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"`
92+
);
93+
assert.match(output, /done: false/);
94+
});
95+
96+
it(`should run get model (from a prexisting model)`, async () => {
97+
// Confirm dataset exists
98+
let output = execSync(`${cmdDataset} list-datasets`);
99+
assert.match(output, /me_do_not_delete/);
100+
101+
// List model evaluations, confirm model exists
102+
output = execSync(
103+
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
104+
);
105+
assert.match(output, /translationEvaluationMetrics:/);
106+
107+
// Get model evaluation
108+
output = execSync(`${cmdModel} get-model -a "${donotdeleteModelId}"`);
109+
assert.match(output, /Model deployment state: DEPLOYED/);
110+
});
111+
112+
it(`should run Prediction from prexisting model`, async () => {
113+
// Confirm dataset exists
114+
let output = execSync(`${cmdDataset} list-datasets`);
115+
assert.match(output, /me_do_not_delete/);
116+
117+
// List model evaluations, confirm model exists
118+
output = execSync(
119+
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
120+
);
121+
assert.match(output, `translationEvaluationMetrics:`);
122+
123+
// Run prediction on 'testImage.jpg' in resources folder
124+
output = execSync(
125+
`${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -t "False"`
126+
);
127+
assert.match(
128+
output,
129+
/Translated Content: {2}/
130+
);
131+
});
132+
133+
// List datasets
134+
it(`should list datasets`, async () => {
135+
const output = execSync(`${cmdDataset} list-datasets`);
136+
assert.match(output, /List of datasets:/);
137+
});
138+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
`use strict`;
17+
18+
async function main(projectId = 'YOUR_PROJECT_ID') {
19+
// [START automl_translation_create_dataset]
20+
const automl = require(`@google-cloud/automl`);
21+
22+
const client = new automl.AutoMlClient();
23+
const computeRegion = 'us-central1';
24+
const datasetName = 'myDataset';
25+
const source = 'en';
26+
const target = 'ja';
27+
28+
// A resource that represents Google Cloud Platform location.
29+
const projectLocation = client.locationPath(projectId, computeRegion);
30+
31+
// Specify the source and target language.
32+
const datasetSpec = {
33+
sourceLanguageCode: source,
34+
targetLanguageCode: target,
35+
};
36+
37+
// Set dataset name and dataset specification.
38+
const datasetInfo = {
39+
displayName: datasetName,
40+
translationDatasetMetadata: datasetSpec,
41+
};
42+
43+
// Create a dataset with the dataset specification in the region.
44+
const [dataset] = await client.createDataset({
45+
parent: projectLocation,
46+
dataset: datasetInfo,
47+
});
48+
49+
// Display the dataset information
50+
console.log(`Dataset name: ${dataset.name}`);
51+
console.log(`Dataset id: ${dataset.name.split(`/`).pop(-1)}`);
52+
console.log(`Dataset display name: ${dataset.displayName}`);
53+
console.log(`Dataset example count: ${dataset.exampleCount}`);
54+
console.log(`Translation dataset specification:`);
55+
console.log(
56+
`\tSource language code: ${
57+
dataset.translationDatasetMetadata.sourceLanguageCode
58+
}`
59+
);
60+
console.log(
61+
`\tTarget language code: ${
62+
dataset.translationDatasetMetadata.targetLanguageCode
63+
}`
64+
);
65+
console.log(`Dataset create time:`);
66+
console.log(`\tseconds: ${dataset.createTime.seconds}`);
67+
console.log(`\tnanos: ${dataset.createTime.nanos}`);
68+
// [END automl_translation_create_dataset]
69+
}
70+
71+
main(...process.argv.slice(2)).catch(err => console.error(err));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
`use strict`;
17+
18+
async function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
computeRegion = 'YOUR_REGION',
21+
datasetId = 'YOUR_DATASET'
22+
) {
23+
// [START automl_translation_delete_dataset]
24+
const automl = require(`@google-cloud/automl`);
25+
const client = new automl.AutoMlClient();
26+
27+
/**
28+
* TODO(developer): Uncomment the following line before running the sample.
29+
*/
30+
// const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
31+
// const computeRegion = `region-name, e.g. "us-central1"`;
32+
// const datasetId = `Id of the dataset`;
33+
34+
// Get the full path of the dataset.
35+
const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId);
36+
37+
// Delete a dataset.
38+
const [operations] = await client.deleteDataset({name: datasetFullId});
39+
const operationResponses = await operations.promise();
40+
// The final result of the operation.
41+
if (operationResponses[2].done === true) console.log(`Dataset deleted.`);
42+
43+
// [END automl_translation_delete_dataset]
44+
}
45+
46+
main(...process.argv.slice(2)).catch(err => console.error(err));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
`use strict`;
17+
18+
async function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
computeRegion = 'YOUR_REGION',
21+
datasetId = 'YOUR_DATASET'
22+
) {
23+
// [START automl_translation_get_dataset]
24+
const automl = require(`@google-cloud/automl`);
25+
const client = new automl.AutoMlClient();
26+
27+
/**
28+
* TODO(developer): Uncomment the following line before running the sample.
29+
*/
30+
// const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
31+
// const computeRegion = `region-name, e.g. "us-central1"`;
32+
// const datasetId = `Id of the dataset`;
33+
34+
// Get the full path of the dataset.
35+
const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId);
36+
37+
// Get complete detail of the dataset.
38+
const [dataset] = await client.getDataset({name: datasetFullId});
39+
40+
// Display the dataset information.
41+
console.log(`Dataset name: ${dataset.name}`);
42+
console.log(`Dataset id: ${dataset.name.split(`/`).pop(-1)}`);
43+
console.log(`Dataset display name: ${dataset.displayName}`);
44+
console.log(`Dataset example count: ${dataset.exampleCount}`);
45+
console.log(`Translation dataset specification:`);
46+
console.log(
47+
`\tSource language code: ${
48+
dataset.translationDatasetMetadata.sourceLanguageCode
49+
}`
50+
);
51+
console.log(
52+
`\tTarget language code: ${
53+
dataset.translationDatasetMetadata.targetLanguageCode
54+
}`
55+
);
56+
console.log(`Dataset create time:`);
57+
console.log(`\tseconds: ${dataset.createTime.seconds}`);
58+
console.log(`\tnanos: ${dataset.createTime.nanos}`);
59+
60+
// [END automl_translation_get_dataset]
61+
}
62+
63+
main(...process.argv.slice(2)).catch(err => console.error(err));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
`use strict`;
17+
18+
async function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
computeRegion = 'YOUR_REGION',
21+
datasetId = 'YOUR_DATASET',
22+
path = 'YOUR_PATH'
23+
) {
24+
// [START automl_translation_import_data]
25+
const automl = require(`@google-cloud/automl`);
26+
27+
const client = new automl.AutoMlClient();
28+
29+
/**
30+
* TODO(developer): Uncomment the following line before running the sample.
31+
*/
32+
// const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
33+
// const computeRegion = `region-name, e.g. "us-central1"`;
34+
// const datasetId = `Id of the dataset`;
35+
// const path = `string or array of .csv paths in AutoML Vision CSV format, e.g. “gs://myproject/mytraindata.csv”;`
36+
37+
// Get the full path of the dataset.
38+
const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId);
39+
40+
// Get the multiple Google Cloud Storage URIs.
41+
const inputUris = path.split(`,`);
42+
const inputConfig = {
43+
gcsSource: {
44+
inputUris: inputUris,
45+
},
46+
};
47+
48+
// Import data from the input URI.
49+
const [operation] = await client.importData({
50+
name: datasetFullId,
51+
inputConfig: inputConfig,
52+
});
53+
console.log(`Processing import...`);
54+
const operationResponses = await operation.promise();
55+
// The final result of the operation.
56+
if (operationResponses[2].done === true) {
57+
console.log(`Data imported.`);
58+
}
59+
60+
// [END automl_translation_import_data]
61+
}
62+
63+
main(...process.argv.slice(2)).catch(err => console.error(err));

0 commit comments

Comments
 (0)