Skip to content

Commit

Permalink
fix: parse CSV correctly (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
leahecole authored Jun 11, 2019
1 parent 0173a31 commit a346786
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions automl/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
59 changes: 32 additions & 27 deletions automl/snippets/tables/predict.v1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]
}
Expand Down

0 comments on commit a346786

Please sign in to comment.