|
| 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 {after, before, describe, it} = require('mocha'); |
| 19 | +const {AutoMlClient} = require('@google-cloud/automl').v1; |
| 20 | +const {Storage} = require('@google-cloud/storage'); |
| 21 | + |
| 22 | +const cp = require('child_process'); |
| 23 | + |
| 24 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 25 | + |
| 26 | +const BATCH_PREDICT_REGION_TAG = 'batch_predict'; |
| 27 | +const LOCATION = 'us-central1'; |
| 28 | +const MODEL_ID = 'TEN2238627664384491520'; |
| 29 | +const PREFIX = 'TEST_BATCH_PREDICT'; |
| 30 | + |
| 31 | +describe('Automl Batch Predict Test', () => { |
| 32 | + const client = new AutoMlClient(); |
| 33 | + |
| 34 | + before('should verify the model is deployed', async () => { |
| 35 | + const projectId = await client.getProjectId(); |
| 36 | + const request = { |
| 37 | + name: client.modelPath(projectId, LOCATION, MODEL_ID), |
| 38 | + }; |
| 39 | + |
| 40 | + const [response] = await client.getModel(request); |
| 41 | + if (response.deploymentState === 'UNDEPLOYED') { |
| 42 | + const request = { |
| 43 | + name: client.modelPath(projectId, LOCATION, MODEL_ID), |
| 44 | + }; |
| 45 | + |
| 46 | + const [operation] = await client.deployModel(request); |
| 47 | + |
| 48 | + // Wait for operation to complete. |
| 49 | + await operation.promise(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('should batch predict', async () => { |
| 54 | + const projectId = await client.getProjectId(); |
| 55 | + const inputUri = `gs://${projectId}-lcm/entity_extraction/input.jsonl`; |
| 56 | + const outputUri = `gs://${projectId}-lcm/${PREFIX}/`; |
| 57 | + |
| 58 | + const batchPredictOutput = execSync( |
| 59 | + `node ${BATCH_PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${inputUri} ${outputUri}` |
| 60 | + ); |
| 61 | + assert.match( |
| 62 | + batchPredictOutput, |
| 63 | + /Batch Prediction results saved to Cloud Storage bucket/ |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + after('delete created files', async () => { |
| 68 | + const projectId = await client.getProjectId(); |
| 69 | + const storageClient = new Storage(); |
| 70 | + const options = { |
| 71 | + prefix: PREFIX, |
| 72 | + }; |
| 73 | + const [files] = await storageClient |
| 74 | + .bucket(`gs://${projectId}-lcm`) |
| 75 | + .getFiles(options); |
| 76 | + files.forEach(file => { |
| 77 | + storageClient |
| 78 | + .bucket(`gs://${projectId}-lcm`) |
| 79 | + .file(file.name) |
| 80 | + .delete(); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments