Skip to content

Commit

Permalink
feat(samples): add additional samples to library (#25)
Browse files Browse the repository at this point in the history
* Add samples

* Fix linter errors

* Remove unnecessary samples and tests

* Fix minor linter error

* Remove unnecessary comments and other small updates

* Update project id

* Add samples

* Fix linter errors

* Remove unnecessary samples and tests

* Fix minor linter error

* Remove unnecessary comments and other small updates

* Update project id

* Remove terminal await calls

* Fix configuration issues

* Fix parsing error

* Update samples/package.json

* Update samples/predict-custom-trained-model.js

* Fix async file access for Node 10

* Ensmallify the example image

Co-authored-by: Eric Schmidt <erschmid@google.com>
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
3 people authored Feb 10, 2021
1 parent a35f497 commit 66ff554
Show file tree
Hide file tree
Showing 77 changed files with 5,304 additions and 16 deletions.
62 changes: 62 additions & 0 deletions ai-platform/snippets/cancel-batch-prediction-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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(batchPredictionJobId, project, location = 'us-central1') {
// [START aiplatform_cancel_batch_prediction_job]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function cancelBatchPredictionJob() {
// Configure the name resource
const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`;
const request = {
name,
};

// Cancel batch prediction job request
await jobServiceClient.cancelBatchPredictionJob(request);
console.log('Cancel batch prediction job response :');
}

cancelBatchPredictionJob();
// [END aiplatform_cancel_batch_prediction_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
62 changes: 62 additions & 0 deletions ai-platform/snippets/cancel-custom-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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(customJobId, project, location = 'us-central1') {
// [START aiplatform_cancel_custom_job]
/**
* TODO(developer): Uncomment these variables before running the sample.\
*/

// const customJobId = 'YOUR_CUSTOM_JOB_ID';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function cancelCustomJob() {
// Configure the name resource
const name = jobServiceClient.customJobPath(project, location, customJobId);
const request = {
name,
};

// Cancel custom job request
const [response] = await jobServiceClient.cancelCustomJob(request);

console.log('Cancel custom job response');
console.log(`${response}`);
}
cancelCustomJob();
// [END aiplatform_cancel_custom_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
87 changes: 87 additions & 0 deletions ai-platform/snippets/create-custom-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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(
customJobDisplayName,
containerImageUri,
project,
location = 'us-central1'
) {
// [START aiplatform_create_custom_job]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const customJobDisplayName = 'YOUR_CUSTOM_JOB_DISPLAY_NAME';
// const containerImageUri = 'YOUR_CONTAINER_IMAGE_URI';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createCustomJob() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;
const customJob = {
displayName: customJobDisplayName,
jobSpec: {
workerPoolSpecs: [
{
machineSpec: {
machineType: 'n1-standard-4',
acceleratorType: 'NVIDIA_TESLA_K80',
acceleratorCount: 1,
},
replicaCount: 1,
containerSpec: {
imageUri: containerImageUri,
command: [],
args: [],
},
},
],
},
};
const request = {parent, customJob};

// Create custom job request
const [response] = await jobServiceClient.createCustomJob(request);

console.log('Create custom job response');
console.log(`${JSON.stringify(response)}`);
}
createCustomJob();
// [END aiplatform_create_custom_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
79 changes: 79 additions & 0 deletions ai-platform/snippets/create-dataset-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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(datasetDisplayName, project, location = 'us-central1') {
// [START aiplatform_create_dataset_image]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME";
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Dataset Service Client library
const {DatasetServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const datasetServiceClient = new DatasetServiceClient(clientOptions);

async function createDatasetImage() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;
// Configure the dataset resource
const dataset = {
displayName: datasetDisplayName,
metadataSchemaUri:
'gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml',
};
const request = {
parent,
dataset,
};

// Create Dataset Request
const [response] = await datasetServiceClient.createDataset(request);
console.log(`Long running operation: ${response.name}`);

// Wait for operation to complete
await response.promise();
const result = response.result;

console.log('Create dataset image response');
console.log(`Name : ${result.name}`);
console.log(`Display name : ${result.displayName}`);
console.log(`Metadata schema uri : ${result.metadataSchemaUri}`);
console.log(`Metadata : ${JSON.stringify(result.metadata)}`);
console.log(`Labels : ${JSON.stringify(result.labels)}`);
}
createDatasetImage();
// [END aiplatform_create_dataset_image]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

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

0 comments on commit 66ff554

Please sign in to comment.