|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const {assert} = require('chai'); |
| 18 | +const {before, describe, it} = require('mocha'); |
| 19 | +const {AutoMlClient} = require('@google-cloud/automl').v1; |
| 20 | + |
| 21 | +const cp = require('child_process'); |
| 22 | + |
| 23 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 24 | + |
| 25 | +const MODEL_ID = 'ICN5317963909599068160'; |
| 26 | +const PREDICT_REGION_TAG = 'vision_classification_predict'; |
| 27 | +const LOCATION = 'us-central1'; |
| 28 | + |
| 29 | +describe('Automl Vision Classification Predict Test', () => { |
| 30 | + const client = new AutoMlClient(); |
| 31 | + |
| 32 | + before('should verify the model is deployed', async () => { |
| 33 | + const projectId = await client.getProjectId(); |
| 34 | + const request = { |
| 35 | + name: client.modelPath(projectId, LOCATION, MODEL_ID), |
| 36 | + }; |
| 37 | + |
| 38 | + const [response] = await client.getModel(request); |
| 39 | + if (response.deploymentState === 'UNDEPLOYED') { |
| 40 | + const request = { |
| 41 | + name: client.modelPath(projectId, LOCATION, MODEL_ID), |
| 42 | + }; |
| 43 | + |
| 44 | + const [operation] = await client.deployModel(request); |
| 45 | + |
| 46 | + // Wait for operation to complete. |
| 47 | + await operation.promise(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('should predict', async () => { |
| 52 | + const projectId = await client.getProjectId(); |
| 53 | + const filePath = 'resources/test.png'; |
| 54 | + |
| 55 | + const predictOutput = execSync( |
| 56 | + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${filePath}` |
| 57 | + ); |
| 58 | + assert.match(predictOutput, /Predicted class name/); |
| 59 | + }); |
| 60 | +}); |
0 commit comments