Skip to content

Add missing deploy / undeploy samples #2433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions vision/automl/automl_vision_object_detection_deploy_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse


def deploy_model(project_id, model_id):
"""Deploy a model."""
# [START automl_vision_object_detection_deploy_model]
from google.cloud import automl_v1beta1 as automl

# project_id = 'YOUR_PROJECT_ID'
# model_name = 'YOUR_MODEL_ID'

client = automl.AutoMlClient()

# The full path to your model
full_model_id = client.model_path(project_id, 'us-central1', model_id)

# Deploy the model
response = client.deploy_model(full_model_id)

print(u'Model deployment finished'.format(response.result()))
# [END automl_vision_object_detection_deploy_model]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
'--project-id',
help='YOUR_PROJECT_ID',
required=True)
parser.add_argument(
'--model-id',
help='YOUR_MODEL_ID',
required=True)

args = parser.parse_args()

deploy_model(args.project_id, args.model_id)
60 changes: 60 additions & 0 deletions vision/automl/automl_vision_object_detection_deploy_node_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse


def deploy_model_node_count(project_id, model_id):
"""Deploy a model."""
# [START automl_vision_object_detection_deploy_model_node_count]
from google.cloud import automl_v1beta1 as automl

# project_id = 'YOUR_PROJECT_ID'
# model_name = 'YOUR_MODEL_ID'

client = automl.AutoMlClient()

# The full path to your model
full_model_id = client.model_path(project_id, 'us-central1', model_id)

# Set how many nodes the model is deployed on
model_deployment_metadata = (
automl.types.ImageObjectDetectionModelDeploymentMetadata(node_count=2))

# Deploy the model
response = client.deploy_model(full_model_id, model_deployment_metadata)

print(u'Model deployment on 2 nodes finished'.format(response.result()))
# [END automl_vision_object_detection_deploy_model_node_count]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
'--project-id',
help='YOUR_PROJECT_ID',
required=True)
parser.add_argument(
'--model-id',
help='YOUR_MODEL_ID',
required=True)

args = parser.parse_args()

deploy_model_node_count(args.project_id, args.model_id)
54 changes: 54 additions & 0 deletions vision/automl/automl_vision_object_detection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import pytest

import automl_vision_object_detection_deploy_model
import automl_vision_object_detection_deploy_node_count
import automl_vision_object_detection_undeploy_model

PROJECT_ID = os.environ['GCLOUD_PROJECT']
MODEL_ID = 'IOD6143103405779845120'


@pytest.mark.slow
def test_object_detection_deploy_undeploy_model(capsys):
automl_vision_object_detection_deploy_model.deploy_model(
PROJECT_ID, MODEL_ID)

out, _ = capsys.readouterr()
assert 'Model deployment finished' in out

automl_vision_object_detection_undeploy_model.undeploy_model(
PROJECT_ID, MODEL_ID)

out, _ = capsys.readouterr()
assert 'Model undeploy finished' in out


@pytest.mark.slow
def test_object_detection_deploy_node_count_undeploy_model(capsys):
automl_vision_object_detection_deploy_node_count.deploy_model_node_count(
PROJECT_ID, MODEL_ID)

out, _ = capsys.readouterr()
assert 'Model deployment on 2 nodes finished' in out

automl_vision_object_detection_undeploy_model.undeploy_model(
PROJECT_ID, MODEL_ID)

out, _ = capsys.readouterr()
assert 'Model undeploy finished' in out
56 changes: 56 additions & 0 deletions vision/automl/automl_vision_object_detection_undeploy_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse


def undeploy_model(project_id, model_id):
"""Undeploy a model."""
# [START automl_vision_object_detection_undeploy_model]
from google.cloud import automl_v1beta1 as automl

# project_id = 'YOUR_PROJECT_ID'
# model_name = 'YOUR_MODEL_ID'

client = automl.AutoMlClient()

# The full path to your model
full_model_id = client.model_path(project_id, 'us-central1', model_id)

# Undeploy the model
response = client.undeploy_model(full_model_id)

print(u'Model undeploy finished'.format(response.result()))
# [END automl_vision_object_detection_undeploy_model]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
'--project-id',
help='YOUR_PROJECT_ID',
required=True)
parser.add_argument(
'--model-id',
help='YOUR_MODEL_ID',
required=True)

args = parser.parse_args()

undeploy_model(args.project_id, args.model_id)