-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Automl ga base samples #2613
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
Merged
Merged
Automl ga base samples #2613
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 9356684
automl: add base set of samples
nnegrey a69e033
Merge branch 'master' into automl-ga-base-samples
nnegrey f527a46
Clean up tests
nnegrey c2232a1
License year 2020, drop python2 print statement unicode
nnegrey fe68bc4
Merge branch 'master' into automl-ga-base-samples
nnegrey 6b5401c
Merge branch 'master' into automl-ga-base-samples
nnegrey 4976068
Merge branch 'master' into automl-ga-base-samples
nnegrey 7cfad55
use centralized automl testing project
nnegrey ce95040
Fix GCS path typo
nnegrey 2cd710a
Use fake dataset for batch predict
nnegrey 9594c94
lint: line length
nnegrey 418a04e
Merge branch 'master' into automl-ga-base-samples
nnegrey b3bdcc5
Merge branch 'master' into automl-ga-base-samples
nnegrey 88ebd78
Merge branch 'master' into automl-ga-base-samples
nnegrey 58a8128
fix fixture naming and use
nnegrey 4420851
Merge branch 'master' into automl-ga-base-samples
leahecole d330ba2
Merge branch 'master' into automl-ga-base-samples
nnegrey e5ffd29
Fix fixture changes
nnegrey 12b5c4e
Merge branch 'master' into automl-ga-base-samples
nnegrey 83d1a4b
Catch resource exhausted error
nnegrey 9526879
Merge branch 'automl-ga-base-samples' of https://github.com/GoogleClo…
nnegrey 5d40849
use fake data for import test
nnegrey f4f0734
Merge branch 'master' into automl-ga-base-samples
nnegrey 17689b1
Merge branch 'master' into automl-ga-base-samples
nnegrey ccb5792
update how to access an operation id
nnegrey fe00a35
Merge branch 'master' into automl-ga-base-samples
nnegrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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(): | ||
| 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.
Outdated
Show resolved
Hide resolved
|
||
| project_location, "" | ||
| ) | ||
| operation_id = "" | ||
| for operation in response: | ||
kurtisvg marked this conversation as resolved.
Outdated
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 | ||
This file contains hidden or 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.