Skip to content

Commit 6d4cf36

Browse files
munkhuushmglAhrar Monsur
authored and
Ahrar Monsur
committed
samples: refactor autoMl table, enable IT test (#423)
1 parent 57a6d8d commit 6d4cf36

5 files changed

+179
-161
lines changed

automl/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
"mocha": "^8.0.0",
2828
"uuid": "^8.0.0"
2929
}
30-
}
30+
}

automl/tables/predict-gcs-source-bq-dest.v1beta1.js

+37-31
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
// limitations under the License.
1414

1515
'use strict';
16+
1617
async function main(
17-
projectId = 'YOUR_PROJECT_ID',
18-
computeRegion = 'YOUR_REGION_NAME',
18+
projectId = 'YOUR_GCP_PROJECT_ID',
19+
computeRegion = 'REGION',
1920
modelId = 'MODEL_ID',
2021
inputUri = 'GCS_PATH',
2122
outputUri = 'BIGQUERY_DIRECTORY'
2223
) {
2324
// [START automl_tables_predict_using_gcs_source_and_bq_dest]
24-
const automl = require('@google-cloud/automl');
25-
26-
// Create client for prediction service.
27-
const client = new automl.v1beta1.PredictionServiceClient();
2825

2926
/**
3027
* Demonstrates using the AutoML client to request prediction from
@@ -39,38 +36,47 @@ async function main(
3936
// const outputUri = '[BIGQUERY_PATH]' e.g., "bq://<project_id>",
4037
// `The destination Big Query URI for storing outputs`;
4138

39+
const automl = require('@google-cloud/automl');
40+
41+
// Create client for prediction service.
42+
const automlClient = new automl.v1beta1.PredictionServiceClient();
43+
4244
// Get the full path of the model.
43-
const modelFullId = client.modelPath(projectId, computeRegion, modelId);
45+
const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId);
4446

45-
// Get the multiple Google Cloud Storage input URIs.
46-
const inputUris = inputUri.split(',');
47-
const inputConfig = {
48-
gcsSource: {
49-
inputUris: inputUris,
50-
},
51-
};
47+
async function batchPredict() {
48+
const inputConfig = {
49+
gcsSource: {
50+
inputUris: [inputUri],
51+
},
52+
};
5253

53-
// Get the Big Query output URIs.
54-
const outputConfig = {
55-
bigqueryDestination: {
56-
outputUri: outputUri,
57-
},
58-
};
54+
// Get the Big Query output URIs.
55+
const outputConfig = {
56+
bigqueryDestination: {
57+
outputUri: outputUri,
58+
},
59+
};
5960

60-
// Get the latest state of long-running operation.
61-
client
62-
.batchPredict({
61+
const [, operation] = await automlClient.batchPredict({
6362
name: modelFullId,
6463
inputConfig: inputConfig,
6564
outputConfig: outputConfig,
66-
})
67-
.then(responses => {
68-
const operation = responses[1];
69-
console.log(`Operation name: ${operation.name}`);
70-
})
71-
.catch(err => {
72-
console.error(err);
7365
});
66+
67+
// Get the latest state of long-running operation.
68+
console.log(`Operation name: ${operation.name}`);
69+
}
70+
71+
batchPredict();
7472
// [END automl_tables_predict_using_gcs_source_and_bq_dest]
7573
}
76-
main(...process.argv.slice(2)).catch(console.error());
74+
75+
main(...process.argv.slice(2)).catch(err => {
76+
console.error(err.message);
77+
process.exitCode = 1;
78+
});
79+
process.on('unhandledRejection', err => {
80+
console.error(err.message);
81+
process.exitCode = 1;
82+
});

automl/tables/predict-gcs-source-gcs-dest.v1beta1.js

+42-34
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
// limitations under the License.
1414

1515
'use strict';
16+
1617
async function main(
17-
projectId = 'YOUR_PROJECT_ID',
18-
computeRegion = 'YOUR_REGION_NAME',
19-
modelId = 'MODEL_ID',
20-
inputUri = 'GCS_PATH',
21-
outputUriPrefix = 'GCS_DIRECTORY'
18+
projectId = 'YOUR_GCP_PROJECT_ID',
19+
computeRegion = 'REGION',
20+
modelId = 'YOUR_MODEL_ID',
21+
inputUri = 'gs://your-bucket-uri/file.csv',
22+
outputUriPrefix = 'gs://your-bucket-uri/OUTPUT_PREFIX/'
2223
) {
2324
// [START automl_tables_predict_using_gcs_source_and_gcs_dest]
24-
const automl = require('@google-cloud/automl');
25-
26-
// Create client for prediction service.
27-
const client = new automl.v1beta1.PredictionServiceClient();
2825

2926
/**
3027
* Demonstrates using the AutoML client to request prediction from
@@ -40,38 +37,49 @@ async function main(
4037
// e.g., "gs://<bucket-name>/<folder-name>",
4138
// `The destination Google Cloud Storage URI for storing outputs`;
4239

40+
const automl = require('@google-cloud/automl');
41+
42+
// Create client for prediction service.
43+
const automlClient = new automl.v1beta1.PredictionServiceClient();
44+
4345
// Get the full path of the model.
44-
const modelFullId = client.modelPath(projectId, computeRegion, modelId);
46+
const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId);
4547

46-
// Get the multiple Google Cloud Storage input URIs.
47-
const inputUris = inputUri.split(',');
48-
const inputConfig = {
49-
gcsSource: {
50-
inputUris: inputUris,
51-
},
52-
};
48+
async function batchPredict() {
49+
// Construct request
50+
const inputConfig = {
51+
gcsSource: {
52+
inputUris: [inputUri],
53+
},
54+
};
5355

54-
// Get the Google Cloud Storage output URI.
55-
const outputConfig = {
56-
gcsDestination: {
57-
outputUriPrefix: outputUriPrefix,
58-
},
59-
};
56+
// Get the Google Cloud Storage output URI.
57+
const outputConfig = {
58+
gcsDestination: {
59+
outputUriPrefix: outputUriPrefix,
60+
},
61+
};
6062

61-
// Get the latest state of long-running operation.
62-
client
63-
.batchPredict({
63+
const [, operation] = await automlClient.batchPredict({
6464
name: modelFullId,
6565
inputConfig: inputConfig,
6666
outputConfig: outputConfig,
67-
})
68-
.then(responses => {
69-
const operation = responses[1];
70-
console.log(`Operation name: ${operation.name}`);
71-
})
72-
.catch(err => {
73-
console.error(err);
7467
});
68+
69+
// Get the latest state of long-running operation.
70+
console.log(`Operation name: ${operation.name}`);
71+
return operation;
72+
}
73+
74+
batchPredict();
7575
// [END automl_tables_predict_using_gcs_source_and_gcs_dest]
7676
}
77-
main(...process.argv.slice(2)).catch(console.error());
77+
78+
main(...process.argv.slice(2)).catch(err => {
79+
console.error(err.message);
80+
process.exitCode = 1;
81+
});
82+
process.on('unhandledRejection', err => {
83+
console.error(err.message);
84+
process.exitCode = 1;
85+
});

automl/tables/predict.v1beta1.js

+59-69
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Copyright 2019 Google LLC
2-
//
32
// Licensed under the Apache License, Version 2.0 (the "License");
43
// you may not use this file except in compliance with the License.
54
// You may obtain a copy of the License at
@@ -15,18 +14,12 @@
1514
'use strict';
1615

1716
async function main(
18-
projectId = 'YOUR_PROJECT_ID',
19-
computeRegion = 'YOUR_REGION_NAME',
20-
modelId = 'MODEL_ID',
21-
filePath = 'FILE_PATH'
17+
projectId = 'YOUR_GCP_PROJECT_ID',
18+
computeRegion = 'REGION',
19+
modelId = 'YOUR_MODEL_ID',
20+
inputs = [{numberValue: 1}, {stringValue: 'value'}]
2221
) {
2322
// [START automl_tables_predict]
24-
const automl = require('@google-cloud/automl');
25-
const fs = require('fs');
26-
const csv = require('csv');
27-
28-
// Create client for prediction service.
29-
const client = new automl.v1beta1.PredictionServiceClient();
3023

3124
/**
3225
* Demonstrates using the AutoML client to request prediction from
@@ -35,71 +28,68 @@ async function main(
3528
*/
3629
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
3730
// const computeRegion = '[REGION_NAME]' e.g., "us-central1";
38-
// const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800";
39-
// const filePath = '[FILE_PATH]'
40-
// e.g., "<resource>/<csv file>", `local csv file path`;
31+
// const modelId = '[MODEL_ID]' e.g., "TBL000000000000";
32+
// const inputs = [{ numberValue: 1 }, { stringValue: 'value' }, { stringValue: 'value2' } ...]
4133

42-
// Get the full path of the model.
43-
const modelFullId = client.modelPath(projectId, computeRegion, modelId);
34+
const automl = require('@google-cloud/automl');
4435

45-
// Read the csv file content for prediction.
46-
const stream = fs
47-
.createReadStream(filePath)
48-
.pipe(csv.parse())
49-
.on('data', data => {
50-
const values = [];
36+
// Create client for prediction service.
37+
const automlClient = new automl.v1beta1.PredictionServiceClient();
38+
39+
// Get the full path of the model.
40+
const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId);
5141

52-
for (const val of data) {
53-
values.push({stringValue: val});
54-
}
42+
inputs = JSON.parse(inputs);
5543

56-
// Set the payload by giving the row values.
57-
const payload = {
58-
row: {
59-
values: values,
60-
},
61-
};
44+
async function predict() {
45+
// Set the payload by giving the row values.
46+
const payload = {
47+
row: {
48+
values: inputs,
49+
},
50+
};
6251

63-
// Params is additional domain-specific parameters.
64-
// Currently there is no additional parameters supported.
65-
client
66-
.predict({
67-
name: modelFullId,
68-
payload: payload,
69-
params: {feature_importance: true},
70-
})
71-
.then(responses => {
72-
console.log(responses);
73-
console.log('Prediction results:');
52+
// Params is additional domain-specific parameters.
53+
// Currently there is no additional parameters supported.
54+
const [response] = await automlClient.predict({
55+
name: modelFullId,
56+
payload: payload,
57+
params: {feature_importance: true},
58+
});
59+
console.log('Prediction results:');
7460

75-
for (const result of responses[0].payload) {
76-
console.log(`Predicted class name: ${result.displayName}`);
77-
console.log(`Predicted class score: ${result.tables.score}`);
61+
for (const result of response.payload) {
62+
console.log(`Predicted class name: ${result.displayName}`);
63+
console.log(`Predicted class score: ${result.tables.score}`);
7864

79-
// Get features of top importance
80-
const featureList = result.tables.tablesModelColumnInfo.map(
81-
columnInfo => {
82-
return {
83-
importance: columnInfo.featureImportance,
84-
displayName: columnInfo.columnDisplayName,
85-
};
86-
}
87-
);
88-
// Sort features by their importance, highest importance first
89-
featureList.sort((a, b) => {
90-
return b.importance - a.importance;
91-
});
65+
// Get features of top importance
66+
const featureList = result.tables.tablesModelColumnInfo.map(
67+
columnInfo => {
68+
return {
69+
importance: columnInfo.featureImportance,
70+
displayName: columnInfo.columnDisplayName,
71+
};
72+
}
73+
);
74+
// Sort features by their importance, highest importance first
75+
featureList.sort((a, b) => {
76+
return b.importance - a.importance;
77+
});
9278

93-
// Print top 10 important features
94-
console.log('Features of top importance');
95-
console.log(featureList.slice(0, 10));
96-
}
97-
})
98-
.catch(err => {
99-
console.error(err);
100-
});
101-
});
102-
stream.read();
79+
// Print top 10 important features
80+
console.log('Features of top importance');
81+
console.log(featureList.slice(0, 10));
82+
}
83+
}
84+
predict();
10385
// [END automl_tables_predict]
10486
}
105-
main(...process.argv.slice(2)).catch(console.error());
87+
88+
main(...process.argv.slice(2)).catch(err => {
89+
console.error(err.message);
90+
process.exitCode = 1;
91+
});
92+
process.on('unhandledRejection', err => {
93+
console.error(err.message);
94+
process.exitCode = 1;
95+
});

0 commit comments

Comments
 (0)