1
1
// Copyright 2019 Google LLC
2
- //
3
2
// Licensed under the Apache License, Version 2.0 (the "License");
4
3
// you may not use this file except in compliance with the License.
5
4
// You may obtain a copy of the License at
15
14
'use strict' ;
16
15
17
16
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' } ]
22
21
) {
23
22
// [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 ( ) ;
30
23
31
24
/**
32
25
* Demonstrates using the AutoML client to request prediction from
@@ -35,71 +28,68 @@ async function main(
35
28
*/
36
29
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
37
30
// 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' } ...]
41
33
42
- // Get the full path of the model.
43
- const modelFullId = client . modelPath ( projectId , computeRegion , modelId ) ;
34
+ const automl = require ( '@google-cloud/automl' ) ;
44
35
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 ) ;
51
41
52
- for ( const val of data ) {
53
- values . push ( { stringValue : val } ) ;
54
- }
42
+ inputs = JSON . parse ( inputs ) ;
55
43
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
+ } ;
62
51
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:' ) ;
74
60
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 } ` ) ;
78
64
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
+ } ) ;
92
78
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 ( ) ;
103
85
// [END automl_tables_predict]
104
86
}
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