Skip to content

Commit c1430ea

Browse files
nnegreybcoe
andauthored
docs: add the base samples for model tasks (#291)
* Add the base samples for model tasks * update package.json * lint fix * Update tests and license headers * use spawnsync to get stderr, update license headers Co-authored-by: Benjamin E. Coe <[email protected]>
1 parent 615b369 commit c1430ea

14 files changed

+786
-0
lines changed

automl/delete_model.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID'
21+
) {
22+
// [START automl_delete_model]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const modelId = 'YOUR_MODEL_ID';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function deleteModel() {
37+
// Construct request
38+
const request = {
39+
name: client.modelPath(projectId, location, modelId),
40+
};
41+
42+
const [response] = await client.deleteModel(request);
43+
console.log(`Model deleted: ${response}`);
44+
}
45+
46+
deleteModel();
47+
// [END automl_delete_model]
48+
}
49+
50+
main(...process.argv.slice(2));

automl/deploy_model.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID'
21+
) {
22+
// [START automl_deploy_model]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const modelId = 'YOUR_MODEL_ID';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function deployModel() {
37+
// Construct request
38+
const request = {
39+
name: client.modelPath(projectId, location, modelId),
40+
};
41+
42+
const [operation] = await client.deployModel(request);
43+
44+
// Wait for operation to complete.
45+
const [response] = await operation.promise();
46+
console.log(`Model deployment finished. ${response}`);
47+
}
48+
49+
deployModel();
50+
// [END automl_deploy_model]
51+
}
52+
53+
main(...process.argv.slice(2));

automl/get_model.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID'
21+
) {
22+
// [START automl_get_model]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const modelId = 'YOUR_MODEL_ID';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function getModel() {
37+
// Construct request
38+
const request = {
39+
name: client.modelPath(projectId, location, modelId),
40+
};
41+
42+
const [response] = await client.getModel(request);
43+
44+
console.log(`Model name: ${response.name}`);
45+
console.log(
46+
`Model id: ${
47+
response.name.split('/')[response.name.split('/').length - 1]
48+
}`
49+
);
50+
console.log(`Model display name: ${response.displayName}`);
51+
console.log(`Model create time`);
52+
console.log(`\tseconds ${response.createTime.seconds}`);
53+
console.log(`\tnanos ${response.createTime.nanos / 1e9}`);
54+
console.log(`Model deployment state: ${response.deploymentState}`);
55+
}
56+
57+
getModel();
58+
// [END automl_get_model]
59+
}
60+
61+
main(...process.argv.slice(2));

automl/get_model_evaluation.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID',
21+
modelEvaluationId = 'YOUR_MODEL_EVALUATION_ID'
22+
) {
23+
// [START automl_language_entity_extraction_get_model_evaluation]
24+
// [START automl_language_sentiment_analysis_get_model_evaluation]
25+
// [START automl_language_text_classification_get_model_evaluation]
26+
// [START automl_translate_get_model_evaluation]
27+
// [START automl_vision_classification_get_model_evaluation]
28+
// [START automl_vision_object_detection_get_model_evaluation]
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const projectId = 'YOUR_PROJECT_ID';
33+
// const location = 'us-central1';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const modelEvaluationId = 'YOUR_MODEL_EVALUATION_ID';
36+
37+
// Imports the Google Cloud AutoML library
38+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
39+
40+
// Instantiates a client
41+
const client = new AutoMlClient();
42+
43+
async function getModelEvaluation() {
44+
// Construct request
45+
const request = {
46+
name: client.modelEvaluationPath(
47+
projectId,
48+
location,
49+
modelId,
50+
modelEvaluationId
51+
),
52+
};
53+
54+
const [response] = await client.getModelEvaluation(request);
55+
56+
console.log(`Model evaluation name: ${response.name}`);
57+
console.log(`Model annotation spec id: ${response.annotationSpecId}`);
58+
console.log(`Model display name: ${response.displayName}`);
59+
console.log(`Model create time`);
60+
console.log(`\tseconds ${response.createTime.seconds}`);
61+
console.log(`\tnanos ${response.createTime.nanos / 1e9}`);
62+
console.log(`Evaluation example count: ${response.evaluatedExampleCount}`);
63+
// [END automl_language_sentiment_analysis_get_model_evaluation]
64+
// [END automl_language_text_classification_get_model_evaluation]
65+
// [END automl_translate_get_model_evaluation]
66+
// [END automl_vision_classification_get_model_evaluation]
67+
// [END automl_vision_object_detection_get_model_evaluation]
68+
console.log(
69+
`Entity extraction model evaluation metrics: ${response.textExtractionEvaluationMetrics}`
70+
);
71+
// [END automl_language_entity_extraction_get_model_evaluation]
72+
73+
// [START automl_language_sentiment_analysis_get_model_evaluation]
74+
console.log(
75+
`Sentiment analysis model evaluation metrics: ${response.textSentimentEvaluationMetrics}`
76+
);
77+
// [END automl_language_sentiment_analysis_get_model_evaluation]
78+
79+
// [START automl_language_text_classification_get_model_evaluation]
80+
// [START automl_vision_classification_get_model_evaluation]
81+
console.log(
82+
`Classification model evaluation metrics: ${response.classificationEvaluationMetrics}`
83+
);
84+
// [END automl_language_text_classification_get_model_evaluation]
85+
// [END automl_vision_classification_get_model_evaluation]
86+
87+
// [START automl_translate_get_model_evaluation]
88+
console.log(
89+
`Translation model evaluation metrics: ${response.translationEvaluationMetrics}`
90+
);
91+
// [END automl_translate_get_model_evaluation]
92+
93+
// [START automl_vision_object_detection_get_model_evaluation]
94+
console.log(
95+
`Object detection model evaluation metrics: ${response.imageObjectDetectionEvaluationMetrics}`
96+
);
97+
// [START automl_language_entity_extraction_get_model_evaluation]
98+
// [START automl_language_sentiment_analysis_get_model_evaluation]
99+
// [START automl_language_text_classification_get_model_evaluation]
100+
// [START automl_translate_get_model_evaluation]
101+
// [START automl_vision_classification_get_model_evaluation]
102+
}
103+
104+
getModelEvaluation();
105+
// [END automl_language_entity_extraction_get_model_evaluation]
106+
// [END automl_language_sentiment_analysis_get_model_evaluation]
107+
// [END automl_language_text_classification_get_model_evaluation]
108+
// [END automl_translate_get_model_evaluation]
109+
// [END automl_vision_classification_get_model_evaluation]
110+
// [END automl_vision_object_detection_get_model_evaluation]
111+
}
112+
113+
main(...process.argv.slice(2));

