Skip to content
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

Automl ga base samples #2613

Merged
merged 27 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6bccf93
automl: add base samples
nnegrey Dec 12, 2019
9356684
automl: add base set of samples
nnegrey Dec 13, 2019
a69e033
Merge branch 'master' into automl-ga-base-samples
nnegrey Dec 18, 2019
f527a46
Clean up tests
nnegrey Dec 20, 2019
c2232a1
License year 2020, drop python2 print statement unicode
nnegrey Jan 2, 2020
fe68bc4
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 2, 2020
6b5401c
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 2, 2020
4976068
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 7, 2020
7cfad55
use centralized automl testing project
nnegrey Jan 7, 2020
ce95040
Fix GCS path typo
nnegrey Jan 7, 2020
2cd710a
Use fake dataset for batch predict
nnegrey Jan 7, 2020
9594c94
lint: line length
nnegrey Jan 7, 2020
418a04e
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 9, 2020
b3bdcc5
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 9, 2020
88ebd78
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 9, 2020
58a8128
fix fixture naming and use
nnegrey Jan 9, 2020
4420851
Merge branch 'master' into automl-ga-base-samples
leahecole Jan 14, 2020
d330ba2
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 15, 2020
e5ffd29
Fix fixture changes
nnegrey Jan 21, 2020
12b5c4e
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 21, 2020
83d1a4b
Catch resource exhausted error
nnegrey Jan 21, 2020
9526879
Merge branch 'automl-ga-base-samples' of https://github.com/GoogleClo…
nnegrey Jan 21, 2020
5d40849
use fake data for import test
nnegrey Jan 22, 2020
f4f0734
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 22, 2020
17689b1
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 27, 2020
ccb5792
update how to access an operation id
nnegrey Jan 28, 2020
fe00a35
Merge branch 'master' into automl-ga-base-samples
nnegrey Jan 28, 2020
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
52 changes: 52 additions & 0 deletions automl/cloud-client/batch_predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2020 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.


def batch_predict(project_id, model_id, input_uri, output_uri):
"""Batch predict"""
# [START automl_batch_predict]
from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# model_id = "YOUR_MODEL_ID"
# input_uri = "gs://YOUR_BUCKET_ID/path/to/your/input/csv_or_jsonl"
# output_uri = "gs://YOUR_BUCKET_ID/path/to/save/results/"

prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = prediction_client.model_path(
project_id, "us-central1", model_id
)

gcs_source = automl.types.GcsSource(input_uris=[input_uri])

input_config = automl.types.BatchPredictInputConfig(gcs_source=gcs_source)
gcs_destination = automl.types.GcsDestination(output_uri_prefix=output_uri)
output_config = automl.types.BatchPredictOutputConfig(
gcs_destination=gcs_destination
)

response = prediction_client.batch_predict(
model_full_id, input_config, output_config
)

print("Waiting for operation to complete...")
print(
"Batch Prediction results saved to Cloud Storage bucket. {}".format(
response.result()
)
)
# [END automl_batch_predict]
47 changes: 47 additions & 0 deletions automl/cloud-client/batch_predict_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2020 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 ladnguage governing permissions and
# limitations under the License.

import datetime
import os

import batch_predict

PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
BUCKET_ID = "{}-lcm".format(PROJECT_ID)
MODEL_ID = "TEN0000000000000000000"
PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime(
"%Y%m%d%H%M%S"
)


def test_batch_predict(capsys):
# As batch prediction can take a long time. Try to batch predict on a model
# and confirm that the model was not found, but other elements of the
# request were valid.
try:
input_uri = "gs://{}/entity-extraction/input.jsonl".format(BUCKET_ID)
output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX)
batch_predict.batch_predict(
PROJECT_ID, MODEL_ID, input_uri, output_uri
)
out, _ = capsys.readouterr()
assert (
"The model is either not found or not supported for prediction yet"
in out
)
except Exception as e:
assert (
"The model is either not found or not supported for prediction yet"
in e.message
)
34 changes: 34 additions & 0 deletions automl/cloud-client/get_operation_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2020 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.


def get_operation_status(operation_full_id):
"""Get operation status."""
# [START automl_get_operation_status]
from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# operation_full_id = \
# "projects/[projectId]/locations/us-central1/operations/[operationId]"

client = automl.AutoMlClient()
# Get the latest state of a long-running operation.
response = client.transport._operations_client.get_operation(
operation_full_id
)

print("Name: {}".format(response.name))
print("Operation details:")
print(response)
# [END automl_get_operation_status]
42 changes: 42 additions & 0 deletions automl/cloud-client/get_operation_status_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2020 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

from google.cloud import automl
import pytest

import get_operation_status

PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]


@pytest.fixture(scope="function")
def get_operation_id():
nnegrey marked this conversation as resolved.
Show resolved Hide resolved
client = automl.AutoMlClient()
project_location = client.location_path(PROJECT_ID, "us-central1")
response = client.transport._operations_client.list_operations(
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
project_location, ""
)
operation_id = ""
for operation in response:
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
operation_id = operation.name
break
yield operation_id


def test_get_operation_status(capsys, get_operation_id):
get_operation_status.get_operation_status(get_operation_id)
out, _ = capsys.readouterr()
assert "Operation details" in out
37 changes: 37 additions & 0 deletions automl/cloud-client/list_operation_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2020 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.


def list_operation_status(project_id):
"""List operation status."""
# [START automl_list_operation_status]
from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"

client = automl.AutoMlClient()
# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, "us-central1")
# List all the operations names available in the region.
response = client.transport._operations_client.list_operations(
project_location, ""
)

print("List of operations:")
for operation in response:
print("Name: {}".format(operation.name))
print("Operation details:")
print(operation)
# [END automl_list_operation_status]
28 changes: 28 additions & 0 deletions automl/cloud-client/list_operation_status_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 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 list_operation_status

PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]


@pytest.mark.slow
def test_list_operation_status(capsys):
list_operation_status.list_operation_status(PROJECT_ID)
out, _ = capsys.readouterr()
assert "Operation details" in out