Skip to content

Commit

Permalink
docs: add the base samples for model tasks (#291)
Browse files Browse the repository at this point in the history
* 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 <bencoe@google.com>
  • Loading branch information
2 people authored and Ace Nassri committed Nov 15, 2022
1 parent 1278b36 commit f564bd7
Show file tree
Hide file tree
Showing 14 changed files with 786 additions and 0 deletions.
50 changes: 50 additions & 0 deletions automl/delete_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'YOUR_MODEL_ID'
) {
// [START automl_delete_model]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function deleteModel() {
// Construct request
const request = {
name: client.modelPath(projectId, location, modelId),
};

const [response] = await client.deleteModel(request);
console.log(`Model deleted: ${response}`);
}

deleteModel();
// [END automl_delete_model]
}

main(...process.argv.slice(2));
53 changes: 53 additions & 0 deletions automl/deploy_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'YOUR_MODEL_ID'
) {
// [START automl_deploy_model]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function deployModel() {
// Construct request
const request = {
name: client.modelPath(projectId, location, modelId),
};

const [operation] = await client.deployModel(request);

// Wait for operation to complete.
const [response] = await operation.promise();
console.log(`Model deployment finished. ${response}`);
}

deployModel();
// [END automl_deploy_model]
}

main(...process.argv.slice(2));
61 changes: 61 additions & 0 deletions automl/get_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'YOUR_MODEL_ID'
) {
// [START automl_get_model]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function getModel() {
// Construct request
const request = {
name: client.modelPath(projectId, location, modelId),
};

const [response] = await client.getModel(request);

console.log(`Model name: ${response.name}`);
console.log(
`Model id: ${
response.name.split('/')[response.name.split('/').length - 1]
}`
);
console.log(`Model display name: ${response.displayName}`);
console.log(`Model create time`);
console.log(`\tseconds ${response.createTime.seconds}`);
console.log(`\tnanos ${response.createTime.nanos / 1e9}`);
console.log(`Model deployment state: ${response.deploymentState}`);
}

getModel();
// [END automl_get_model]
}

main(...process.argv.slice(2));
113 changes: 113 additions & 0 deletions automl/get_model_evaluation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'YOUR_MODEL_ID',
modelEvaluationId = 'YOUR_MODEL_EVALUATION_ID'
) {
// [START automl_language_entity_extraction_get_model_evaluation]
// [START automl_language_sentiment_analysis_get_model_evaluation]
// [START automl_language_text_classification_get_model_evaluation]
// [START automl_translate_get_model_evaluation]
// [START automl_vision_classification_get_model_evaluation]
// [START automl_vision_object_detection_get_model_evaluation]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const modelEvaluationId = 'YOUR_MODEL_EVALUATION_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function getModelEvaluation() {
// Construct request
const request = {
name: client.modelEvaluationPath(
projectId,
location,
modelId,
modelEvaluationId
),
};

const [response] = await client.getModelEvaluation(request);

console.log(`Model evaluation name: ${response.name}`);
console.log(`Model annotation spec id: ${response.annotationSpecId}`);
console.log(`Model display name: ${response.displayName}`);
console.log(`Model create time`);
console.log(`\tseconds ${response.createTime.seconds}`);
console.log(`\tnanos ${response.createTime.nanos / 1e9}`);
console.log(`Evaluation example count: ${response.evaluatedExampleCount}`);
// [END automl_language_sentiment_analysis_get_model_evaluation]
// [END automl_language_text_classification_get_model_evaluation]
// [END automl_translate_get_model_evaluation]
// [END automl_vision_classification_get_model_evaluation]
// [END automl_vision_object_detection_get_model_evaluation]
console.log(
`Entity extraction model evaluation metrics: ${response.textExtractionEvaluationMetrics}`
);
// [END automl_language_entity_extraction_get_model_evaluation]

// [START automl_language_sentiment_analysis_get_model_evaluation]
console.log(
`Sentiment analysis model evaluation metrics: ${response.textSentimentEvaluationMetrics}`
);
// [END automl_language_sentiment_analysis_get_model_evaluation]

// [START automl_language_text_classification_get_model_evaluation]
// [START automl_vision_classification_get_model_evaluation]
console.log(
`Classification model evaluation metrics: ${response.classificationEvaluationMetrics}`
);
// [END automl_language_text_classification_get_model_evaluation]
// [END automl_vision_classification_get_model_evaluation]

// [START automl_translate_get_model_evaluation]
console.log(
`Translation model evaluation metrics: ${response.translationEvaluationMetrics}`
);
// [END automl_translate_get_model_evaluation]

// [START automl_vision_object_detection_get_model_evaluation]
console.log(
`Object detection model evaluation metrics: ${response.imageObjectDetectionEvaluationMetrics}`
);
// [START automl_language_entity_extraction_get_model_evaluation]
// [START automl_language_sentiment_analysis_get_model_evaluation]
// [START automl_language_text_classification_get_model_evaluation]
// [START automl_translate_get_model_evaluation]
// [START automl_vision_classification_get_model_evaluation]
}

