-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* automl: add base samples * automl: add base set of samples * Clean up tests * License year 2020, drop python2 print statement unicode * use centralized automl testing project * Fix GCS path typo * Use fake dataset for batch predict * lint: line length * fix fixture naming and use * Fix fixture changes * Catch resource exhausted error * use fake data for import test * update how to access an operation id Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
277 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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 operation_id(): | ||
client = automl.AutoMlClient() | ||
project_location = client.location_path(PROJECT_ID, "us-central1") | ||
generator = client.transport._operations_client.list_operations( | ||
project_location, filter_="" | ||
).pages | ||
page = next(generator) | ||
operation = page.next() | ||
yield operation.name | ||
|
||
|
||
def test_get_operation_status(capsys, operation_id): | ||
get_operation_status.get_operation_status(operation_id) | ||
out, _ = capsys.readouterr() | ||
assert "Operation details" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.