Skip to content

Commit 76f26cf

Browse files
nnegreybusunkim96
authored andcommitted
automl: add base model samples for automl ga [(#2609)](GoogleCloudPlatform/python-docs-samples#2609)
* automl: add base model samples for automl ga * Add tests for each file, provide a unique model for each python version for kokoro * Fix version check * Update tests and format * Update deploy_model_test.py * Update license headers, ensure double quotes is used everywhere, leave black formatting
1 parent 2891d55 commit 76f26cf

15 files changed

+597
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019 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+
# http://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+
16+
def delete_model(project_id, model_id):
17+
"""Delete a model."""
18+
# [START automl_delete_model]
19+
from google.cloud import automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# model_id = "YOUR_MODEL_ID"
24+
25+
client = automl.AutoMlClient()
26+
# Get the full path of the model.
27+
model_full_id = client.model_path(project_id, "us-central1", model_id)
28+
response = client.delete_model(model_full_id)
29+
30+
print("Model deleted. {}".format(response.result()))
31+
# [END automl_delete_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019 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+
# http://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+
import os
16+
17+
import delete_model
18+
19+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
20+
21+
22+
def test_delete_model(capsys):
23+
# As model creation can take many hours, instead try to delete a
24+
# nonexistent model and confirm that the model was not found, but other
25+
# elements of the request were valid.
26+
try:
27+
delete_model.delete_model(PROJECT_ID, "TRL0000000000000000000")
28+
out, _ = capsys.readouterr()
29+
assert "The model does not exist" in out
30+
except Exception as e:
31+
assert "The model does not exist" in e.message
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019 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+
# http://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+
16+
def deploy_model(project_id, model_id):
17+
"""Deploy a model."""
18+
# [START automl_deploy_model]
19+
from google.cloud import automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# model_id = "YOUR_MODEL_ID"
24+
25+
client = automl.AutoMlClient()
26+
# Get the full path of the model.
27+
model_full_id = client.model_path(project_id, "us-central1", model_id)
28+
response = client.deploy_model(model_full_id)
29+
30+
print("Model deployment finished. {}".format(response.result()))
31+
# [END automl_deploy_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2019 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+
# http://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+
import os
16+
17+
import pytest
18+
19+
import deploy_model
20+
21+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
22+
MODEL_ID = "TRL0000000000000000000"
23+
24+
25+
@pytest.mark.slow
26+
def test_deploy_model(capsys):
27+
# As model deployment can take a long time, instead try to deploy a
28+
# nonexistent model and confirm that the model was not found, but other
29+
# elements of the request were valid.
30+
try:
31+
deploy_model.deploy_model(PROJECT_ID, MODEL_ID)
32+
out, _ = capsys.readouterr()
33+
assert "The model does not exist" in out
34+
except Exception as e:
35+
assert "The model does not exist" in e.message
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2019 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+
# http://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+
16+
def get_model(project_id, model_id):
17+
"""Get a model."""
18+
# [START automl_get_model]
19+
from google.cloud import automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# model_id = "YOUR_MODEL_ID"
24+
25+
client = automl.AutoMlClient()
26+
# Get the full path of the model.
27+
model_full_id = client.model_path(project_id, "us-central1", model_id)
28+
model = client.get_model(model_full_id)
29+
30+
# Retrieve deployment state.
31+
if model.deployment_state == automl.enums.Model.DeploymentState.DEPLOYED:
32+
deployment_state = "deployed"
33+
else:
34+
deployment_state = "undeployed"
35+
36+
# Display the model information.
37+
print("Model name: {}".format(model.name))
38+
print("Model id: {}".format(model.name.split("/")[-1]))
39+
print("Model display name: {}".format(model.display_name))
40+
print("Model create time:")
41+
print("\tseconds: {}".format(model.create_time.seconds))
42+
print("\tnanos: {}".format(model.create_time.nanos))
43+
print("Model deployment state: {}".format(deployment_state))
44+
# [END automl_get_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright 2019 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+
# http://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+
16+
def get_model_evaluation(project_id, model_id, model_evaluation_id):
17+
"""Get model evaluation."""
18+
# [START automl_language_entity_extraction_get_model_evaluation]
19+
# [START automl_language_sentiment_analysis_get_model_evaluation]
20+
# [START automl_language_text_classification_get_model_evaluation]
21+
# [START automl_translate_get_model_evaluation]
22+
# [START automl_vision_classification_get_model_evaluation]
23+
# [START automl_vision_object_detection_get_model_evaluation]
24+
from google.cloud import automl
25+
26+
# TODO(developer): Uncomment and set the following variables
27+
# project_id = "YOUR_PROJECT_ID"
28+
# model_id = "YOUR_MODEL_ID"
29+
# model_evaluation_id = "YOUR_MODEL_EVALUATION_ID"
30+
31+
client = automl.AutoMlClient()
32+
# Get the full path of the model evaluation.
33+
model_evaluation_full_id = client.model_evaluation_path(
34+
project_id, "us-central1", model_id, model_evaluation_id
35+
)
36+
37+
# Get complete detail of the model evaluation.
38+
response = client.get_model_evaluation(model_evaluation_full_id)
39+
40+
print("Model evaluation name: {}".format(response.name))
41+
print("Model annotation spec id: {}".format(response.annotation_spec_id))
42+
print("Create Time:")
43+
print("\tseconds: {}".format(response.create_time.seconds))
44+
print("\tnanos: {}".format(response.create_time.nanos / 1e9))
45+
print(
46+
"Evaluation example count: {}".format(response.evaluated_example_count)
47+
)
48+
# [END automl_language_sentiment_analysis_get_model_evaluation]
49+
# [END automl_language_text_classification_get_model_evaluation]
50+
# [END automl_translate_get_model_evaluation]
51+
# [END automl_vision_classification_get_model_evaluation]
52+
# [END automl_vision_object_detection_get_model_evaluation]
53+
print(
54+
"Entity extraction model evaluation metrics: {}".format(
55+
response.text_extraction_evaluation_metrics
56+
)
57+
)
58+
# [END automl_language_entity_extraction_get_model_evaluation]
59+
60+
# [START automl_language_sentiment_analysis_get_model_evaluation]
61+
print(
62+
"Sentiment analysis model evaluation metrics: {}".format(
63+
response.text_sentiment_evaluation_metrics
64+
)
65+
)
66+
# [END automl_language_sentiment_analysis_get_model_evaluation]
67+
68+
# [START automl_language_text_classification_get_model_evaluation]
69+
# [START automl_vision_classification_get_model_evaluation]
70+
print(
71+
"Classification model evaluation metrics: {}".format(
72+
response.classification_evaluation_metrics
73+
)
74+
)
75+
# [END automl_language_text_classification_get_model_evaluation]
76+
# [END automl_vision_classification_get_model_evaluation]
77+
78+
# [START automl_translate_get_model_evaluation]
79+
print(
80+
"Translation model evaluation metrics: {}".format(
81+
response.translation_evaluation_metrics
82+
)
83+
)
84+
# [END automl_translate_get_model_evaluation]
85+
86+
# [START automl_vision_object_detection_get_model_evaluation]
87+
print(
88+
"Object detection model evaluation metrics: {}".format(
89+
response.image_object_detection_evaluation_metrics
90+
)
91+
)
92+
# [END automl_vision_object_detection_get_model_evaluation]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2019 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+
# http://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+
import os
16+
17+
import pytest
18+
19+
import get_model_evaluation
20+
21+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
22+
MODEL_ID = "TEN1499896588007374848"
23+
24+
25+
@pytest.fixture(scope="function")
26+
def get_evaluation_id():
27+
from google.cloud import automl
28+
29+
client = automl.AutoMlClient()
30+
model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID)
31+
evaluation = None
32+
for e in client.list_model_evaluations(model_full_id, ""):
33+
evaluation = e
34+
break
35+
model_evaluation_id = evaluation.name.split(
36+
"{}/modelEvaluations/".format(MODEL_ID)
37+
)[1].split("\n")[0]
38+
yield model_evaluation_id
39+
40+
41+
def test_get_model_evaluation(capsys, get_evaluation_id):
42+
get_model_evaluation.get_model_evaluation(
43+
PROJECT_ID, MODEL_ID, get_evaluation_id
44+
)
45+
out, _ = capsys.readouterr()
46+
assert "Model evaluation name: " in out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2019 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+
# http://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+
import os
16+
17+
import get_model
18+
19+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
20+
MODEL_ID = "TEN1499896588007374848"
21+
22+
23+
def test_get_model(capsys):
24+
get_model.get_model(PROJECT_ID, MODEL_ID)
25+
out, _ = capsys.readouterr()
26+
assert "Model id: " in out

0 commit comments

Comments
 (0)