automl/list_model_evaluations.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID'
21+
) {
22+
// [START automl_language_entity_extraction_list_model_evaluations]
23+
// [START automl_language_sentiment_analysis_list_model_evaluations]
24+
// [START automl_language_text_classification_list_model_evaluations]
25+
// [START automl_translate_list_model_evaluations]
26+
// [START automl_vision_classification_list_model_evaluations]
27+
// [START automl_vision_object_detection_list_model_evaluations]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const location = 'us-central1';
33+
// const modelId = 'YOUR_MODEL_ID';
34+
35+
// Imports the Google Cloud AutoML library
36+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
37+
38+
// Instantiates a client
39+
const client = new AutoMlClient();
40+
41+
async function listModelEvaluations() {
42+
// Construct request
43+
const request = {
44+
parent: client.modelPath(projectId, location, modelId),
45+
filter: '',
46+
};
47+
48+
const [response] = await client.listModelEvaluations(request);
49+
50+
console.log(`List of model evaluations:`);
51+
for (const evaluation of response) {
52+
console.log(`Model evaluation name: ${evaluation.name}`);
53+
console.log(`Model annotation spec id: ${evaluation.annotationSpecId}`);
54+
console.log(`Model display name: ${evaluation.displayName}`);
55+
console.log(`Model create time`);
56+
console.log(`\tseconds ${evaluation.createTime.seconds}`);
57+
console.log(`\tnanos ${evaluation.createTime.nanos / 1e9}`);
58+
console.log(
59+
`Evaluation example count: ${evaluation.evaluatedExampleCount}`
60+
);
61+
// [END automl_language_sentiment_analysis_list_model_evaluations]
62+
// [END automl_language_text_classification_list_model_evaluations]
63+
// [END automl_translate_list_model_evaluations]
64+
// [END automl_vision_classification_list_model_evaluations]
65+
// [END automl_vision_object_detection_list_model_evaluations]
66+
console.log(
67+
`Entity extraction model evaluation metrics: ${evaluation.textExtractionEvaluationMetrics}`
68+
);
69+
// [END automl_language_entity_extraction_list_model_evaluations]
70+
71+
// [START automl_language_sentiment_analysis_list_model_evaluations]
72+
console.log(
73+
`Sentiment analysis model evaluation metrics: ${evaluation.textSentimentEvaluationMetrics}`
74+
);
75+
// [END automl_language_sentiment_analysis_list_model_evaluations]
76+
77+
// [START automl_language_text_classification_list_model_evaluations]
78+
// [START automl_vision_classification_list_model_evaluations]
79+
console.log(
80+
`Classification model evaluation metrics: ${evaluation.classificationEvaluationMetrics}`
81+
);
82+
// [END automl_language_text_classification_list_model_evaluations]
83+
// [END automl_vision_classification_list_model_evaluations]
84+
85+
// [START automl_translate_list_model_evaluations]
86+
console.log(
87+
`Translation model evaluation metrics: ${evaluation.translationEvaluationMetrics}`
88+
);
89+
// [END automl_translate_list_model_evaluations]
90+
91+
// [START automl_vision_object_detection_list_model_evaluations]
92+
console.log(
93+
`Object detection model evaluation metrics: ${evaluation.imageObjectDetectionEvaluationMetrics}`
94+
);
95+
// [START automl_language_entity_extraction_list_model_evaluations]
96+
// [START automl_language_sentiment_analysis_list_model_evaluations]
97+
// [START automl_language_text_classification_list_model_evaluations]
98+
// [START automl_translate_list_model_evaluations]
99+
// [START automl_vision_classification_list_model_evaluations]
100+
}
101+
}
102+
103+
listModelEvaluations();
104+
// [END automl_language_entity_extraction_list_model_evaluations]
105+
// [END automl_language_sentiment_analysis_list_model_evaluations]
106+
// [END automl_language_text_classification_list_model_evaluations]
107+
// [END automl_translate_list_model_evaluations]
108+
// [END automl_vision_classification_list_model_evaluations]
109+
// [END automl_vision_object_detection_list_model_evaluations]
110+
}
111+
112+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)