Skip to content

Commit 84c5bcf

Browse files
leahecoleAhrar Monsur
authored andcommitted
fix: parse CSV correctly (#198)
1 parent a80a2c3 commit 84c5bcf

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

automl/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@google-cloud/automl": "^1.2.0",
2222
"chai": "^4.2.0",
23+
"csv": "^5.1.1",
2324
"execa": "^1.0.0",
2425
"mathjs": "^6.0.0",
2526
"yargs": "^13.2.1"

automl/tables/predict.v1beta1.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async function main(
2323
// [START automl_tables_predict]
2424
const automl = require(`@google-cloud/automl`);
2525
const fs = require(`fs`);
26+
const csv = require(`csv`);
2627

2728
// Create client for prediction service.
2829
const client = new automl.v1beta1.PredictionServiceClient();
@@ -42,36 +43,40 @@ async function main(
4243
const modelFullId = client.modelPath(projectId, computeRegion, modelId);
4344

4445
// Read the csv file content for prediction.
45-
const stream = fs.createReadStream(filePath).on(`data`, function(data) {
46-
const values = [];
47-
for (const val of data) {
48-
values.push({string_value: val});
49-
}
46+
const stream = fs
47+
.createReadStream(filePath)
48+
.pipe(csv.parse())
49+
.on(`data`, function(data) {
50+
const values = [];
5051

51-
// Set the payload by giving the row values.
52-
const payload = {
53-
row: {
54-
values: values,
55-
},
56-
};
52+
for (const val of data) {
53+
values.push({stringValue: val});
54+
}
5755

58-
// Params is additional domain-specific parameters.
59-
// Currently there is no additional parameters supported.
60-
client
61-
.predict({name: modelFullId, payload: payload, params: {}})
62-
.then(responses => {
63-
console.log(responses);
64-
console.log(`Prediction results:`);
56+
// Set the payload by giving the row values.
57+
const payload = {
58+
row: {
59+
values: values,
60+
},
61+
};
6562

66-
for (const result of responses[0].payload) {
67-
console.log(`Predicted class name: ${result.displayName}`);
68-
console.log(`Predicted class score: ${result.classification.score}`);
69-
}
70-
})
71-
.catch(err => {
72-
console.error(err);
73-
});
74-
});
63+
// Params is additional domain-specific parameters.
64+
// Currently there is no additional parameters supported.
65+
client
66+
.predict({name: modelFullId, payload: payload, params: {}})
67+
.then(responses => {
68+
console.log(responses);
69+
console.log(`Prediction results:`);
70+
71+
for (const result of responses[0].payload) {
72+
console.log(`Predicted class name: ${result.displayName}`);
73+
console.log(`Predicted class score: ${result.tables.score}`);
74+
}
75+
})
76+
.catch(err => {
77+
console.error(err);
78+
});
79+
});
7580
stream.read();
7681
// [END automl_tables_predict]
7782
}

0 commit comments

Comments
 (0)