Skip to content

Commit

Permalink
samples: refactor autoMl table, enable IT test (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored Jul 15, 2020
1 parent 624cf67 commit fd8321e
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 161 deletions.
2 changes: 1 addition & 1 deletion automl/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"mocha": "^8.0.0",
"uuid": "^8.0.0"
}
}
}
68 changes: 37 additions & 31 deletions automl/snippets/tables/predict-gcs-source-bq-dest.v1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,38 +36,47 @@ async function main(
// const outputUri = '[BIGQUERY_PATH]' e.g., "bq://<project_id>",
// `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;
});
76 changes: 42 additions & 34 deletions automl/snippets/tables/predict-gcs-source-gcs-dest.v1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,38 +37,49 @@ async function main(
// e.g., "gs://<bucket-name>/<folder-name>",
// `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;
});
128 changes: 59 additions & 69 deletions automl/snippets/tables/predict.v1beta1.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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., "<resource>/<csv file>", `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;
});
Loading

0 comments on commit fd8321e

Please sign in to comment.