Skip to content

Commit d8bcad2

Browse files
nnegreybusunkim96
authored andcommitted
automl: add natural language text classification ga samples [(#2678)](#2678)
* automl: add natural language text classification ga samples * use centralized automl testing project, update doc comments, update test method names
1 parent faaa7d1 commit d8bcad2

12 files changed

+270
-6
lines changed

automl/snippets/language_sentiment_analysis_create_model_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@pytest.mark.slow
2727
def test_sentiment_analysis_create_model(capsys):
2828
language_sentiment_analysis_create_model.create_model(
29-
PROJECT_ID, DATASET_ID, "object_test_create_model"
29+
PROJECT_ID, DATASET_ID, "sentiment_test_create_model"
3030
)
3131
out, _ = capsys.readouterr()
3232
assert "Training started" in out

automl/snippets/language_sentiment_analysis_predict_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def verify_model_state():
3535
response.result()
3636

3737

38-
def test_predict(capsys, verify_model_state):
38+
def test_sentiment_analysis_predict(capsys, verify_model_state):
3939
verify_model_state
4040
text = "Hopefully this Claritin kicks in soon"
4141
language_sentiment_analysis_predict.predict(PROJECT_ID, MODEL_ID, text)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
# 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 create_dataset(project_id, display_name):
17+
"""Create a dataset."""
18+
# [START automl_language_text_classification_create_dataset]
19+
from google.cloud import automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# display_name = "YOUR_DATASET_NAME"
24+
25+
client = automl.AutoMlClient()
26+
27+
# A resource that represents Google Cloud Platform location.
28+
project_location = client.location_path(project_id, "us-central1")
29+
# Specify the classification type
30+
# Types:
31+
# MultiLabel: Multiple labels are allowed for one example.
32+
# MultiClass: At most one label is allowed per example.
33+
metadata = automl.types.TextClassificationDatasetMetadata(
34+
classification_type=automl.enums.ClassificationType.MULTICLASS
35+
)
36+
dataset = automl.types.Dataset(
37+
display_name=display_name,
38+
text_classification_dataset_metadata=metadata,
39+
)
40+
41+
# Create a dataset with the dataset metadata in the region.
42+
response = client.create_dataset(project_location, dataset)
43+
44+
created_dataset = response.result()
45+
46+
# Display the dataset information
47+
print("Dataset name: {}".format(created_dataset.name))
48+
print("Dataset id: {}".format(created_dataset.name.split("/")[-1]))
49+
# [END automl_language_text_classification_create_dataset]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
# 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 datetime
16+
import os
17+
18+
from google.cloud import automl
19+
20+
import language_text_classification_create_dataset
21+
22+
23+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
24+
25+
26+
def test_text_classification_create_dataset(capsys):
27+
dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
28+
language_text_classification_create_dataset.create_dataset(
29+
PROJECT_ID, dataset_name
30+
)
31+
out, _ = capsys.readouterr()
32+
assert "Dataset id: " in out
33+
34+
# Delete the created dataset
35+
dataset_id = out.splitlines()[1].split()[2]
36+
client = automl.AutoMlClient()
37+
dataset_full_id = client.dataset_path(
38+
PROJECT_ID, "us-central1", dataset_id
39+
)
40+
response = client.delete_dataset(dataset_full_id)
41+
response.result()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
# 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 create_model(project_id, dataset_id, display_name):
17+
"""Create a model."""
18+
# [START automl_language_text_classification_create_model]
19+
from google.cloud import automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# dataset_id = "YOUR_DATASET_ID"
24+
# display_name = "YOUR_MODEL_NAME"
25+
26+
client = automl.AutoMlClient()
27+
28+
# A resource that represents Google Cloud Platform location.
29+
project_location = client.location_path(project_id, "us-central1")
30+
# Leave model unset to use the default base model provided by Google
31+
metadata = automl.types.TextClassificationModelMetadata()
32+
model = automl.types.Model(
33+
display_name=display_name,
34+
dataset_id=dataset_id,
35+
text_classification_model_metadata=metadata,
36+
)
37+
38+
# Create a model with the model metadata in the region.
39+
response = client.create_model(project_location, model)
40+
41+
print(u"Training operation name: {}".format(response.operation.name))
42+
print("Training started...")
43+
# [END automl_language_text_classification_create_model]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# 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+
from google.cloud import automl
18+
import pytest
19+
20+
import language_text_classification_create_model
21+
22+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
23+
DATASET_ID = os.environ["TEXT_CLASSIFICATION_DATASET_ID"]
24+
25+
26+
@pytest.mark.slow
27+
def test_text_classification_create_model(capsys):
28+
language_text_classification_create_model.create_model(
29+
PROJECT_ID, DATASET_ID, "classification_test_create_model"
30+
)
31+
out, _ = capsys.readouterr()
32+
assert "Training started" in out
33+
34+
# Cancel the operation
35+
operation_id = out.split("Training operation name: ")[1].split("\n")[0]
36+
client = automl.AutoMlClient()
37+
client.transport._operations_client.cancel_operation(operation_id)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
# 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 predict(project_id, model_id, content):
17+
"""Predict."""
18+
# [START automl_language_text_classification_predict]
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+
# content = "text to predict"
25+
26+
prediction_client = automl.PredictionServiceClient()
27+
28+
# Get the full path of the model.
29+
model_full_id = prediction_client.model_path(
30+
project_id, "us-central1", model_id
31+
)
32+
33+
# Supported mime_types: 'text/plain', 'text/html'
34+
# https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet
35+
text_snippet = automl.types.TextSnippet(
36+
content=content, mime_type="text/plain"
37+
)
38+
payload = automl.types.ExamplePayload(text_snippet=text_snippet)
39+
40+
response = prediction_client.predict(model_full_id, payload)
41+
42+
for annotation_payload in response.payload:
43+
print(
44+
u"Predicted class name: {}".format(annotation_payload.display_name)
45+
)
46+
print(
47+
u"Predicted class score: {}".format(
48+
annotation_payload.classification.score
49+
)
50+
)
51+
# [END automl_language_text_classification_predict]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
# 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+
from google.cloud import automl
18+
import pytest
19+
20+
import language_text_classification_predict
21+
22+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
23+
MODEL_ID = os.environ["TEXT_CLASSIFICATION_MODEL_ID"]
24+
25+
26+
@pytest.fixture(scope="function")
27+
def verify_model_state():
28+
client = automl.AutoMlClient()
29+
model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID)
30+
31+
model = client.get_model(model_full_id)
32+
if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED:
33+
# Deploy model if it is not deployed
34+
response = client.deploy_model(model_full_id)
35+
response.result()
36+
37+
38+
def test_text_classification_predict(capsys, verify_model_state):
39+
verify_model_state
40+
text = "Fruit and nut flavour"
41+
language_text_classification_predict.predict(PROJECT_ID, MODEL_ID, text)
42+
out, _ = capsys.readouterr()
43+
assert "Predicted class name: " in out

automl/snippets/list_model_evaluations_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"]
2222

2323

24-
def test_list_get_eval_model(capsys):
24+
def test_list_model_evaluations(capsys):
2525
list_model_evaluations.list_model_evaluations(PROJECT_ID, MODEL_ID)
2626
out, _ = capsys.readouterr()
2727
assert "Model evaluation name: " in out

automl/snippets/list_models_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
2020

2121

22-
def test_list_get_eval_model(capsys):
22+
def test_list_models(capsys):
2323
list_models.list_models(PROJECT_ID)
2424
out, _ = capsys.readouterr()
2525
assert "Model id: " in out

automl/snippets/translate_predict_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def verify_model_state():
3535
response.result()
3636

3737

38-
def test_predict(capsys, verify_model_state):
38+
def test_translate_predict(capsys, verify_model_state):
3939
verify_model_state
4040
translate_predict.predict(PROJECT_ID, MODEL_ID, "resources/input.txt")
4141
out, _ = capsys.readouterr()

automl/snippets/vision_classification_create_dataset_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
@pytest.mark.slow
28-
def test_create_dataset(capsys):
28+
def test_vision_classification_create_dataset(capsys):
2929
# create dataset
3030
dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
3131
vision_classification_create_dataset.create_dataset(

0 commit comments

Comments
 (0)