Skip to content

Commit 374b8bb

Browse files
authored
automl: move samples to beta set (#3045)
1 parent 66e457f commit 374b8bb

7 files changed

+244
-0
lines changed

automl/beta/get_model_evaluation.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
def get_model_evaluation(project_id, model_id, model_evaluation_id):
1717
"""Get model evaluation."""
1818
# [START automl_video_classification_get_model_evaluation_beta]
19+
# [START automl_video_object_tracking_get_model_evaluation_beta]
1920
from google.cloud import automl_v1beta1 as automl
2021

2122
# TODO(developer): Uncomment and set the following variables
@@ -41,9 +42,19 @@ def get_model_evaluation(project_id, model_id, model_evaluation_id):
4142
"Evaluation example count: {}".format(response.evaluated_example_count)
4243
)
4344

45+
# [END automl_video_object_tracking_get_model_evaluation_beta]
46+
4447
print(
4548
"Classification model evaluation metrics: {}".format(
4649
response.classification_evaluation_metrics
4750
)
4851
)
4952
# [END automl_video_classification_get_model_evaluation_beta]
53+
54+
# [START automl_video_object_tracking_get_model_evaluation_beta]
55+
print(
56+
"Video object tracking model evaluation metrics: {}".format(
57+
response.video_object_tracking_evaluation_metrics
58+
)
59+
)
60+
# [END automl_video_object_tracking_get_model_evaluation_beta]

automl/beta/get_operation_status.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
# [START automl_get_operation_status_beta]
16+
from google.cloud import automl_v1beta1 as automl
17+
18+
19+
def get_operation_status(
20+
operation_full_id="projects/YOUR_PROJECT_ID/locations/us-central1/"
21+
"operations/YOUR_OPERATION_ID",
22+
):
23+
"""Get operation status."""
24+
client = automl.AutoMlClient()
25+
26+
# Get the latest state of a long-running operation.
27+
response = client.transport._operations_client.get_operation(
28+
operation_full_id
29+
)
30+
31+
print("Name: {}".format(response.name))
32+
print("Operation details:")
33+
print(response)
34+
# [END automl_get_operation_status_beta]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_v1beta1 as automl
18+
import pytest
19+
20+
import get_operation_status
21+
22+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
23+
24+
25+
@pytest.fixture(scope="function")
26+
def operation_id():
27+
client = automl.AutoMlClient()
28+
project_location = client.location_path(PROJECT_ID, "us-central1")
29+
generator = client.transport._operations_client.list_operations(
30+
project_location, filter_=""
31+
).pages
32+
page = next(generator)
33+
operation = page.next()
34+
yield operation.name
35+
36+
37+
def test_get_operation_status(capsys, operation_id):
38+
get_operation_status.get_operation_status(operation_id)
39+
out, _ = capsys.readouterr()
40+
assert "Operation details" in out

automl/beta/import_dataset.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
# [START automl_import_data_beta]
16+
from google.cloud import automl_v1beta1 as automl
17+
18+
19+
def import_dataset(
20+
project_id="YOUR_PROJECT_ID",
21+
dataset_id="YOUR_DATASET_ID",
22+
path="gs://YOUR_BUCKET_ID/path/to/data.csv",
23+
):
24+
"""Import a dataset."""
25+
client = automl.AutoMlClient()
26+
# Get the full path of the dataset.
27+
dataset_full_id = client.dataset_path(
28+
project_id, "us-central1", dataset_id
29+
)
30+
# Get the multiple Google Cloud Storage URIs
31+
input_uris = path.split(",")
32+
gcs_source = automl.types.GcsSource(input_uris=input_uris)
33+
input_config = automl.types.InputConfig(gcs_source=gcs_source)
34+
# Import data from the input URI
35+
response = client.import_data(dataset_full_id, input_config)
36+
37+
print("Processing import...")
38+
print("Data imported. {}".format(response.result()))
39+
# [END automl_import_data_beta]

automl/beta/import_dataset_test.py

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 os
16+
17+
import import_dataset
18+
19+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
20+
BUCKET_ID = "{}-lcm".format(PROJECT_ID)
21+
DATASET_ID = "TEN0000000000000000000"
22+
23+
24+
def test_import_dataset(capsys):
25+
# As importing a dataset can take a long time and only four operations can
26+
# be run on a dataset at once. Try to import into a nonexistent dataset and
27+
# confirm that the dataset was not found, but other elements of the request
28+
# were valid.
29+
try:
30+
data = "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID)
31+
import_dataset.import_dataset(PROJECT_ID, DATASET_ID, data)
32+
out, _ = capsys.readouterr()
33+
assert (
34+
"The Dataset doesn't exist or is inaccessible for use with AutoMl."
35+
in out
36+
)
37+
except Exception as e:
38+
assert (
39+
"The Dataset doesn't exist or is inaccessible for use with AutoMl."
40+
in e.message
41+
)

automl/beta/list_datasets.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
# [START automl_video_classification_list_datasets_beta]
16+
# [START automl_video_object_tracking_list_datasets_beta]
17+
from google.cloud import automl_v1beta1 as automl
18+
19+
20+
def list_datasets(project_id="YOUR_PROJECT_ID"):
21+
"""List datasets."""
22+
client = automl.AutoMlClient()
23+
# A resource that represents Google Cloud Platform location.
24+
project_location = client.location_path(project_id, "us-central1")
25+
26+
# List all the datasets available in the region.
27+
response = client.list_datasets(project_location, "")
28+
29+
print("List of datasets:")
30+
for dataset in response:
31+
print("Dataset name: {}".format(dataset.name))
32+
print("Dataset id: {}".format(dataset.name.split("/")[-1]))
33+
print("Dataset display name: {}".format(dataset.display_name))
34+
print("Dataset create time:")
35+
print("\tseconds: {}".format(dataset.create_time.seconds))
36+
print("\tnanos: {}".format(dataset.create_time.nanos))
37+
# [END automl_video_object_tracking_list_datasets_beta]
38+
39+
print(
40+
"Video classification dataset metadata: {}".format(
41+
dataset.video_classification_dataset_metadata
42+
)
43+
)
44+
# [END automl_video_classification_list_datasets_beta]
45+
46+
# [START automl_video_object_tracking_list_datasets_beta]
47+
print(
48+
"Video object tracking dataset metadata: {}".format(
49+
dataset.video_object_tracking_dataset_metadata
50+
)
51+
)
52+
# [END automl_video_object_tracking_list_datasets_beta]

automl/beta/list_datasets_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
import list_datasets
18+
19+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
20+
DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"]
21+
22+
23+
def test_list_dataset(capsys):
24+
# list datasets
25+
list_datasets.list_datasets(PROJECT_ID)
26+
out, _ = capsys.readouterr()
27+
assert "Dataset id: {}".format(DATASET_ID) in out

0 commit comments

Comments
 (0)