getModelEvaluation();
// [END automl_language_entity_extraction_get_model_evaluation]
// [END automl_language_sentiment_analysis_get_model_evaluation]
// [END automl_language_text_classification_get_model_evaluation]
// [END automl_translate_get_model_evaluation]
// [END automl_vision_classification_get_model_evaluation]
// [END automl_vision_object_detection_get_model_evaluation]
}

main(...process.argv.slice(2));
112 changes: 112 additions & 0 deletions automl/list_model_evaluations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'YOUR_MODEL_ID'
) {
// [START automl_language_entity_extraction_list_model_evaluations]
// [START automl_language_sentiment_analysis_list_model_evaluations]
// [START automl_language_text_classification_list_model_evaluations]
// [START automl_translate_list_model_evaluations]
// [START automl_vision_classification_list_model_evaluations]
// [START automl_vision_object_detection_list_model_evaluations]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function listModelEvaluations() {
// Construct request
const request = {
parent: client.modelPath(projectId, location, modelId),
filter: '',
};

const [response] = await client.listModelEvaluations(request);

console.log(`List of model evaluations:`);
for (const evaluation of response) {
console.log(`Model evaluation name: ${evaluation.name}`);
console.log(`Model annotation spec id: ${evaluation.annotationSpecId}`);
console.log(`Model display name: ${evaluation.displayName}`);
console.log(`Model create time`);
console.log(`\tseconds ${evaluation.createTime.seconds}`);
console.log(`\tnanos ${evaluation.createTime.nanos / 1e9}`);
console.log(
`Evaluation example count: ${evaluation.evaluatedExampleCount}`
);
// [END automl_language_sentiment_analysis_list_model_evaluations]
// [END automl_language_text_classification_list_model_evaluations]
// [END automl_translate_list_model_evaluations]
// [END automl_vision_classification_list_model_evaluations]
// [END automl_vision_object_detection_list_model_evaluations]
console.log(
`Entity extraction model evaluation metrics: ${evaluation.textExtractionEvaluationMetrics}`
);
// [END automl_language_entity_extraction_list_model_evaluations]

// [START automl_language_sentiment_analysis_list_model_evaluations]
console.log(
`Sentiment analysis model evaluation metrics: ${evaluation.textSentimentEvaluationMetrics}`
);
// [END automl_language_sentiment_analysis_list_model_evaluations]

// [START automl_language_text_classification_list_model_evaluations]
// [START automl_vision_classification_list_model_evaluations]
console.log(
`Classification model evaluation metrics: ${evaluation.classificationEvaluationMetrics}`
);
// [END automl_language_text_classification_list_model_evaluations]
// [END automl_vision_classification_list_model_evaluations]

// [START automl_translate_list_model_evaluations]
console.log(
`Translation model evaluation metrics: ${evaluation.translationEvaluationMetrics}`
);
// [END automl_translate_list_model_evaluations]

// [START automl_vision_object_detection_list_model_evaluations]
console.log(
`Object detection model evaluation metrics: ${evaluation.imageObjectDetectionEvaluationMetrics}`
);
// [START automl_language_entity_extraction_list_model_evaluations]
// [START automl_language_sentiment_analysis_list_model_evaluations]
// [START automl_language_text_classification_list_model_evaluations]
// [START automl_translate_list_model_evaluations]
// [START automl_vision_classification_list_model_evaluations]
}
}

listModelEvaluations();
// [END automl_language_entity_extraction_list_model_evaluations]
// [END automl_language_sentiment_analysis_list_model_evaluations]
// [END automl_language_text_classification_list_model_evaluations]
// [END automl_translate_list_model_evaluations]
// [END automl_vision_classification_list_model_evaluations]
// [END automl_vision_object_detection_list_model_evaluations]
}

main(...process.argv.slice(2));
Loading

0 comments on commit f564bd7

Please sign in to comment.