From b606616b0a27202731c01ba6b40701536ba1732e Mon Sep 17 00:00:00 2001 From: "Leah E. Cole" <6719667+leahecole@users.noreply.github.com> Date: Tue, 11 Jun 2019 16:25:23 -0700 Subject: [PATCH] fix: parse CSV correctly (#198) --- automl/package.json | 1 + automl/tables/predict.v1beta1.js | 59 +++++++++++++++++--------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/automl/package.json b/automl/package.json index 23e1276f9f..e82a0dc644 100644 --- a/automl/package.json +++ b/automl/package.json @@ -20,6 +20,7 @@ "dependencies": { "@google-cloud/automl": "^1.2.0", "chai": "^4.2.0", + "csv": "^5.1.1", "execa": "^1.0.0", "mathjs": "^6.0.0", "yargs": "^13.2.1" diff --git a/automl/tables/predict.v1beta1.js b/automl/tables/predict.v1beta1.js index 53b2e0982e..ebfb70d12f 100644 --- a/automl/tables/predict.v1beta1.js +++ b/automl/tables/predict.v1beta1.js @@ -23,6 +23,7 @@ async function main( // [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(); @@ -42,36 +43,40 @@ async function main( const modelFullId = client.modelPath(projectId, computeRegion, modelId); // Read the csv file content for prediction. - const stream = fs.createReadStream(filePath).on(`data`, function(data) { - const values = []; - for (const val of data) { - values.push({string_value: val}); - } + const stream = fs + .createReadStream(filePath) + .pipe(csv.parse()) + .on(`data`, function(data) { + const values = []; - // Set the payload by giving the row values. - const payload = { - row: { - values: values, - }, - }; + for (const val of data) { + values.push({stringValue: val}); + } - // Params is additional domain-specific parameters. - // Currently there is no additional parameters supported. - client - .predict({name: modelFullId, payload: payload, params: {}}) - .then(responses => { - console.log(responses); - console.log(`Prediction results:`); + // Set the payload by giving the row values. + const payload = { + row: { + values: values, + }, + }; - for (const result of responses[0].payload) { - console.log(`Predicted class name: ${result.displayName}`); - console.log(`Predicted class score: ${result.classification.score}`); - } - }) - .catch(err => { - console.error(err); - }); - }); + // Params is additional domain-specific parameters. + // Currently there is no additional parameters supported. + client + .predict({name: modelFullId, payload: payload, params: {}}) + .then(responses => { + console.log(responses); + 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}`); + } + }) + .catch(err => { + console.error(err); + }); + }); stream.read(); // [END automl_tables_predict] }