diff --git a/automl/package.json b/automl/package.json index 016c7c9bdc..02f8f1996e 100644 --- a/automl/package.json +++ b/automl/package.json @@ -27,4 +27,4 @@ "mocha": "^8.0.0", "uuid": "^8.0.0" } -} +} \ No newline at end of file diff --git a/automl/tables/predict-gcs-source-bq-dest.v1beta1.js b/automl/tables/predict-gcs-source-bq-dest.v1beta1.js index f824afe2d2..2520925482 100644 --- a/automl/tables/predict-gcs-source-bq-dest.v1beta1.js +++ b/automl/tables/predict-gcs-source-bq-dest.v1beta1.js @@ -13,18 +13,15 @@ // limitations under the License. 'use strict'; + async function main( - projectId = 'YOUR_PROJECT_ID', - computeRegion = 'YOUR_REGION_NAME', + projectId = 'YOUR_GCP_PROJECT_ID', + computeRegion = 'REGION', modelId = 'MODEL_ID', inputUri = 'GCS_PATH', outputUri = 'BIGQUERY_DIRECTORY' ) { // [START automl_tables_predict_using_gcs_source_and_bq_dest] - const automl = require('@google-cloud/automl'); - - // Create client for prediction service. - const client = new automl.v1beta1.PredictionServiceClient(); /** * Demonstrates using the AutoML client to request prediction from @@ -39,38 +36,47 @@ async function main( // const outputUri = '[BIGQUERY_PATH]' e.g., "bq://", // `The destination Big Query URI for storing outputs`; + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); - // Get the multiple Google Cloud Storage input URIs. - const inputUris = inputUri.split(','); - const inputConfig = { - gcsSource: { - inputUris: inputUris, - }, - }; + async function batchPredict() { + const inputConfig = { + gcsSource: { + inputUris: [inputUri], + }, + }; - // Get the Big Query output URIs. - const outputConfig = { - bigqueryDestination: { - outputUri: outputUri, - }, - }; + // Get the Big Query output URIs. + const outputConfig = { + bigqueryDestination: { + outputUri: outputUri, + }, + }; - // Get the latest state of long-running operation. - client - .batchPredict({ + const [, operation] = await automlClient.batchPredict({ name: modelFullId, inputConfig: inputConfig, outputConfig: outputConfig, - }) - .then(responses => { - const operation = responses[1]; - console.log(`Operation name: ${operation.name}`); - }) - .catch(err => { - console.error(err); }); + + // Get the latest state of long-running operation. + console.log(`Operation name: ${operation.name}`); + } + + batchPredict(); // [END automl_tables_predict_using_gcs_source_and_bq_dest] } -main(...process.argv.slice(2)).catch(console.error()); + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js b/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js index 9e3fd397de..6f0d885f3b 100644 --- a/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js +++ b/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js @@ -13,18 +13,15 @@ // limitations under the License. 'use strict'; + async function main( - projectId = 'YOUR_PROJECT_ID', - computeRegion = 'YOUR_REGION_NAME', - modelId = 'MODEL_ID', - inputUri = 'GCS_PATH', - outputUriPrefix = 'GCS_DIRECTORY' + projectId = 'YOUR_GCP_PROJECT_ID', + computeRegion = 'REGION', + modelId = 'YOUR_MODEL_ID', + inputUri = 'gs://your-bucket-uri/file.csv', + outputUriPrefix = 'gs://your-bucket-uri/OUTPUT_PREFIX/' ) { // [START automl_tables_predict_using_gcs_source_and_gcs_dest] - const automl = require('@google-cloud/automl'); - - // Create client for prediction service. - const client = new automl.v1beta1.PredictionServiceClient(); /** * Demonstrates using the AutoML client to request prediction from @@ -40,38 +37,49 @@ async function main( // e.g., "gs:///", // `The destination Google Cloud Storage URI for storing outputs`; + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); - // Get the multiple Google Cloud Storage input URIs. - const inputUris = inputUri.split(','); - const inputConfig = { - gcsSource: { - inputUris: inputUris, - }, - }; + async function batchPredict() { + // Construct request + const inputConfig = { + gcsSource: { + inputUris: [inputUri], + }, + }; - // Get the Google Cloud Storage output URI. - const outputConfig = { - gcsDestination: { - outputUriPrefix: outputUriPrefix, - }, - }; + // Get the Google Cloud Storage output URI. + const outputConfig = { + gcsDestination: { + outputUriPrefix: outputUriPrefix, + }, + }; - // Get the latest state of long-running operation. - client - .batchPredict({ + const [, operation] = await automlClient.batchPredict({ name: modelFullId, inputConfig: inputConfig, outputConfig: outputConfig, - }) - .then(responses => { - const operation = responses[1]; - console.log(`Operation name: ${operation.name}`); - }) - .catch(err => { - console.error(err); }); + + // Get the latest state of long-running operation. + console.log(`Operation name: ${operation.name}`); + return operation; + } + + batchPredict(); // [END automl_tables_predict_using_gcs_source_and_gcs_dest] } -main(...process.argv.slice(2)).catch(console.error()); + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/tables/predict.v1beta1.js b/automl/tables/predict.v1beta1.js index ccf70fd049..c90760aadd 100644 --- a/automl/tables/predict.v1beta1.js +++ b/automl/tables/predict.v1beta1.js @@ -1,5 +1,4 @@ // 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 @@ -15,18 +14,12 @@ 'use strict'; async function main( - projectId = 'YOUR_PROJECT_ID', - computeRegion = 'YOUR_REGION_NAME', - modelId = 'MODEL_ID', - filePath = 'FILE_PATH' + projectId = 'YOUR_GCP_PROJECT_ID', + computeRegion = 'REGION', + modelId = 'YOUR_MODEL_ID', + inputs = [{numberValue: 1}, {stringValue: 'value'}] ) { // [START automl_tables_predict] - const automl = require('@google-cloud/automl'); - const fs = require('fs'); - const csv = require('csv'); - - // Create client for prediction service. - const client = new automl.v1beta1.PredictionServiceClient(); /** * Demonstrates using the AutoML client to request prediction from @@ -35,71 +28,68 @@ async function main( */ // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; - // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; - // const filePath = '[FILE_PATH]' - // e.g., "/", `local csv file path`; + // const modelId = '[MODEL_ID]' e.g., "TBL000000000000"; + // const inputs = [{ numberValue: 1 }, { stringValue: 'value' }, { stringValue: 'value2' } ...] - // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); + const automl = require('@google-cloud/automl'); - // Read the csv file content for prediction. - const stream = fs - .createReadStream(filePath) - .pipe(csv.parse()) - .on('data', data => { - const values = []; + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); - for (const val of data) { - values.push({stringValue: val}); - } + inputs = JSON.parse(inputs); - // Set the payload by giving the row values. - const payload = { - row: { - values: values, - }, - }; + async function predict() { + // Set the payload by giving the row values. + const payload = { + row: { + values: inputs, + }, + }; - // Params is additional domain-specific parameters. - // Currently there is no additional parameters supported. - client - .predict({ - name: modelFullId, - payload: payload, - params: {feature_importance: true}, - }) - .then(responses => { - console.log(responses); - console.log('Prediction results:'); + // Params is additional domain-specific parameters. + // Currently there is no additional parameters supported. + const [response] = await automlClient.predict({ + name: modelFullId, + payload: payload, + params: {feature_importance: true}, + }); + console.log('Prediction results:'); - for (const result of responses[0].payload) { - console.log(`Predicted class name: ${result.displayName}`); - console.log(`Predicted class score: ${result.tables.score}`); + for (const result of response.payload) { + console.log(`Predicted class name: ${result.displayName}`); + console.log(`Predicted class score: ${result.tables.score}`); - // Get features of top importance - const featureList = result.tables.tablesModelColumnInfo.map( - columnInfo => { - return { - importance: columnInfo.featureImportance, - displayName: columnInfo.columnDisplayName, - }; - } - ); - // Sort features by their importance, highest importance first - featureList.sort((a, b) => { - return b.importance - a.importance; - }); + // Get features of top importance + const featureList = result.tables.tablesModelColumnInfo.map( + columnInfo => { + return { + importance: columnInfo.featureImportance, + displayName: columnInfo.columnDisplayName, + }; + } + ); + // Sort features by their importance, highest importance first + featureList.sort((a, b) => { + return b.importance - a.importance; + }); - // Print top 10 important features - console.log('Features of top importance'); - console.log(featureList.slice(0, 10)); - } - }) - .catch(err => { - console.error(err); - }); - }); - stream.read(); + // Print top 10 important features + console.log('Features of top importance'); + console.log(featureList.slice(0, 10)); + } + } + predict(); // [END automl_tables_predict] } -main(...process.argv.slice(2)).catch(console.error()); + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/test/automlTablesPredict.v1beta1.test.js b/automl/test/automlTablesPredict.v1beta1.test.js index 82efc133d0..2522787c8b 100644 --- a/automl/test/automlTablesPredict.v1beta1.test.js +++ b/automl/test/automlTablesPredict.v1beta1.test.js @@ -21,66 +21,80 @@ const {execSync} = require('child_process'); /** Tests for AutoML Tables "Prediction API" sample. */ -const cmdPredict = 'node automlTablesPrediction.js'; - -// // TODO(developer): Before running the test cases, set the environment -// variables PROJECT_ID, REGION_NAME and -// change modelId, gcsInputUri, gcsOutputUriPrefix, bqInputUri and -// bqOutputUriPrefix. -//const projectId = process.env.PROJECT_ID; -//const computeRegion = process.env.REGION_NAME; -const modelId = 'TBL3613734080685801472'; -const filePath = './resource/predictTest.csv'; -const gcsInputUri = 'gs://automl-tables/input/test.csv'; -const gcsOutputUriPrefix = 'gs://automl-tables/output'; +const projectId = process.env.AUTOML_PROJECT_ID; +const region = 'us-central1'; +const modelId = process.env.TABLE_MODEL_ID; +const gcsInputUri = `gs://${projectId}-tables/predictTest.csv`; +const gcsOutputUriPrefix = `gs://${projectId}-tables/test_outputs/`; const bqInputUri = 'bq://automl-tables-bg-input'; const bqOutputUriPrefix = 'bq://automl-tables-bg-output'; const exec = cmd => execSync(cmd, {encoding: 'utf8'}); describe('Tables PredictionAPI', () => { - it.skip('should perform single prediction', async () => { - // Run single prediction on predictTest.csv in resource folder - const output = exec(`${cmdPredict} predict "${modelId}" "${filePath}"`); - assert.match(output, /Prediction results:/); - assert.match(output, /Features of top importance:/); + it('should perform single prediction', async () => { + const inputs = [ + {numberValue: 39}, // Age + {stringValue: 'technician'}, // Job + {stringValue: 'married'}, // MaritalStatus + {stringValue: 'secondary'}, // Education + {stringValue: 'no'}, // Default + {numberValue: 52}, // Balance + {stringValue: 'no'}, // Housing + {stringValue: 'no'}, // Loan + {stringValue: 'cellular'}, // Contact + {numberValue: 12}, // Day + {stringValue: 'aug'}, // Month + {numberValue: 96}, // Duration + {numberValue: 2}, //Campaign + {numberValue: -1}, // PDays + {numberValue: 0}, // Previous + {stringValue: 'unknown'}, // POutcome + ]; + + const output = exec( + `node tables/predict.v1beta1.js "${projectId}" "${region}" "${modelId}" '${JSON.stringify( + inputs + )}'` + ); + + assert.include(output, 'Prediction results:'); }); - it.skip(`should perform batch prediction using GCS as source and + it(`should perform batch prediction using GCS as source and GCS as destination`, async () => { // Run batch prediction using GCS as source and GCS as destination const output = exec( - `${cmdPredict} predict-using-gcs-source-and-gcs-dest "${modelId}"` + - ` "${gcsInputUri}" "${gcsOutputUriPrefix}"` + `node tables/predict-gcs-source-gcs-dest.v1beta1.js "${projectId}" "${region}" "${modelId}" "${gcsInputUri}" "${gcsOutputUriPrefix}"` ); - assert.match(output, /Operation name:/); + assert.include(output, 'Operation name:'); }); it.skip(`should perform batch prediction using BQ as source and GCS as destination`, async () => { // Run batch prediction using BQ as source and GCS as destination const output = exec( - `${cmdPredict} predict-using-bq-source-and-gcs-dest "${modelId}"` + + `node tables/predict-gcs-source-bq-dest.v1beta1.js predict-using-bq-source-and-gcs-dest "${modelId}"` + ` "${bqInputUri}" "${gcsOutputUriPrefix}"` ); assert.match(output, /Operation name:/); }); - it.skip(`should perform batch prediction using GCS as source and + it(`should perform batch prediction using GCS as source and BQ as destination`, async () => { // Run batch prediction using GCS as source and BQ as destination const output = exec( - `${cmdPredict} predict-using-gcs-source-and-bq-dest "${modelId}"` + + `node tables/predict-gcs-source-bq-dest.v1beta1.js "${projectId}" "${region}" "${modelId}" ` + ` "${gcsInputUri}" "${bqOutputUriPrefix}"` ); - assert.match(output, /Operation name:/); + assert.include(output, 'Operation name:'); }); it.skip(`should perform batch prediction using BQ as source and BQ as destination`, async () => { // Run batch prediction using BQ as source and BQ as destination const output = exec( - `${cmdPredict} predict-using-bq-source-and-bq-dest "${modelId}"` + + `node tables/predict-gcs-source-bq-dest.v1beta1.js predict-using-bq-source-and-bq-dest "${modelId}"` + ` "${bqInputUri}" "${bqOutputUriPrefix}"` ); assert.match(output, /Operation name:/);