diff --git a/.github/auto-label.yaml b/.github/auto-label.yaml index cb1e172647c..bfd016259d8 100644 --- a/.github/auto-label.yaml +++ b/.github/auto-label.yaml @@ -20,7 +20,6 @@ path: appengine: "appengine" asset: "asset" auth: "auth" - automl: "automl" batch: "batch" bigquery: "bigquery" bigquery_storage: "bigquerystorage" @@ -37,7 +36,6 @@ path: contentwarehouse: "contentwarehouse" datacatalog: "datacatalog" dataplex: "dataplex" - datalabeling: "datalabeling" dataproc: "dataproc" datastore: "datastore" dialogflow: "dialogflow" @@ -52,7 +50,6 @@ path: healthcare: "healhcare" iam: "iam" iap: "iap" - iot: "cloudiot" jobs: "jobs" kms: "cloudkms" kubernetes_engine: "container" diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index 191b40b09e0..a67e635b746 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -128,9 +128,6 @@ gcloud auth activate-service-account \ export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json -# For Datalabeling samples to hit the testing endpoint -export DATALABELING_ENDPOINT="test-datalabeling.sandbox.googleapis.com:443" - # Run Cloud SQL proxy (background process exit when script does) wget --quiet https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 \ -O ${HOME}/cloud_sql_proxy && chmod +x ${HOME}/cloud_sql_proxy diff --git a/.kokoro/tests/run_tests_orig.sh b/.kokoro/tests/run_tests_orig.sh index dc954fd13bd..99bbf25de4d 100755 --- a/.kokoro/tests/run_tests_orig.sh +++ b/.kokoro/tests/run_tests_orig.sh @@ -88,9 +88,6 @@ fi export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json -# For Datalabeling samples to hit the testing endpoint -export DATALABELING_ENDPOINT="test-datalabeling.sandbox.googleapis.com:443" - # Run Cloud SQL proxy (background process exit when script does) wget --quiet https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 \ -O ${HOME}/cloud_sql_proxy && chmod +x ${HOME}/cloud_sql_proxy diff --git a/.kokoro/trampoline_v2.sh b/.kokoro/trampoline_v2.sh index d9031cfd6fa..1f7b4de9b2c 100755 --- a/.kokoro/trampoline_v2.sh +++ b/.kokoro/trampoline_v2.sh @@ -27,7 +27,6 @@ # (/dev/shm is passed into the container as KOKORO_GFILE_DIR). # # gsutil cp gs://cloud-devrel-kokoro-resources/python-docs-samples/secrets_viewer_service_account.json /dev/shm -# gsutil cp gs://cloud-devrel-kokoro-resources/python-docs-samples/automl_secrets.txt /dev/shm # # Then run the script. # .kokoro/trampoline_v2.sh diff --git a/automl/AUTHORING_GUIDE.md b/automl/AUTHORING_GUIDE.md deleted file mode 100644 index 8249522ffc2..00000000000 --- a/automl/AUTHORING_GUIDE.md +++ /dev/null @@ -1 +0,0 @@ -See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md \ No newline at end of file diff --git a/automl/CONTRIBUTING.md b/automl/CONTRIBUTING.md deleted file mode 100644 index f5fe2e6baf1..00000000000 --- a/automl/CONTRIBUTING.md +++ /dev/null @@ -1 +0,0 @@ -See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md \ No newline at end of file diff --git a/automl/snippets/batch_predict.py b/automl/snippets/batch_predict.py deleted file mode 100644 index 5b5f810b4ed..00000000000 --- a/automl/snippets/batch_predict.py +++ /dev/null @@ -1,46 +0,0 @@ -# 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 = f"projects/{project_id}/locations/us-central1/models/{model_id}" - - gcs_source = automl.GcsSource(input_uris=[input_uri]) - - input_config = automl.BatchPredictInputConfig(gcs_source=gcs_source) - gcs_destination = automl.GcsDestination(output_uri_prefix=output_uri) - output_config = automl.BatchPredictOutputConfig(gcs_destination=gcs_destination) - - response = prediction_client.batch_predict( - name=model_full_id, input_config=input_config, output_config=output_config - ) - - print("Waiting for operation to complete...") - print( - f"Batch Prediction results saved to Cloud Storage bucket. {response.result()}" - ) - # [END automl_batch_predict] diff --git a/automl/snippets/batch_predict_test.py b/automl/snippets/batch_predict_test.py deleted file mode 100644 index 7b7595098fd..00000000000 --- a/automl/snippets/batch_predict_test.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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 - -from google.api_core.retry import Retry - -import batch_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = f"{PROJECT_ID}-lcm" -MODEL_ID = "TEN0000000000000000000" -PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - - -@Retry() -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 = f"gs://{BUCKET_ID}/entity-extraction/input.jsonl" - output_uri = f"gs://{BUCKET_ID}/{PREFIX}/" - batch_predict.batch_predict(PROJECT_ID, MODEL_ID, input_uri, output_uri) - out, _ = capsys.readouterr() - assert "does not exist" in out - except Exception as e: - assert "does not exist" in e.message diff --git a/automl/snippets/delete_dataset.py b/automl/snippets/delete_dataset.py deleted file mode 100644 index 17d65d592fc..00000000000 --- a/automl/snippets/delete_dataset.py +++ /dev/null @@ -1,31 +0,0 @@ -# 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 delete_dataset(project_id, dataset_id): - """Delete a dataset.""" - # [START automl_delete_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - - client = automl.AutoMlClient() - # Get the full path of the dataset - dataset_full_id = client.dataset_path(project_id, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - - print(f"Dataset deleted. {response.result()}") - # [END automl_delete_dataset] diff --git a/automl/snippets/delete_dataset_test.py b/automl/snippets/delete_dataset_test.py deleted file mode 100644 index dbbaf1de6cf..00000000000 --- a/automl/snippets/delete_dataset_test.py +++ /dev/null @@ -1,48 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl -import pytest - -import delete_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = f"{PROJECT_ID}-lcm" - - -@pytest.fixture(scope="function") -def dataset_id(): - client = automl.AutoMlClient() - project_location = f"projects/{PROJECT_ID}/locations/us-central1" - display_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - metadata = automl.TextExtractionDatasetMetadata() - dataset = automl.Dataset( - display_name=display_name, text_extraction_dataset_metadata=metadata - ) - response = client.create_dataset(parent=project_location, dataset=dataset) - dataset_id = response.result().name.split("/")[-1] - - yield dataset_id - - -@Retry() -def test_delete_dataset(capsys, dataset_id): - # delete dataset - delete_dataset.delete_dataset(PROJECT_ID, dataset_id) - out, _ = capsys.readouterr() - assert "Dataset deleted." in out diff --git a/automl/snippets/delete_model.py b/automl/snippets/delete_model.py deleted file mode 100644 index fddf5e4c793..00000000000 --- a/automl/snippets/delete_model.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2019 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 delete_model(project_id, model_id): - """Delete a model.""" - # [START automl_delete_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - response = client.delete_model(name=model_full_id) - - print(f"Model deleted. {response.result()}") - # [END automl_delete_model] diff --git a/automl/snippets/delete_model_test.py b/automl/snippets/delete_model_test.py deleted file mode 100644 index 7911230a1f5..00000000000 --- a/automl/snippets/delete_model_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry - -import delete_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -def test_delete_model(capsys): - # As model creation can take many hours, instead try to delete a - # nonexistent model and confirm that the model was not found, but other - # elements of the request were valid. - try: - delete_model.delete_model(PROJECT_ID, "TRL0000000000000000000") - out, _ = capsys.readouterr() - assert "The model does not exist" in out - except Exception as e: - assert "The model does not exist" in e.message diff --git a/automl/snippets/deploy_model.py b/automl/snippets/deploy_model.py deleted file mode 100644 index 12518fefb89..00000000000 --- a/automl/snippets/deploy_model.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2019 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 deploy_model(project_id, model_id): - """Deploy a model.""" - # [START automl_deploy_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - response = client.deploy_model(name=model_full_id) - - print(f"Model deployment finished. {response.result()}") - # [END automl_deploy_model] diff --git a/automl/snippets/deploy_model_test.py b/automl/snippets/deploy_model_test.py deleted file mode 100644 index 3c3139795fc..00000000000 --- a/automl/snippets/deploy_model_test.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry -import pytest - -import deploy_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "TRL0000000000000000000" - - -@Retry() -@pytest.mark.slow -def test_deploy_model(capsys): - # As model deployment can take a long time, instead try to deploy a - # nonexistent model and confirm that the model was not found, but other - # elements of the request were valid. - try: - deploy_model.deploy_model(PROJECT_ID, MODEL_ID) - out, _ = capsys.readouterr() - assert "The model does not exist" in out - except Exception as e: - assert "The model does not exist" in e.message diff --git a/automl/snippets/export_dataset.py b/automl/snippets/export_dataset.py deleted file mode 100644 index bd68ca3116a..00000000000 --- a/automl/snippets/export_dataset.py +++ /dev/null @@ -1,36 +0,0 @@ -# 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 export_dataset(project_id, dataset_id, gcs_uri): - """Export a dataset.""" - # [START automl_export_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # gcs_uri = "gs://YOUR_BUCKET_ID/path/to/export/" - - client = automl.AutoMlClient() - - # Get the full path of the dataset - dataset_full_id = client.dataset_path(project_id, "us-central1", dataset_id) - - gcs_destination = automl.GcsDestination(output_uri_prefix=gcs_uri) - output_config = automl.OutputConfig(gcs_destination=gcs_destination) - - response = client.export_data(name=dataset_full_id, output_config=output_config) - print(f"Dataset exported. {response.result()}") - # [END automl_export_dataset] diff --git a/automl/snippets/export_dataset_test.py b/automl/snippets/export_dataset_test.py deleted file mode 100644 index f90eb5859c0..00000000000 --- a/automl/snippets/export_dataset_test.py +++ /dev/null @@ -1,46 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry - -import export_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = f"{PROJECT_ID}-lcm" -PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -DATASET_ID = "TEN0000000000000000000" - - -@Retry() -def test_export_dataset(capsys): - # As exporting a dataset can take a long time and only one operation can be - # run on a dataset at once. Try to export a nonexistent dataset and confirm - # that the dataset was not found, but other elements of the request were\ - # valid. - try: - export_dataset.export_dataset( - PROJECT_ID, DATASET_ID, f"gs://{BUCKET_ID}/{PREFIX}/" - ) - out, _ = capsys.readouterr() - assert ( - "The Dataset doesn't exist or is inaccessible for use with AutoMl." in out - ) - except Exception as e: - assert ( - "The Dataset doesn't exist or is inaccessible for use with AutoMl." - in e.message - ) diff --git a/automl/snippets/get_dataset.py b/automl/snippets/get_dataset.py deleted file mode 100644 index d185fc57832..00000000000 --- a/automl/snippets/get_dataset.py +++ /dev/null @@ -1,96 +0,0 @@ -# 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_dataset(project_id, dataset_id): - """Get a dataset.""" - # [START automl_language_entity_extraction_get_dataset] - # [START automl_language_sentiment_analysis_get_dataset] - # [START automl_language_text_classification_get_dataset] - # [START automl_translate_get_dataset] - # [START automl_vision_classification_get_dataset] - # [START automl_vision_object_detection_get_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - - client = automl.AutoMlClient() - # Get the full path of the dataset - dataset_full_id = client.dataset_path(project_id, "us-central1", dataset_id) - dataset = client.get_dataset(name=dataset_full_id) - - # Display the dataset information - print(f"Dataset name: {dataset.name}") - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print(f"Dataset display name: {dataset.display_name}") - print(f"Dataset create time: {dataset.create_time}") - # [END automl_language_sentiment_analysis_get_dataset] - # [END automl_language_text_classification_get_dataset] - # [END automl_translate_get_dataset] - # [END automl_vision_classification_get_dataset] - # [END automl_vision_object_detection_get_dataset] - print( - "Text extraction dataset metadata: {}".format( - dataset.text_extraction_dataset_metadata - ) - ) - # [END automl_language_entity_extraction_get_dataset] - - # [START automl_language_sentiment_analysis_get_dataset] - print( - "Text sentiment dataset metadata: {}".format( - dataset.text_sentiment_dataset_metadata - ) - ) - # [END automl_language_sentiment_analysis_get_dataset] - - # [START automl_language_text_classification_get_dataset] - print( - "Text classification dataset metadata: {}".format( - dataset.text_classification_dataset_metadata - ) - ) - # [END automl_language_text_classification_get_dataset] - - # [START automl_translate_get_dataset] - print("Translation dataset metadata:") - print( - "\tsource_language_code: {}".format( - dataset.translation_dataset_metadata.source_language_code - ) - ) - print( - "\ttarget_language_code: {}".format( - dataset.translation_dataset_metadata.target_language_code - ) - ) - # [END automl_translate_get_dataset] - - # [START automl_vision_classification_get_dataset] - print( - "Image classification dataset metadata: {}".format( - dataset.image_classification_dataset_metadata - ) - ) - # [END automl_vision_classification_get_dataset] - - # [START automl_vision_object_detection_get_dataset] - print( - "Image object detection dataset metadata: {}".format( - dataset.image_object_detection_dataset_metadata - ) - ) - # [END automl_vision_object_detection_get_dataset] diff --git a/automl/snippets/get_dataset_test.py b/automl/snippets/get_dataset_test.py deleted file mode 100644 index e6f422b6b7b..00000000000 --- a/automl/snippets/get_dataset_test.py +++ /dev/null @@ -1,29 +0,0 @@ -# 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.api_core.retry import Retry - -import get_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"] - - -@Retry() -def test_get_dataset(capsys): - get_dataset.get_dataset(PROJECT_ID, DATASET_ID) - out, _ = capsys.readouterr() - assert "Dataset name: " in out diff --git a/automl/snippets/get_model.py b/automl/snippets/get_model.py deleted file mode 100644 index 9b778fa608f..00000000000 --- a/automl/snippets/get_model.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2019 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_model(project_id, model_id): - """Get a model.""" - # [START automl_get_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - model = client.get_model(name=model_full_id) - - # Retrieve deployment state. - if model.deployment_state == automl.Model.DeploymentState.DEPLOYED: - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - # Display the model information. - print(f"Model name: {model.name}") - print("Model id: {}".format(model.name.split("/")[-1])) - print(f"Model display name: {model.display_name}") - print(f"Model create time: {model.create_time}") - print(f"Model deployment state: {deployment_state}") - # [END automl_get_model] diff --git a/automl/snippets/get_model_evaluation.py b/automl/snippets/get_model_evaluation.py deleted file mode 100644 index b657a78caf1..00000000000 --- a/automl/snippets/get_model_evaluation.py +++ /dev/null @@ -1,87 +0,0 @@ -# Copyright 2019 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_model_evaluation(project_id, model_id, model_evaluation_id): - """Get model evaluation.""" - # [START automl_language_entity_extraction_get_model_evaluation] - # [START automl_language_sentiment_analysis_get_model_evaluation] - # [START automl_language_text_classification_get_model_evaluation] - # [START automl_translate_get_model_evaluation] - # [START automl_vision_classification_get_model_evaluation] - # [START automl_vision_object_detection_get_model_evaluation] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # model_evaluation_id = "YOUR_MODEL_EVALUATION_ID" - - client = automl.AutoMlClient() - # Get the full path of the model evaluation. - model_path = client.model_path(project_id, "us-central1", model_id) - model_evaluation_full_id = f"{model_path}/modelEvaluations/{model_evaluation_id}" - - # Get complete detail of the model evaluation. - response = client.get_model_evaluation(name=model_evaluation_full_id) - - print(f"Model evaluation name: {response.name}") - print(f"Model annotation spec id: {response.annotation_spec_id}") - print(f"Create Time: {response.create_time}") - print(f"Evaluation example count: {response.evaluated_example_count}") - # [END automl_language_sentiment_analysis_get_model_evaluation] - # [END automl_language_text_classification_get_model_evaluation] - # [END automl_translate_get_model_evaluation] - # [END automl_vision_classification_get_model_evaluation] - # [END automl_vision_object_detection_get_model_evaluation] - print( - "Entity extraction model evaluation metrics: {}".format( - response.text_extraction_evaluation_metrics - ) - ) - # [END automl_language_entity_extraction_get_model_evaluation] - - # [START automl_language_sentiment_analysis_get_model_evaluation] - print( - "Sentiment analysis model evaluation metrics: {}".format( - response.text_sentiment_evaluation_metrics - ) - ) - # [END automl_language_sentiment_analysis_get_model_evaluation] - - # [START automl_language_text_classification_get_model_evaluation] - # [START automl_vision_classification_get_model_evaluation] - print( - "Classification model evaluation metrics: {}".format( - response.classification_evaluation_metrics - ) - ) - # [END automl_language_text_classification_get_model_evaluation] - # [END automl_vision_classification_get_model_evaluation] - - # [START automl_translate_get_model_evaluation] - print( - "Translation model evaluation metrics: {}".format( - response.translation_evaluation_metrics - ) - ) - # [END automl_translate_get_model_evaluation] - - # [START automl_vision_object_detection_get_model_evaluation] - print( - "Object detection model evaluation metrics: {}".format( - response.image_object_detection_evaluation_metrics - ) - ) - # [END automl_vision_object_detection_get_model_evaluation] diff --git a/automl/snippets/get_model_evaluation_test.py b/automl/snippets/get_model_evaluation_test.py deleted file mode 100644 index f6bb054af26..00000000000 --- a/automl/snippets/get_model_evaluation_test.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry -from google.cloud import automl -import pytest - -import get_model_evaluation - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -@Retry() -@pytest.fixture(scope="function") -def model_evaluation_id(): - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - evaluation = None - for e in client.list_model_evaluations(parent=model_full_id, filter=""): - evaluation = e - break - model_evaluation_id = evaluation.name.split(f"{MODEL_ID}/modelEvaluations/")[ - 1 - ].split("\n")[0] - yield model_evaluation_id - - -@Retry() -def test_get_model_evaluation(capsys, model_evaluation_id): - get_model_evaluation.get_model_evaluation(PROJECT_ID, MODEL_ID, model_evaluation_id) - out, _ = capsys.readouterr() - assert "Model evaluation name: " in out diff --git a/automl/snippets/get_model_test.py b/automl/snippets/get_model_test.py deleted file mode 100644 index afb3a28fb2d..00000000000 --- a/automl/snippets/get_model_test.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry - -import get_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -@Retry() -def test_get_model(capsys): - get_model.get_model(PROJECT_ID, MODEL_ID) - out, _ = capsys.readouterr() - assert "Model id: " in out diff --git a/automl/snippets/get_operation_status.py b/automl/snippets/get_operation_status.py deleted file mode 100644 index 6b17b16505a..00000000000 --- a/automl/snippets/get_operation_status.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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(f"Name: {response.name}") - print("Operation details:") - print(response) - # [END automl_get_operation_status] diff --git a/automl/snippets/get_operation_status_test.py b/automl/snippets/get_operation_status_test.py deleted file mode 100644 index 82ec58191b2..00000000000 --- a/automl/snippets/get_operation_status_test.py +++ /dev/null @@ -1,44 +0,0 @@ -# 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.api_core.retry import Retry -from google.cloud import automl -import pytest - -import get_operation_status - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -@pytest.fixture(scope="function") -def operation_id(): - client = automl.AutoMlClient() - project_location = f"projects/{PROJECT_ID}/locations/us-central1" - - generator = client._transport.operations_client.list_operations( - project_location, filter_="" - ).pages - page = next(generator) - operation = next(page) - yield operation.name - - -@Retry() -def test_get_operation_status(capsys, operation_id): - get_operation_status.get_operation_status(operation_id) - out, _ = capsys.readouterr() - assert "Operation details" in out diff --git a/automl/snippets/import_dataset.py b/automl/snippets/import_dataset.py deleted file mode 100644 index 70db4e4a9a1..00000000000 --- a/automl/snippets/import_dataset.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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 import_dataset(project_id, dataset_id, path): - """Import a dataset.""" - # [START automl_import_data] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # path = "gs://YOUR_BUCKET_ID/path/to/data.csv" - - client = automl.AutoMlClient() - # Get the full path of the dataset. - dataset_full_id = client.dataset_path(project_id, "us-central1", dataset_id) - # Get the multiple Google Cloud Storage URIs - input_uris = path.split(",") - gcs_source = automl.GcsSource(input_uris=input_uris) - input_config = automl.InputConfig(gcs_source=gcs_source) - # Import data from the input URI - response = client.import_data(name=dataset_full_id, input_config=input_config) - - print("Processing import...") - print(f"Data imported. {response.result()}") - # [END automl_import_data] diff --git a/automl/snippets/import_dataset_test.py b/automl/snippets/import_dataset_test.py deleted file mode 100644 index a569bc07787..00000000000 --- a/automl/snippets/import_dataset_test.py +++ /dev/null @@ -1,43 +0,0 @@ -# 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.api_core.retry import Retry - -import import_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = f"{PROJECT_ID}-lcm" -DATASET_ID = "TEN0000000000000000000" - - -@Retry() -def test_import_dataset(capsys): - # As importing a dataset can take a long time and only four operations can - # be run on a dataset at once. Try to import into a nonexistent dataset and - # confirm that the dataset was not found, but other elements of the request - # were valid. - try: - data = f"gs://{BUCKET_ID}/sentiment-analysis/dataset.csv" - import_dataset.import_dataset(PROJECT_ID, DATASET_ID, data) - out, _ = capsys.readouterr() - assert ( - "The Dataset doesn't exist or is inaccessible for use with AutoMl." in out - ) - except Exception as e: - assert ( - "The Dataset doesn't exist or is inaccessible for use with AutoMl." - in e.message - ) diff --git a/automl/snippets/language_entity_extraction_create_dataset.py b/automl/snippets/language_entity_extraction_create_dataset.py deleted file mode 100644 index 386411bf276..00000000000 --- a/automl/snippets/language_entity_extraction_create_dataset.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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 create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_language_entity_extraction_create_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "YOUR_DATASET_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - metadata = automl.TextExtractionDatasetMetadata() - dataset = automl.Dataset( - display_name=display_name, text_extraction_dataset_metadata=metadata - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(parent=project_location, dataset=dataset) - - created_dataset = response.result() - - # Display the dataset information - print(f"Dataset name: {created_dataset.name}") - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_language_entity_extraction_create_dataset] diff --git a/automl/snippets/language_entity_extraction_create_dataset_test.py b/automl/snippets/language_entity_extraction_create_dataset_test.py deleted file mode 100644 index dd0f160c5cd..00000000000 --- a/automl/snippets/language_entity_extraction_create_dataset_test.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl - -import language_entity_extraction_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -def test_entity_extraction_create_dataset(capsys): - # create dataset - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - language_entity_extraction_create_dataset.create_dataset(PROJECT_ID, dataset_name) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Delete the created dataset - dataset_id = out.splitlines()[1].split()[2] - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path(PROJECT_ID, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - response.result() diff --git a/automl/snippets/language_entity_extraction_create_model.py b/automl/snippets/language_entity_extraction_create_model.py deleted file mode 100644 index 1ae8dcf8904..00000000000 --- a/automl/snippets/language_entity_extraction_create_model.py +++ /dev/null @@ -1,43 +0,0 @@ -# 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 create_model(project_id, dataset_id, display_name): - """Create a model.""" - # [START automl_language_entity_extraction_create_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # display_name = "YOUR_MODEL_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Leave model unset to use the default base model provided by Google - metadata = automl.TextExtractionModelMetadata() - model = automl.Model( - display_name=display_name, - dataset_id=dataset_id, - text_extraction_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(parent=project_location, model=model) - - print(f"Training operation name: {response.operation.name}") - print("Training started...") - # [END automl_language_entity_extraction_create_model] diff --git a/automl/snippets/language_entity_extraction_create_model_test.py b/automl/snippets/language_entity_extraction_create_model_test.py deleted file mode 100644 index db5743d78a1..00000000000 --- a/automl/snippets/language_entity_extraction_create_model_test.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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.api_core.retry import Retry - -import language_entity_extraction_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "TEN0000000000000000000" - - -@Retry() -def test_entity_extraction_create_model(capsys): - # As entity extraction does not let you cancel model creation, instead try - # to create a model from a nonexistent dataset, but other elements of the - # request were valid. - try: - language_entity_extraction_create_model.create_model( - PROJECT_ID, DATASET_ID, "classification_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Dataset does not exist." in out - except Exception as e: - assert "Dataset does not exist." in e.message diff --git a/automl/snippets/language_entity_extraction_predict.py b/automl/snippets/language_entity_extraction_predict.py deleted file mode 100644 index 7075a39a6e7..00000000000 --- a/automl/snippets/language_entity_extraction_predict.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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 predict(project_id, model_id, content): - """Predict.""" - # [START automl_language_entity_extraction_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # content = "text to predict" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) - - # Supported mime_types: 'text/plain', 'text/html' - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet - text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") - payload = automl.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(name=model_full_id, payload=payload) - - for annotation_payload in response.payload: - print(f"Text Extract Entity Types: {annotation_payload.display_name}") - print(f"Text Score: {annotation_payload.text_extraction.score}") - text_segment = annotation_payload.text_extraction.text_segment - print(f"Text Extract Entity Content: {text_segment.content}") - print(f"Text Start Offset: {text_segment.start_offset}") - print(f"Text End Offset: {text_segment.end_offset}") - # [END automl_language_entity_extraction_predict] diff --git a/automl/snippets/language_entity_extraction_predict_test.py b/automl/snippets/language_entity_extraction_predict_test.py deleted file mode 100644 index a8d1657baa4..00000000000 --- a/automl/snippets/language_entity_extraction_predict_test.py +++ /dev/null @@ -1,52 +0,0 @@ -# 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 backoff -from google.api_core.exceptions import InternalServerError -from google.api_core.exceptions import ServiceUnavailable -from google.cloud import automl -import pytest - -import language_entity_extraction_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -@pytest.fixture(scope="function") -def verify_model_state(): - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - - model = client.get_model(name=model_full_id) - if model.deployment_state == automl.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(name=model_full_id) - response.result() - - -@backoff.on_exception( - backoff.expo, (InternalServerError, ServiceUnavailable), max_tries=3 -) -def test_predict(capsys, verify_model_state): - verify_model_state - text = ( - "Constitutional mutations in the WT1 gene in patients with " - "Denys-Drash syndrome." - ) - language_entity_extraction_predict.predict(PROJECT_ID, MODEL_ID, text) - out, _ = capsys.readouterr() - assert "Text Extract Entity Types: " in out diff --git a/automl/snippets/language_sentiment_analysis_create_dataset.py b/automl/snippets/language_sentiment_analysis_create_dataset.py deleted file mode 100644 index 94225fc82bd..00000000000 --- a/automl/snippets/language_sentiment_analysis_create_dataset.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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 create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_language_sentiment_analysis_create_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "YOUR_DATASET_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - - # Each dataset requires a sentiment score with a defined sentiment_max - # value, for more information on TextSentimentDatasetMetadata, see: - # https://cloud.google.com/natural-language/automl/docs/prepare#sentiment-analysis - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsentimentdatasetmetadata - metadata = automl.TextSentimentDatasetMetadata( - sentiment_max=4 - ) # Possible max sentiment score: 1-10 - - dataset = automl.Dataset( - display_name=display_name, text_sentiment_dataset_metadata=metadata - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(parent=project_location, dataset=dataset) - - created_dataset = response.result() - - # Display the dataset information - print(f"Dataset name: {created_dataset.name}") - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_language_sentiment_analysis_create_dataset] diff --git a/automl/snippets/language_sentiment_analysis_create_dataset_test.py b/automl/snippets/language_sentiment_analysis_create_dataset_test.py deleted file mode 100644 index 159d9af1343..00000000000 --- a/automl/snippets/language_sentiment_analysis_create_dataset_test.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl - -import language_sentiment_analysis_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -def test_sentiment_analysis_create_dataset(capsys): - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - language_sentiment_analysis_create_dataset.create_dataset(PROJECT_ID, dataset_name) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Delete the created dataset - dataset_id = out.splitlines()[1].split()[2] - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path(PROJECT_ID, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - response.result() diff --git a/automl/snippets/language_sentiment_analysis_create_model.py b/automl/snippets/language_sentiment_analysis_create_model.py deleted file mode 100644 index 62a0cf13121..00000000000 --- a/automl/snippets/language_sentiment_analysis_create_model.py +++ /dev/null @@ -1,44 +0,0 @@ -# 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 create_model(project_id, dataset_id, display_name): - """Create a model.""" - # [START automl_language_sentiment_analysis_create_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # display_name = "YOUR_MODEL_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Leave model unset to use the default base model provided by Google - metadata = automl.TextSentimentModelMetadata() - model = automl.Model( - display_name=display_name, - dataset_id=dataset_id, - text_sentiment_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(parent=project_location, model=model) - - print(f"Training operation name: {response.operation.name}") - print("Training started...") - # [END automl_language_sentiment_analysis_create_model] - return response diff --git a/automl/snippets/language_sentiment_analysis_create_model_test.py b/automl/snippets/language_sentiment_analysis_create_model_test.py deleted file mode 100644 index 12ebab6c6a2..00000000000 --- a/automl/snippets/language_sentiment_analysis_create_model_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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.api_core.retry import Retry - -import language_sentiment_analysis_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "TST00000000000000000" - - -@Retry() -def test_sentiment_analysis_create_model(capsys): - try: - language_sentiment_analysis_create_model.create_model( - PROJECT_ID, DATASET_ID, "lang_sent_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Dataset does not exist." in out - except Exception as e: - assert "Dataset does not exist." in e.message diff --git a/automl/snippets/language_sentiment_analysis_predict.py b/automl/snippets/language_sentiment_analysis_predict.py deleted file mode 100644 index 686756d73ac..00000000000 --- a/automl/snippets/language_sentiment_analysis_predict.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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 predict(project_id, model_id, content): - """Predict.""" - # [START automl_language_sentiment_analysis_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # content = "text to predict" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) - - # Supported mime_types: 'text/plain', 'text/html' - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet - text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") - payload = automl.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(name=model_full_id, payload=payload) - - for annotation_payload in response.payload: - print(f"Predicted class name: {annotation_payload.display_name}") - print( - "Predicted sentiment score: {}".format( - annotation_payload.text_sentiment.sentiment - ) - ) - # [END automl_language_sentiment_analysis_predict] diff --git a/automl/snippets/language_sentiment_analysis_predict_test.py b/automl/snippets/language_sentiment_analysis_predict_test.py deleted file mode 100644 index a405e3de537..00000000000 --- a/automl/snippets/language_sentiment_analysis_predict_test.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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.api_core.retry import Retry -from google.cloud import automl -import pytest - -import language_sentiment_analysis_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["SENTIMENT_ANALYSIS_MODEL_ID"] - - -@pytest.fixture(scope="function", autouse=True) -def setup(): - # Verify the model is deployed before trying to predict - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - - model = client.get_model(name=model_full_id) - if model.deployment_state == automl.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(name=model_full_id) - response.result() - - -@Retry() -def test_sentiment_analysis_predict(capsys): - text = "Hopefully this Claritin kicks in soon" - language_sentiment_analysis_predict.predict(PROJECT_ID, MODEL_ID, text) - out, _ = capsys.readouterr() - assert "Predicted sentiment score: " in out diff --git a/automl/snippets/language_text_classification_create_dataset.py b/automl/snippets/language_text_classification_create_dataset.py deleted file mode 100644 index 4156a62ca52..00000000000 --- a/automl/snippets/language_text_classification_create_dataset.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_language_text_classification_create_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "YOUR_DATASET_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Specify the classification type - # Types: - # MultiLabel: Multiple labels are allowed for one example. - # MultiClass: At most one label is allowed per example. - metadata = automl.TextClassificationDatasetMetadata( - classification_type=automl.ClassificationType.MULTICLASS - ) - dataset = automl.Dataset( - display_name=display_name, - text_classification_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(parent=project_location, dataset=dataset) - - created_dataset = response.result() - - # Display the dataset information - print(f"Dataset name: {created_dataset.name}") - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_language_text_classification_create_dataset] diff --git a/automl/snippets/language_text_classification_create_dataset_test.py b/automl/snippets/language_text_classification_create_dataset_test.py deleted file mode 100644 index 5d7ab4ea3a2..00000000000 --- a/automl/snippets/language_text_classification_create_dataset_test.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl - -import language_text_classification_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -def test_text_classification_create_dataset(capsys): - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - language_text_classification_create_dataset.create_dataset(PROJECT_ID, dataset_name) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Delete the created dataset - dataset_id = out.splitlines()[1].split()[2] - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path(PROJECT_ID, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - response.result() diff --git a/automl/snippets/language_text_classification_create_model.py b/automl/snippets/language_text_classification_create_model.py deleted file mode 100644 index 411f3bedd33..00000000000 --- a/automl/snippets/language_text_classification_create_model.py +++ /dev/null @@ -1,43 +0,0 @@ -# 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 create_model(project_id, dataset_id, display_name): - """Create a model.""" - # [START automl_language_text_classification_create_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # display_name = "YOUR_MODEL_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Leave model unset to use the default base model provided by Google - metadata = automl.TextClassificationModelMetadata() - model = automl.Model( - display_name=display_name, - dataset_id=dataset_id, - text_classification_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(parent=project_location, model=model) - - print(f"Training operation name: {response.operation.name}") - print("Training started...") - # [END automl_language_text_classification_create_model] diff --git a/automl/snippets/language_text_classification_create_model_test.py b/automl/snippets/language_text_classification_create_model_test.py deleted file mode 100644 index a9181f89258..00000000000 --- a/automl/snippets/language_text_classification_create_model_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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.api_core.retry import Retry - -import language_text_classification_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "TCN00000000000000000000" - - -@Retry() -def test_text_classification_create_model(capsys): - try: - language_text_classification_create_model.create_model( - PROJECT_ID, DATASET_ID, "lang_text_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Dataset does not exist." in out - except Exception as e: - assert "Dataset does not exist." in e.message diff --git a/automl/snippets/language_text_classification_predict.py b/automl/snippets/language_text_classification_predict.py deleted file mode 100644 index f3777e3d0d6..00000000000 --- a/automl/snippets/language_text_classification_predict.py +++ /dev/null @@ -1,41 +0,0 @@ -# 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 predict(project_id, model_id, content): - """Predict.""" - # [START automl_language_text_classification_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # content = "text to predict" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) - - # Supported mime_types: 'text/plain', 'text/html' - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet - text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") - payload = automl.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(name=model_full_id, payload=payload) - - for annotation_payload in response.payload: - print(f"Predicted class name: {annotation_payload.display_name}") - print(f"Predicted class score: {annotation_payload.classification.score}") - # [END automl_language_text_classification_predict] diff --git a/automl/snippets/language_text_classification_predict_test.py b/automl/snippets/language_text_classification_predict_test.py deleted file mode 100644 index db9fc3c59bf..00000000000 --- a/automl/snippets/language_text_classification_predict_test.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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.api_core.retry import Retry -from google.cloud import automl -import pytest - -import language_text_classification_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["TEXT_CLASSIFICATION_MODEL_ID"] - - -@pytest.fixture(scope="function") -def verify_model_state(): - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - - model = client.get_model(name=model_full_id) - if model.deployment_state == automl.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(name=model_full_id) - response.result() - - -@Retry() -def test_text_classification_predict(capsys, verify_model_state): - verify_model_state - text = "Fruit and nut flavour" - language_text_classification_predict.predict(PROJECT_ID, MODEL_ID, text) - out, _ = capsys.readouterr() - assert "Predicted class name: " in out diff --git a/automl/snippets/list_datasets.py b/automl/snippets/list_datasets.py deleted file mode 100644 index c496776abc3..00000000000 --- a/automl/snippets/list_datasets.py +++ /dev/null @@ -1,99 +0,0 @@ -# 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_datasets(project_id): - """List datasets.""" - # [START automl_language_entity_extraction_list_datasets] - # [START automl_language_sentiment_analysis_list_datasets] - # [START automl_language_text_classification_list_datasets] - # [START automl_translate_list_datasets] - # [START automl_vision_classification_list_datasets] - # [START automl_vision_object_detection_list_datasets] - 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 = f"projects/{project_id}/locations/us-central1" - - # List all the datasets available in the region. - request = automl.ListDatasetsRequest(parent=project_location, filter="") - response = client.list_datasets(request=request) - - print("List of datasets:") - for dataset in response: - print(f"Dataset name: {dataset.name}") - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print(f"Dataset display name: {dataset.display_name}") - print(f"Dataset create time: {dataset.create_time}") - # [END automl_language_sentiment_analysis_list_datasets] - # [END automl_language_text_classification_list_datasets] - # [END automl_translate_list_datasets] - # [END automl_vision_classification_list_datasets] - # [END automl_vision_object_detection_list_datasets] - print( - "Text extraction dataset metadata: {}".format( - dataset.text_extraction_dataset_metadata - ) - ) - # [END automl_language_entity_extraction_list_datasets] - - # [START automl_language_sentiment_analysis_list_datasets] - print( - "Text sentiment dataset metadata: {}".format( - dataset.text_sentiment_dataset_metadata - ) - ) - # [END automl_language_sentiment_analysis_list_datasets] - - # [START automl_language_text_classification_list_datasets] - print( - "Text classification dataset metadata: {}".format( - dataset.text_classification_dataset_metadata - ) - ) - # [END automl_language_text_classification_list_datasets] - - # [START automl_translate_list_datasets] - print("Translation dataset metadata:") - print( - "\tsource_language_code: {}".format( - dataset.translation_dataset_metadata.source_language_code - ) - ) - print( - "\ttarget_language_code: {}".format( - dataset.translation_dataset_metadata.target_language_code - ) - ) - # [END automl_translate_list_datasets] - - # [START automl_vision_classification_list_datasets] - print( - "Image classification dataset metadata: {}".format( - dataset.image_classification_dataset_metadata - ) - ) - # [END automl_vision_classification_list_datasets] - - # [START automl_vision_object_detection_list_datasets] - print( - "Image object detection dataset metadata: {}".format( - dataset.image_object_detection_dataset_metadata - ) - ) - # [END automl_vision_object_detection_list_datasets] diff --git a/automl/snippets/list_datasets_test.py b/automl/snippets/list_datasets_test.py deleted file mode 100644 index 1e4c8ac294a..00000000000 --- a/automl/snippets/list_datasets_test.py +++ /dev/null @@ -1,30 +0,0 @@ -# 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.api_core.retry import Retry - -import list_datasets - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"] - - -@Retry() -def test_list_dataset(capsys): - # list datasets - list_datasets.list_datasets(PROJECT_ID) - out, _ = capsys.readouterr() - assert f"Dataset id: {DATASET_ID}" in out diff --git a/automl/snippets/list_model_evaluations.py b/automl/snippets/list_model_evaluations.py deleted file mode 100644 index 7d99cabecd6..00000000000 --- a/automl/snippets/list_model_evaluations.py +++ /dev/null @@ -1,84 +0,0 @@ -# Copyright 2019 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_model_evaluations(project_id, model_id): - """List model evaluations.""" - # [START automl_language_entity_extraction_list_model_evaluations] - # [START automl_language_sentiment_analysis_list_model_evaluations] - # [START automl_language_text_classification_list_model_evaluations] - # [START automl_translate_list_model_evaluations] - # [START automl_vision_classification_list_model_evaluations] - # [START automl_vision_object_detection_list_model_evaluations] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - - print("List of model evaluations:") - for evaluation in client.list_model_evaluations(parent=model_full_id, filter=""): - print(f"Model evaluation name: {evaluation.name}") - print(f"Model annotation spec id: {evaluation.annotation_spec_id}") - print(f"Create Time: {evaluation.create_time}") - print(f"Evaluation example count: {evaluation.evaluated_example_count}") - # [END automl_language_sentiment_analysis_list_model_evaluations] - # [END automl_language_text_classification_list_model_evaluations] - # [END automl_translate_list_model_evaluations] - # [END automl_vision_classification_list_model_evaluations] - # [END automl_vision_object_detection_list_model_evaluations] - print( - "Entity extraction model evaluation metrics: {}".format( - evaluation.text_extraction_evaluation_metrics - ) - ) - # [END automl_language_entity_extraction_list_model_evaluations] - - # [START automl_language_sentiment_analysis_list_model_evaluations] - print( - "Sentiment analysis model evaluation metrics: {}".format( - evaluation.text_sentiment_evaluation_metrics - ) - ) - # [END automl_language_sentiment_analysis_list_model_evaluations] - - # [START automl_language_text_classification_list_model_evaluations] - # [START automl_vision_classification_list_model_evaluations] - print( - "Classification model evaluation metrics: {}".format( - evaluation.classification_evaluation_metrics - ) - ) - # [END automl_language_text_classification_list_model_evaluations] - # [END automl_vision_classification_list_model_evaluations] - - # [START automl_translate_list_model_evaluations] - print( - "Translation model evaluation metrics: {}".format( - evaluation.translation_evaluation_metrics - ) - ) - # [END automl_translate_list_model_evaluations] - - # [START automl_vision_object_detection_list_model_evaluations] - print( - "Object detection model evaluation metrics: {}\n\n".format( - evaluation.image_object_detection_evaluation_metrics - ) - ) - # [END automl_vision_object_detection_list_model_evaluations] diff --git a/automl/snippets/list_model_evaluations_test.py b/automl/snippets/list_model_evaluations_test.py deleted file mode 100644 index 9a24602b10b..00000000000 --- a/automl/snippets/list_model_evaluations_test.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry - -import list_model_evaluations - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -@Retry() -def test_list_model_evaluations(capsys): - list_model_evaluations.list_model_evaluations(PROJECT_ID, MODEL_ID) - out, _ = capsys.readouterr() - assert "Model evaluation name: " in out diff --git a/automl/snippets/list_models.py b/automl/snippets/list_models.py deleted file mode 100644 index 2eabeacf42b..00000000000 --- a/automl/snippets/list_models.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2019 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_models(project_id): - """List models.""" - # [START automl_list_models] - 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 = f"projects/{project_id}/locations/us-central1" - - request = automl.ListModelsRequest(parent=project_location, filter="") - response = client.list_models(request=request) - - print("List of models:") - for model in response: - # Display the model information. - if model.deployment_state == automl.Model.DeploymentState.DEPLOYED: - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - print(f"Model name: {model.name}") - print("Model id: {}".format(model.name.split("/")[-1])) - print(f"Model display name: {model.display_name}") - print(f"Model create time: {model.create_time}") - print(f"Model deployment state: {deployment_state}") - # [END automl_list_models] diff --git a/automl/snippets/list_models_test.py b/automl/snippets/list_models_test.py deleted file mode 100644 index 1a662c84dc2..00000000000 --- a/automl/snippets/list_models_test.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry - -import list_models - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -def test_list_models(capsys): - list_models.list_models(PROJECT_ID) - out, _ = capsys.readouterr() - assert "Model id: " in out diff --git a/automl/snippets/list_operation_status.py b/automl/snippets/list_operation_status.py deleted file mode 100644 index dff3d112e9b..00000000000 --- a/automl/snippets/list_operation_status.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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 = f"projects/{project_id}/locations/us-central1" - # List all the operations names available in the region. - response = client._transport.operations_client.list_operations( - name=project_location, filter_="", timeout=5 - ) - - print("List of operations:") - for operation in response: - print(f"Name: {operation.name}") - print("Operation details:") - print(operation) - # [END automl_list_operation_status] diff --git a/automl/snippets/list_operation_status_test.py b/automl/snippets/list_operation_status_test.py deleted file mode 100644 index aaeccaaf1ab..00000000000 --- a/automl/snippets/list_operation_status_test.py +++ /dev/null @@ -1,35 +0,0 @@ -# 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 backoff -from google.api_core.exceptions import InternalServerError -import pytest - -import list_operation_status - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@pytest.mark.slow -def test_list_operation_status(capsys): - # We saw 500 InternalServerError. Now we just retry few times. - @backoff.on_exception(backoff.expo, InternalServerError, max_time=120) - def run_sample(): - list_operation_status.list_operation_status(PROJECT_ID) - - run_sample() - out, _ = capsys.readouterr() - assert "Operation details" in out diff --git a/automl/snippets/noxfile_config.py b/automl/snippets/noxfile_config.py deleted file mode 100644 index b4a45d95757..00000000000 --- a/automl/snippets/noxfile_config.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2024 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. - -# Default TEST_CONFIG_OVERRIDE for python repos. - -# You can copy this file into your directory, then it will be inported from -# the noxfile.py. - -# The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py - -TEST_CONFIG_OVERRIDE = { - # You can opt out from the test for specific Python versions. - "ignored_versions": ["3.8", "3.9", "3.11", "3.12", "3.13"], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - # "enforce_type_hints": False, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - # "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - # "envs": {}, -} diff --git a/automl/snippets/requirements-test.txt b/automl/snippets/requirements-test.txt deleted file mode 100644 index 79932f83530..00000000000 --- a/automl/snippets/requirements-test.txt +++ /dev/null @@ -1,2 +0,0 @@ -backoff==2.2.1 -pytest==9.0.3; python_version >= "3.10" diff --git a/automl/snippets/requirements.txt b/automl/snippets/requirements.txt deleted file mode 100644 index 6386048f6eb..00000000000 --- a/automl/snippets/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -google-cloud-translate==3.18.0 -google-cloud-storage==2.9.0 -google-cloud-automl==2.14.1 diff --git a/automl/snippets/resources/input.txt b/automl/snippets/resources/input.txt deleted file mode 100644 index ac25dac422b..00000000000 --- a/automl/snippets/resources/input.txt +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2022 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. -Tell me how this ends \ No newline at end of file diff --git a/automl/snippets/resources/salad.jpg b/automl/snippets/resources/salad.jpg deleted file mode 100644 index a7f960b5030..00000000000 Binary files a/automl/snippets/resources/salad.jpg and /dev/null differ diff --git a/automl/snippets/resources/test.png b/automl/snippets/resources/test.png deleted file mode 100644 index 653342a46e5..00000000000 Binary files a/automl/snippets/resources/test.png and /dev/null differ diff --git a/automl/snippets/translate_create_dataset.py b/automl/snippets/translate_create_dataset.py deleted file mode 100644 index c483ec048d5..00000000000 --- a/automl/snippets/translate_create_dataset.py +++ /dev/null @@ -1,47 +0,0 @@ -# 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 create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_translate_create_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "YOUR_DATASET_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # For a list of supported languages, see: - # https://cloud.google.com/translate/automl/docs/languages - dataset_metadata = automl.TranslationDatasetMetadata( - source_language_code="en", target_language_code="ja" - ) - dataset = automl.Dataset( - display_name=display_name, - translation_dataset_metadata=dataset_metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(parent=project_location, dataset=dataset) - - created_dataset = response.result() - - # Display the dataset information - print(f"Dataset name: {created_dataset.name}") - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_translate_create_dataset] diff --git a/automl/snippets/translate_create_dataset_test.py b/automl/snippets/translate_create_dataset_test.py deleted file mode 100644 index a1b9d1d54df..00000000000 --- a/automl/snippets/translate_create_dataset_test.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl - -import translate_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -def test_translate_create_dataset(capsys): - # create dataset - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - translate_create_dataset.create_dataset(PROJECT_ID, dataset_name) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Delete the created dataset - dataset_id = out.splitlines()[1].split()[2] - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path(PROJECT_ID, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - response.result() diff --git a/automl/snippets/translate_create_model.py b/automl/snippets/translate_create_model.py deleted file mode 100644 index 7fef364081d..00000000000 --- a/automl/snippets/translate_create_model.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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 create_model(project_id, dataset_id, display_name): - """Create a model.""" - # [START automl_translate_create_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # display_name = "YOUR_MODEL_NAME" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - translation_model_metadata = automl.TranslationModelMetadata() - model = automl.Model( - display_name=display_name, - dataset_id=dataset_id, - translation_model_metadata=translation_model_metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(parent=project_location, model=model) - - print(f"Training operation name: {response.operation.name}") - print("Training started...") - # [END automl_translate_create_model] diff --git a/automl/snippets/translate_create_model_test.py b/automl/snippets/translate_create_model_test.py deleted file mode 100644 index 078f6775fd5..00000000000 --- a/automl/snippets/translate_create_model_test.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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.api_core.retry import Retry - -import translate_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "TRL00000000000000000" - - -@Retry() -def test_translate_create_model(capsys): - try: - translate_create_model.create_model( - PROJECT_ID, DATASET_ID, "translate_test_create_model" - ) - out, _ = capsys.readouterr() - # After setting DATASET_ID, change line below to - # assert "Training started..." in out - assert "Dataset does not exist." in out - except Exception as e: - # After setting DATASET_ID, change line below to - # assert "Training started..." in e.message - assert "Dataset does not exist." in e.message diff --git a/automl/snippets/translate_predict.py b/automl/snippets/translate_predict.py deleted file mode 100644 index 16760caa6b4..00000000000 --- a/automl/snippets/translate_predict.py +++ /dev/null @@ -1,43 +0,0 @@ -# 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 predict(project_id, model_id, file_path): - """Predict.""" - # [START automl_translate_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # file_path = "path_to_local_file.txt" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) - - # Read the file content for translation. - with open(file_path, "rb") as content_file: - content = content_file.read() - content.decode("utf-8") - - text_snippet = automl.TextSnippet(content=content) - payload = automl.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(name=model_full_id, payload=payload) - translated_content = response.payload[0].translation.translated_content - - print(f"Translated content: {translated_content.content}") - # [END automl_translate_predict] diff --git a/automl/snippets/translate_predict_test.py b/automl/snippets/translate_predict_test.py deleted file mode 100644 index 27f872f559c..00000000000 --- a/automl/snippets/translate_predict_test.py +++ /dev/null @@ -1,44 +0,0 @@ -# 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.api_core.retry import Retry -from google.cloud import automl -import pytest - -import translate_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["TRANSLATION_MODEL_ID"] - - -@pytest.fixture(scope="function", autouse=True) -def setup(): - # Verify the model is deployed before trying to predict - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - - model = client.get_model(name=model_full_id) - if model.deployment_state == automl.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(name=model_full_id) - response.result() - - -@Retry() -def test_translate_predict(capsys): - translate_predict.predict(PROJECT_ID, MODEL_ID, "resources/input.txt") - out, _ = capsys.readouterr() - assert "Translated content: " in out diff --git a/automl/snippets/undeploy_model.py b/automl/snippets/undeploy_model.py deleted file mode 100644 index dea80bff209..00000000000 --- a/automl/snippets/undeploy_model.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2019 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 undeploy_model(project_id, model_id): - """Undeploy a model.""" - # [START automl_undeploy_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - response = client.undeploy_model(name=model_full_id) - - print(f"Model undeployment finished. {response.result()}") - # [END automl_undeploy_model] diff --git a/automl/snippets/undeploy_model_test.py b/automl/snippets/undeploy_model_test.py deleted file mode 100644 index 37a55113eae..00000000000 --- a/automl/snippets/undeploy_model_test.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2019 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.api_core.retry import Retry -import pytest - -import undeploy_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "TRL0000000000000000000" - - -@Retry() -@pytest.mark.slow -def test_undeploy_model(capsys): - # As model undeployment can take a long time, instead try to deploy a - # nonexistent model and confirm that the model was not found, but other - # elements of the request were valid. - try: - undeploy_model.undeploy_model(PROJECT_ID, MODEL_ID) - out, _ = capsys.readouterr() - assert "The model does not exist" in out - except Exception as e: - assert "The model does not exist" in e.message diff --git a/automl/snippets/vision_classification_create_dataset.py b/automl/snippets/vision_classification_create_dataset.py deleted file mode 100644 index 5ee51389820..00000000000 --- a/automl/snippets/vision_classification_create_dataset.py +++ /dev/null @@ -1,52 +0,0 @@ -# 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 create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_vision_classification_create_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "your_datasets_display_name" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Specify the classification type - # Types: - # MultiLabel: Multiple labels are allowed for one example. - # MultiClass: At most one label is allowed per example. - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#classificationtype - metadata = automl.ImageClassificationDatasetMetadata( - classification_type=automl.ClassificationType.MULTILABEL - ) - dataset = automl.Dataset( - display_name=display_name, - image_classification_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset( - parent=project_location, dataset=dataset, timeout=300 - ) - - created_dataset = response.result() - - # Display the dataset information - print(f"Dataset name: {created_dataset.name}") - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_vision_classification_create_dataset] diff --git a/automl/snippets/vision_classification_create_dataset_test.py b/automl/snippets/vision_classification_create_dataset_test.py deleted file mode 100644 index a8070661fef..00000000000 --- a/automl/snippets/vision_classification_create_dataset_test.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl -import pytest - -import vision_classification_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -@pytest.mark.slow -def test_vision_classification_create_dataset(capsys): - # create dataset - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - vision_classification_create_dataset.create_dataset(PROJECT_ID, dataset_name) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Delete the created dataset - dataset_id = out.splitlines()[1].split()[2] - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path(PROJECT_ID, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - response.result() diff --git a/automl/snippets/vision_classification_create_model.py b/automl/snippets/vision_classification_create_model.py deleted file mode 100644 index df5bdc1616a..00000000000 --- a/automl/snippets/vision_classification_create_model.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 create_model(project_id, dataset_id, display_name): - """Create a model.""" - # [START automl_vision_classification_create_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # display_name = "your_models_display_name" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Leave model unset to use the default base model provided by Google - # train_budget_milli_node_hours: The actual train_cost will be equal or - # less than this value. - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#imageclassificationmodelmetadata - metadata = automl.ImageClassificationModelMetadata( - train_budget_milli_node_hours=24000 - ) - model = automl.Model( - display_name=display_name, - dataset_id=dataset_id, - image_classification_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(parent=project_location, model=model) - - print(f"Training operation name: {response.operation.name}") - print("Training started...") - # [END automl_vision_classification_create_model] - return response diff --git a/automl/snippets/vision_classification_create_model_test.py b/automl/snippets/vision_classification_create_model_test.py deleted file mode 100644 index 566fee6d627..00000000000 --- a/automl/snippets/vision_classification_create_model_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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.api_core.retry import Retry - -import vision_classification_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "ICN000000000000000000" - - -@Retry() -def test_vision_classification_create_model(capsys): - try: - vision_classification_create_model.create_model( - PROJECT_ID, DATASET_ID, "classification_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Dataset does not exist." in out - except Exception as e: - assert "Dataset does not exist." in e.message diff --git a/automl/snippets/vision_classification_deploy_model_node_count.py b/automl/snippets/vision_classification_deploy_model_node_count.py deleted file mode 100644 index 59b3a26c758..00000000000 --- a/automl/snippets/vision_classification_deploy_model_node_count.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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 deploy_model(project_id, model_id): - """Deploy a model with a specified node count.""" - # [START automl_vision_classification_deploy_model_node_count] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - - # node count determines the number of nodes to deploy the model on. - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#imageclassificationmodeldeploymentmetadata - metadata = automl.ImageClassificationModelDeploymentMetadata(node_count=2) - - request = automl.DeployModelRequest( - name=model_full_id, image_classification_model_deployment_metadata=metadata - ) - response = client.deploy_model(request=request) - - print(f"Model deployment finished. {response.result()}") - # [END automl_vision_classification_deploy_model_node_count] diff --git a/automl/snippets/vision_classification_deploy_model_node_count_test.py b/automl/snippets/vision_classification_deploy_model_node_count_test.py deleted file mode 100644 index c740c9930a6..00000000000 --- a/automl/snippets/vision_classification_deploy_model_node_count_test.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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.api_core.retry import Retry -import pytest - -import vision_classification_deploy_model_node_count - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "ICN0000000000000000000" - - -@Retry() -@pytest.mark.slow -def test_classification_deploy_model_with_node_count(capsys): - # As model deployment can take a long time, instead try to deploy a - # nonexistent model and confirm that the model was not found, but other - # elements of the request were valid. - try: - vision_classification_deploy_model_node_count.deploy_model(PROJECT_ID, MODEL_ID) - out, _ = capsys.readouterr() - assert "The model does not exist" in out - except Exception as e: - assert "The model does not exist" in e.message diff --git a/automl/snippets/vision_classification_predict.py b/automl/snippets/vision_classification_predict.py deleted file mode 100644 index 33e777da55a..00000000000 --- a/automl/snippets/vision_classification_predict.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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 predict(project_id, model_id, file_path): - """Predict.""" - # [START automl_vision_classification_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # file_path = "path_to_local_file.jpg" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) - - # Read the file. - with open(file_path, "rb") as content_file: - content = content_file.read() - - image = automl.Image(image_bytes=content) - payload = automl.ExamplePayload(image=image) - - # params is additional domain-specific parameters. - # score_threshold is used to filter the result - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest - params = {"score_threshold": "0.8"} - - request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) - response = prediction_client.predict(request=request) - - print("Prediction results:") - for result in response.payload: - print(f"Predicted class name: {result.display_name}") - print(f"Predicted class score: {result.classification.score}") - # [END automl_vision_classification_predict] diff --git a/automl/snippets/vision_classification_predict_test.py b/automl/snippets/vision_classification_predict_test.py deleted file mode 100644 index f6dc4928316..00000000000 --- a/automl/snippets/vision_classification_predict_test.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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.api_core.retry import Retry -from google.cloud import automl -import pytest - -import vision_classification_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["VISION_CLASSIFICATION_MODEL_ID"] - - -@pytest.fixture(scope="function", autouse=True) -def setup(): - # Verify the model is deployed before tyring to predict - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - - model = client.get_model(name=model_full_id) - if model.deployment_state == automl.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(name=model_full_id) - response.result() - - -@Retry() -def test_vision_classification_predict(capsys): - file_path = "resources/test.png" - vision_classification_predict.predict(PROJECT_ID, MODEL_ID, file_path) - out, _ = capsys.readouterr() - assert "Predicted class name:" in out diff --git a/automl/snippets/vision_object_detection_create_dataset.py b/automl/snippets/vision_object_detection_create_dataset.py deleted file mode 100644 index fe8cb0cbfcf..00000000000 --- a/automl/snippets/vision_object_detection_create_dataset.py +++ /dev/null @@ -1,43 +0,0 @@ -# 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 create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_vision_object_detection_create_dataset] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "your_datasets_display_name" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - metadata = automl.ImageObjectDetectionDatasetMetadata() - dataset = automl.Dataset( - display_name=display_name, - image_object_detection_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(parent=project_location, dataset=dataset) - - created_dataset = response.result() - - # Display the dataset information - print(f"Dataset name: {created_dataset.name}") - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_vision_object_detection_create_dataset] diff --git a/automl/snippets/vision_object_detection_create_dataset_test.py b/automl/snippets/vision_object_detection_create_dataset_test.py deleted file mode 100644 index 8ef91e8d983..00000000000 --- a/automl/snippets/vision_object_detection_create_dataset_test.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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 datetime -import os - -from google.api_core.retry import Retry -from google.cloud import automl -import pytest - -import vision_object_detection_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@Retry() -@pytest.mark.slow -def test_vision_object_detection_create_dataset(capsys): - # create dataset - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - vision_object_detection_create_dataset.create_dataset(PROJECT_ID, dataset_name) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Delete the created dataset - dataset_id = out.splitlines()[1].split()[2] - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path(PROJECT_ID, "us-central1", dataset_id) - response = client.delete_dataset(name=dataset_full_id) - response.result() diff --git a/automl/snippets/vision_object_detection_create_model.py b/automl/snippets/vision_object_detection_create_model.py deleted file mode 100644 index 1c69eae1387..00000000000 --- a/automl/snippets/vision_object_detection_create_model.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 create_model(project_id, dataset_id, display_name): - """Create a model.""" - # [START automl_vision_object_detection_create_model] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # dataset_id = "YOUR_DATASET_ID" - # display_name = "your_models_display_name" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = f"projects/{project_id}/locations/us-central1" - # Leave model unset to use the default base model provided by Google - # train_budget_milli_node_hours: The actual train_cost will be equal or - # less than this value. - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#imageobjectdetectionmodelmetadata - metadata = automl.ImageObjectDetectionModelMetadata( - train_budget_milli_node_hours=24000 - ) - model = automl.Model( - display_name=display_name, - dataset_id=dataset_id, - image_object_detection_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(parent=project_location, model=model) - - print(f"Training operation name: {response.operation.name}") - print("Training started...") - # [END automl_vision_object_detection_create_model] - return response diff --git a/automl/snippets/vision_object_detection_create_model_test.py b/automl/snippets/vision_object_detection_create_model_test.py deleted file mode 100644 index df2f58f7a7f..00000000000 --- a/automl/snippets/vision_object_detection_create_model_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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.api_core.retry import Retry - -import vision_object_detection_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "IOD0000000000000000" - - -@Retry() -def test_vision_object_detection_create_model(capsys): - try: - vision_object_detection_create_model.create_model( - PROJECT_ID, DATASET_ID, "object_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Dataset does not exist." in out - except Exception as e: - assert "Dataset does not exist." in e.message diff --git a/automl/snippets/vision_object_detection_deploy_model_node_count.py b/automl/snippets/vision_object_detection_deploy_model_node_count.py deleted file mode 100644 index a21bc2f8f11..00000000000 --- a/automl/snippets/vision_object_detection_deploy_model_node_count.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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 deploy_model(project_id, model_id): - """Deploy a model with a specified node count.""" - # [START automl_vision_object_detection_deploy_model_node_count] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - - client = automl.AutoMlClient() - # Get the full path of the model. - model_full_id = client.model_path(project_id, "us-central1", model_id) - - # node count determines the number of nodes to deploy the model on. - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#imageobjectdetectionmodeldeploymentmetadata - metadata = automl.ImageObjectDetectionModelDeploymentMetadata(node_count=2) - - request = automl.DeployModelRequest( - name=model_full_id, - image_object_detection_model_deployment_metadata=metadata, - ) - response = client.deploy_model(request=request) - - print(f"Model deployment finished. {response.result()}") - # [END automl_vision_object_detection_deploy_model_node_count] diff --git a/automl/snippets/vision_object_detection_deploy_model_node_count_test.py b/automl/snippets/vision_object_detection_deploy_model_node_count_test.py deleted file mode 100644 index 7a6f3112c84..00000000000 --- a/automl/snippets/vision_object_detection_deploy_model_node_count_test.py +++ /dev/null @@ -1,39 +0,0 @@ -# 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.api_core.retry import Retry -import pytest - -import vision_object_detection_deploy_model_node_count - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "0000000000000000000000" - - -@Retry() -@pytest.mark.slow -def test_object_detection_deploy_model_with_node_count(capsys): - # As model deployment can take a long time, instead try to deploy a - # nonexistent model and confirm that the model was not found, but other - # elements of the request were valid. - try: - vision_object_detection_deploy_model_node_count.deploy_model( - PROJECT_ID, MODEL_ID - ) - out, _ = capsys.readouterr() - assert "The model does not exist" in out - except Exception as e: - assert "The model does not exist" in e.message diff --git a/automl/snippets/vision_object_detection_predict.py b/automl/snippets/vision_object_detection_predict.py deleted file mode 100644 index 179315a0eb6..00000000000 --- a/automl/snippets/vision_object_detection_predict.py +++ /dev/null @@ -1,54 +0,0 @@ -# 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 predict(project_id, model_id, file_path): - """Predict.""" - # [START automl_vision_object_detection_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # file_path = "path_to_local_file.jpg" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) - - # Read the file. - with open(file_path, "rb") as content_file: - content = content_file.read() - - image = automl.Image(image_bytes=content) - payload = automl.ExamplePayload(image=image) - - # params is additional domain-specific parameters. - # score_threshold is used to filter the result - # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest - params = {"score_threshold": "0.8"} - - request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) - - response = prediction_client.predict(request=request) - print("Prediction results:") - for result in response.payload: - print(f"Predicted class name: {result.display_name}") - print(f"Predicted class score: {result.image_object_detection.score}") - bounding_box = result.image_object_detection.bounding_box - print("Normalized Vertices:") - for vertex in bounding_box.normalized_vertices: - print(f"\tX: {vertex.x}, Y: {vertex.y}") - # [END automl_vision_object_detection_predict] diff --git a/automl/snippets/vision_object_detection_predict_test.py b/automl/snippets/vision_object_detection_predict_test.py deleted file mode 100644 index 6ba9af363a5..00000000000 --- a/automl/snippets/vision_object_detection_predict_test.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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 backoff -from google.api_core.exceptions import DeadlineExceeded -from google.cloud import automl -import pytest - -import vision_object_detection_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["OBJECT_DETECTION_MODEL_ID"] - - -@pytest.fixture(scope="function") -def verify_model_state(): - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - - model = client.get_model(name=model_full_id) - if model.deployment_state == automl.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(name=model_full_id) - response.result(600) # 10 minutes - - -def test_vision_object_detection_predict(capsys, verify_model_state): - file_path = "resources/salad.jpg" - - # Retry the sample upon DeadlineExceeded, with a hard deadline of 5 mins. - @backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=300) - def run_sample(): - vision_object_detection_predict.predict(PROJECT_ID, MODEL_ID, file_path) - - run_sample() - out, _ = capsys.readouterr() - assert "Predicted class name:" in out diff --git a/datalabeling/AUTHORING_GUIDE.md b/datalabeling/AUTHORING_GUIDE.md deleted file mode 100644 index f2725aaf3f4..00000000000 --- a/datalabeling/AUTHORING_GUIDE.md +++ /dev/null @@ -1 +0,0 @@ -See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md diff --git a/datalabeling/CONTRIBUTING.md b/datalabeling/CONTRIBUTING.md deleted file mode 100644 index 354af3073ee..00000000000 --- a/datalabeling/CONTRIBUTING.md +++ /dev/null @@ -1 +0,0 @@ -See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md diff --git a/datalabeling/snippets/README.rst b/datalabeling/snippets/README.rst deleted file mode 100644 index bf5949b8cb7..00000000000 --- a/datalabeling/snippets/README.rst +++ /dev/null @@ -1,78 +0,0 @@ -.. This file is automatically generated. Do not edit this file directly. - -Google Cloud Data Labeling Service Python Samples -=============================================================================== - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=datalabeling/README.rst - - -This directory contains samples for Google Cloud Data Labeling Service. `Google Cloud Data Labeling Service`_ allows developers to request having human labelers label a collection of data that you plan to use to train a custom machine learning model. - - - - -.. _Google Cloud Data Labeling Service: https://cloud.google.com/data-labeling/docs/ - -Setup -------------------------------------------------------------------------------- - - -Authentication -++++++++++++++ - -This sample requires you to have authentication setup. Refer to the -`Authentication Getting Started Guide`_ for instructions on setting up -credentials for applications. - -.. _Authentication Getting Started Guide: - https://cloud.google.com/docs/authentication/getting-started - -Install Dependencies -++++++++++++++++++++ - -#. Clone python-docs-samples and change directory to the sample directory you want to use. - - .. code-block:: bash - - $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git - -#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. - - .. _Python Development Environment Setup Guide: - https://cloud.google.com/python/setup - -#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. - - .. code-block:: bash - - $ virtualenv env - $ source env/bin/activate - -#. Install the dependencies needed to run the samples. - - .. code-block:: bash - - $ pip install -r requirements.txt - -.. _pip: https://pip.pypa.io/ -.. _virtualenv: https://virtualenv.pypa.io/ - - - -The client library -------------------------------------------------------------------------------- - -This sample uses the `Google Cloud Client Library for Python`_. -You can read the documentation for more details on API usage and use GitHub -to `browse the source`_ and `report issues`_. - -.. _Google Cloud Client Library for Python: - https://googlecloudplatform.github.io/google-cloud-python/ -.. _browse the source: - https://github.com/GoogleCloudPlatform/google-cloud-python -.. _report issues: - https://github.com/GoogleCloudPlatform/google-cloud-python/issues - - -.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/datalabeling/snippets/README.rst.in b/datalabeling/snippets/README.rst.in deleted file mode 100644 index c87a1ff89b4..00000000000 --- a/datalabeling/snippets/README.rst.in +++ /dev/null @@ -1,18 +0,0 @@ -# This file is used to generate README.rst - -product: - name: Google Cloud Data Labeling Service - short_name: Cloud Data Labeling - url: https://cloud.google.com/data-labeling/docs/ - description: > - `Google Cloud Data Labeling Service`_ allows developers to request having - human labelers label a collection of data that you plan to use to train a - custom machine learning model. - -setup: -- auth -- install_deps - -cloud_client_library: true - -folder: datalabeling \ No newline at end of file diff --git a/datalabeling/snippets/create_annotation_spec_set.py b/datalabeling/snippets/create_annotation_spec_set.py deleted file mode 100644 index b8bb842646c..00000000000 --- a/datalabeling/snippets/create_annotation_spec_set.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_create_annotation_spec_set_beta] -def create_annotation_spec_set(project_id): - """Creates a data labeling annotation spec set for the given - Google Cloud project. - """ - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_create_annotation_spec_set_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_create_annotation_spec_set_beta] - - project_path = f"projects/{project_id}" - - annotation_spec_1 = datalabeling.AnnotationSpec( - display_name="label_1", description="label_description_1" - ) - - annotation_spec_2 = datalabeling.AnnotationSpec( - display_name="label_2", description="label_description_2" - ) - - annotation_spec_set = datalabeling.AnnotationSpecSet( - display_name="YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME", - description="YOUR_DESCRIPTION", - annotation_specs=[annotation_spec_1, annotation_spec_2], - ) - - response = client.create_annotation_spec_set( - request={"parent": project_path, "annotation_spec_set": annotation_spec_set} - ) - - # The format of the resource name: - # project_id/{project_id}/annotationSpecSets/{annotationSpecSets_id} - print(f"The annotation_spec_set resource name: {response.name}") - print(f"Display name: {response.display_name}") - print(f"Description: {response.description}") - print("Annotation specs:") - for annotation_spec in response.annotation_specs: - print(f"\tDisplay name: {annotation_spec.display_name}") - print(f"\tDescription: {annotation_spec.description}\n") - - return response - - -# [END datalabeling_create_annotation_spec_set_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument("--project-id", help="Project ID. Required.", required=True) - - args = parser.parse_args() - - create_annotation_spec_set(args.project_id) diff --git a/datalabeling/snippets/create_annotation_spec_set_test.py b/datalabeling/snippets/create_annotation_spec_set_test.py deleted file mode 100644 index 2653169009e..00000000000 --- a/datalabeling/snippets/create_annotation_spec_set_test.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import ServerError -import pytest - -import create_annotation_spec_set -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -@pytest.fixture(scope="module") -def cleaner(): - resource_names = [] - - yield resource_names - - for resource_name in resource_names: - testing_lib.delete_annotation_spec_set(resource_name) - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_create_annotation_spec_set(cleaner, capsys): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - return create_annotation_spec_set.create_annotation_spec_set(PROJECT_ID) - - response = run_sample() - - # For cleanup - cleaner.append(response.name) - - out, _ = capsys.readouterr() - assert "The annotation_spec_set resource name:" in out diff --git a/datalabeling/snippets/create_instruction.py b/datalabeling/snippets/create_instruction.py deleted file mode 100644 index 600e7ad4be5..00000000000 --- a/datalabeling/snippets/create_instruction.py +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_create_instruction_beta] -def create_instruction(project_id, data_type, instruction_gcs_uri): - """Creates a data labeling PDF instruction for the given Google Cloud - project. The PDF file should be uploaded to the project in - Google Cloud Storage. - """ - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_create_instruction_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_create_instruction_beta] - - project_path = f"projects/{project_id}" - - pdf_instruction = datalabeling.PdfInstruction(gcs_file_uri=instruction_gcs_uri) - - instruction = datalabeling.Instruction( - display_name="YOUR_INSTRUCTION_DISPLAY_NAME", - description="YOUR_DESCRIPTION", - data_type=data_type, - pdf_instruction=pdf_instruction, - ) - - operation = client.create_instruction( - request={"parent": project_path, "instruction": instruction} - ) - - result = operation.result() - - # The format of the resource name: - # project_id/{project_id}/instruction/{instruction_id} - print(f"The instruction resource name: {result.name}") - print(f"Display name: {result.display_name}") - print(f"Description: {result.description}") - print("Create time:") - print(f"\tseconds: {result.create_time.timestamp_pb().seconds}") - print(f"\tnanos: {result.create_time.timestamp_pb().nanos}") - print(f"Data type: {datalabeling.DataType(result.data_type).name}") - print("Pdf instruction:") - print(f"\tGcs file uri: {result.pdf_instruction.gcs_file_uri}\n") - - return result - - -# [END datalabeling_create_instruction_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument("--project-id", help="Project ID. Required.", required=True) - - parser.add_argument( - "--data-type", - help="Data type. Only support IMAGE, VIDEO, TEXT and AUDIO. Required.", - required=True, - ) - - parser.add_argument( - "--instruction-gcs-uri", - help="The URI of Google Cloud Storage of the instruction. Required.", - required=True, - ) - - args = parser.parse_args() - - create_instruction(args.project_id, args.data_type, args.instruction_gcs_uri) diff --git a/datalabeling/snippets/create_instruction_test.py b/datalabeling/snippets/create_instruction_test.py deleted file mode 100644 index 0ee09b34fdd..00000000000 --- a/datalabeling/snippets/create_instruction_test.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import ServerError -from google.cloud import datalabeling -import pytest - -import create_instruction -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -INSTRUCTION_GCS_URI = "gs://cloud-samples-data/datalabeling" "/instruction/test.pdf" - - -@pytest.fixture(scope="module") -def cleaner(): - resource_names = [] - - yield resource_names - - for resource_name in resource_names: - testing_lib.delete_instruction(resource_name) - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_create_instruction(cleaner, capsys): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - return create_instruction.create_instruction( - PROJECT_ID, datalabeling.DataType.IMAGE, INSTRUCTION_GCS_URI - ) - - instruction = run_sample() - cleaner.append(instruction.name) - - out, _ = capsys.readouterr() - assert "The instruction resource name: " in out diff --git a/datalabeling/snippets/export_data.py b/datalabeling/snippets/export_data.py deleted file mode 100644 index ae6f9fd11d9..00000000000 --- a/datalabeling/snippets/export_data.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_export_data_beta] -def export_data(dataset_resource_name, annotated_dataset_resource_name, export_gcs_uri): - """Exports a dataset from the given Google Cloud project.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_export_data_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_export_data_beta] - - gcs_destination = datalabeling.GcsDestination( - output_uri=export_gcs_uri, mime_type="text/csv" - ) - - output_config = datalabeling.OutputConfig(gcs_destination=gcs_destination) - - response = client.export_data( - request={ - "name": dataset_resource_name, - "annotated_dataset": annotated_dataset_resource_name, - "output_config": output_config, - } - ) - - print(f"Dataset ID: {response.result().dataset}\n") - print("Output config:") - print("\tGcs destination:") - print( - "\t\tOutput URI: {}\n".format( - response.result().output_config.gcs_destination.output_uri - ) - ) - - -# [END datalabeling_export_data_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument( - "--dataset-resource-name", - help="Dataset resource name. Required.", - required=True, - ) - - parser.add_argument( - "--annotated-dataset-resource-name", - help="Annotated Dataset resource name. Required.", - required=True, - ) - - parser.add_argument( - "--export-gcs-uri", help="The export GCS URI. Required.", required=True - ) - - args = parser.parse_args() - - export_data( - args.dataset_resource_name, - args.annotated_dataset_resource_name, - args.export_gcs_uri, - ) diff --git a/datalabeling/snippets/import_data.py b/datalabeling/snippets/import_data.py deleted file mode 100644 index f9c4d59803b..00000000000 --- a/datalabeling/snippets/import_data.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_import_data_beta] -def import_data(dataset_resource_name, data_type, input_gcs_uri): - """Imports data to the given Google Cloud project and dataset.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_import_data_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_import_data_beta] - - gcs_source = datalabeling.GcsSource(input_uri=input_gcs_uri, mime_type="text/csv") - - csv_input_config = datalabeling.InputConfig( - data_type=data_type, gcs_source=gcs_source - ) - - response = client.import_data( - request={"name": dataset_resource_name, "input_config": csv_input_config} - ) - - result = response.result() - - # The format of resource name: - # project_id/{project_id}/datasets/{dataset_id} - print(f"Dataset resource name: {result.dataset}\n") - - return result - - -# [END datalabeling_import_data_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument( - "--dataset-resource-name", - help="Dataset resource name. Required.", - required=True, - ) - - parser.add_argument( - "--data-type", - help="Data type. Only support IMAGE, VIDEO, TEXT and AUDIO. Required.", - required=True, - ) - - parser.add_argument( - "--input-gcs-uri", - help="The GCS URI of the input dataset. Required.", - required=True, - ) - - args = parser.parse_args() - - import_data(args.dataset_resource_name, args.data_type, args.input_gcs_uri) diff --git a/datalabeling/snippets/import_data_test.py b/datalabeling/snippets/import_data_test.py deleted file mode 100644 index 8d836515960..00000000000 --- a/datalabeling/snippets/import_data_test.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import ServerError -from google.cloud import datalabeling -import pytest - -import import_data -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -INPUT_GCS_URI = "gs://cloud-samples-data/datalabeling/image/image_dataset.csv" - - -@pytest.fixture(scope="module") -def dataset(): - # create a temporary dataset - dataset = testing_lib.create_dataset(PROJECT_ID) - - yield dataset - - # tear down - testing_lib.delete_dataset(dataset.name) - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_import_data(capsys, dataset): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - import_data.import_data( - dataset.name, datalabeling.DataType.IMAGE, INPUT_GCS_URI - ) - - run_sample() - out, _ = capsys.readouterr() - assert "Dataset resource name: " in out diff --git a/datalabeling/snippets/label_image.py b/datalabeling/snippets/label_image.py deleted file mode 100644 index e12e28e0084..00000000000 --- a/datalabeling/snippets/label_image.py +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_label_image_beta] -def label_image( - dataset_resource_name, instruction_resource_name, annotation_spec_set_resource_name -): - """Labels an image dataset.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_label_image_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_label_image_beta] - - basic_config = datalabeling.HumanAnnotationConfig( - instruction=instruction_resource_name, - annotated_dataset_display_name="YOUR_ANNOTATED_DATASET_DISPLAY_NAME", - label_group="YOUR_LABEL_GROUP", - replica_count=1, - ) - - feature = datalabeling.LabelImageRequest.Feature.CLASSIFICATION - - # annotation_spec_set_resource_name needs to be created beforehand. - # See the examples in the following: - # https://cloud.google.com/ai-platform/data-labeling/docs/label-sets - config = datalabeling.ImageClassificationConfig( - annotation_spec_set=annotation_spec_set_resource_name, - allow_multi_label=False, - answer_aggregation_type=datalabeling.StringAggregationType.MAJORITY_VOTE, - ) - - response = client.label_image( - request={ - "parent": dataset_resource_name, - "basic_config": basic_config, - "feature": feature, - "image_classification_config": config, - } - ) - - print(f"Label_image operation name: {response.operation.name}") - return response - - -# [END datalabeling_label_image_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument( - "--dataset-resource-name", - help="Dataset resource name. Required.", - required=True, - ) - - parser.add_argument( - "--instruction-resource-name", - help="Instruction resource name. Required.", - required=True, - ) - - parser.add_argument( - "--annotation-spec-set-resource-name", - help="Annotation spec set resource name. Required.", - required=True, - ) - - args = parser.parse_args() - - label_image( - args.dataset_resource_name, - args.instruction_resource_name, - args.annotation_spec_set_resource_name, - ) diff --git a/datalabeling/snippets/label_image_test.py b/datalabeling/snippets/label_image_test.py deleted file mode 100644 index 636b65c2b83..00000000000 --- a/datalabeling/snippets/label_image_test.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import ServerError -from google.cloud import datalabeling -import pytest - -import label_image -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -INPUT_GCS_URI = "gs://cloud-samples-data/datalabeling/image/image_dataset.csv" -INSTRUCTION_GCS_URI = "gs://cloud-samples-data/datalabeling" "/instruction/test.pdf" - - -@pytest.fixture(scope="module") -def dataset(): - # create a temporary dataset - dataset = testing_lib.create_dataset(PROJECT_ID) - - testing_lib.import_data(dataset.name, datalabeling.DataType.IMAGE, INPUT_GCS_URI) - yield dataset - - # tear down - testing_lib.delete_dataset(dataset.name) - - -@pytest.fixture(scope="module") -def annotation_spec_set(): - # create a temporary annotation_spec_set - response = testing_lib.create_annotation_spec_set(PROJECT_ID) - - yield response - - testing_lib.delete_annotation_spec_set(response.name) - - -@pytest.fixture(scope="module") -def instruction(): - # create a temporary instruction - instruction = testing_lib.create_instruction( - PROJECT_ID, datalabeling.DataType.IMAGE, INSTRUCTION_GCS_URI - ) - - yield instruction - - # tear down - testing_lib.delete_instruction(instruction.name) - - -@pytest.fixture(scope="module") -def cleaner(): - resource_names = [] - - yield resource_names - - for resource_name in resource_names: - testing_lib.cancel_operation(resource_name) - - -# Passing in dataset as the last argument in test_label_image since it needs -# to be deleted before the annotation_spec_set can be deleted. -@pytest.mark.skip(reason="currently unavailable") -def test_label_image(capsys, annotation_spec_set, instruction, dataset, cleaner): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - # Start labeling. - return label_image.label_image( - dataset.name, instruction.name, annotation_spec_set.name - ) - - response = run_sample() - cleaner.append(response.operation.name) - - out, _ = capsys.readouterr() - assert "Label_image operation name: " in out diff --git a/datalabeling/snippets/label_text.py b/datalabeling/snippets/label_text.py deleted file mode 100644 index a0b4913ed76..00000000000 --- a/datalabeling/snippets/label_text.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_label_text_beta] -def label_text( - dataset_resource_name, instruction_resource_name, annotation_spec_set_resource_name -): - """Labels a text dataset.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_label_text_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_label_text_beta] - - basic_config = datalabeling.HumanAnnotationConfig( - instruction=instruction_resource_name, - annotated_dataset_display_name="YOUR_ANNOTATED_DATASET_DISPLAY_NAME", - label_group="YOUR_LABEL_GROUP", - replica_count=1, - ) - - feature = datalabeling.LabelTextRequest.Feature.TEXT_ENTITY_EXTRACTION - - config = datalabeling.TextEntityExtractionConfig( - annotation_spec_set=annotation_spec_set_resource_name - ) - - response = client.label_text( - request={ - "parent": dataset_resource_name, - "basic_config": basic_config, - "feature": feature, - "text_classification_config": config, - } - ) - - print(f"Label_text operation name: {response.operation.name}") - return response - - -# [END datalabeling_label_text_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument( - "--dataset-resource-name", - help="Dataset resource name. Required.", - required=True, - ) - - parser.add_argument( - "--instruction-resource-name", - help="Instruction resource name. Required.", - required=True, - ) - - parser.add_argument( - "--annotation-spec-set-resource-name", - help="Annotation spec set resource name. Required.", - required=True, - ) - - args = parser.parse_args() - - label_text( - args.dataset_resource_name, - args.instruction_resource_name, - args.annotation_spec_set_resource_name, - ) diff --git a/datalabeling/snippets/label_text_test.py b/datalabeling/snippets/label_text_test.py deleted file mode 100644 index b91f086e180..00000000000 --- a/datalabeling/snippets/label_text_test.py +++ /dev/null @@ -1,99 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import ServerError -from google.cloud import datalabeling -import pytest - -import label_text -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -INPUT_GCS_URI = "gs://cloud-samples-data/datalabeling/text/input.csv" -INSTRUCTION_GCS_URI = "gs://cloud-samples-data/datalabeling" "/instruction/test.pdf" - - -@pytest.fixture(scope="module") -def dataset(): - # create a temporary dataset - dataset = testing_lib.create_dataset(PROJECT_ID) - - testing_lib.import_data(dataset.name, "TEXT", INPUT_GCS_URI) - - yield dataset - - # tear down - testing_lib.delete_dataset(dataset.name) - - -@pytest.fixture(scope="module") -def annotation_spec_set(): - # create a temporary annotation_spec_set - response = testing_lib.create_annotation_spec_set(PROJECT_ID) - - yield response - - testing_lib.delete_annotation_spec_set(response.name) - - -@pytest.fixture(scope="module") -def instruction(): - # create a temporary instruction - instruction = testing_lib.create_instruction( - PROJECT_ID, datalabeling.DataType.IMAGE, INSTRUCTION_GCS_URI - ) - - yield instruction - - # tear down - testing_lib.delete_instruction(instruction.name) - - -@pytest.fixture(scope="module") -def cleaner(): - resource_names = [] - - yield resource_names - - for resource_name in resource_names: - testing_lib.cancel_operation(resource_name) - - -# Passing in dataset as the last argument in test_label_image since it needs -# to be deleted before the annotation_spec_set can be deleted. -@pytest.mark.skip("Constantly failing") -def test_label_text(capsys, annotation_spec_set, instruction, dataset, cleaner): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - # Start labeling. - return label_text.label_text( - dataset.name, instruction.name, annotation_spec_set.name - ) - - response = run_sample() - cleaner.append(response.operation.name) - - out, _ = capsys.readouterr() - assert "Label_text operation name: " in out - - # Cancels the labeling operation. - response.cancel() - assert response.cancelled() is True diff --git a/datalabeling/snippets/label_video.py b/datalabeling/snippets/label_video.py deleted file mode 100644 index 487bdf664de..00000000000 --- a/datalabeling/snippets/label_video.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_label_video_beta] -def label_video( - dataset_resource_name, instruction_resource_name, annotation_spec_set_resource_name -): - """Labels a video dataset.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_label_video_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_label_video_beta] - - basic_config = datalabeling.HumanAnnotationConfig( - instruction=instruction_resource_name, - annotated_dataset_display_name="YOUR_ANNOTATED_DATASET_DISPLAY_NAME", - label_group="YOUR_LABEL_GROUP", - replica_count=1, - ) - - feature = datalabeling.LabelVideoRequest.Feature.OBJECT_TRACKING - - config = datalabeling.ObjectTrackingConfig( - annotation_spec_set=annotation_spec_set_resource_name - ) - - response = client.label_video( - request={ - "parent": dataset_resource_name, - "basic_config": basic_config, - "feature": feature, - "object_tracking_config": config, - } - ) - - print(f"Label_video operation name: {response.operation.name}") - return response - - -# [END datalabeling_label_video_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - parser.add_argument( - "--dataset-resource-name", - help="Dataset resource name. Required.", - required=True, - ) - - parser.add_argument( - "--instruction-resource-name", - help="Instruction resource name. Required.", - required=True, - ) - - parser.add_argument( - "--annotation-spec-set-resource-name", - help="Annotation spec set resource name. Required.", - required=True, - ) - - args = parser.parse_args() - - label_video( - args.dataset_resource_name, - args.instruction_resource_name, - args.annotation_spec_set_resource_name, - ) diff --git a/datalabeling/snippets/label_video_test.py b/datalabeling/snippets/label_video_test.py deleted file mode 100644 index eefcce68144..00000000000 --- a/datalabeling/snippets/label_video_test.py +++ /dev/null @@ -1,99 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import ServerError -from google.cloud import datalabeling -import pytest - -import label_video -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -INPUT_GCS_URI = "gs://cloud-samples-data/datalabeling/videos/video_dataset.csv" -INSTRUCTION_GCS_URI = "gs://cloud-samples-data/datalabeling" "/instruction/test.pdf" - - -@pytest.fixture(scope="module") -def dataset(): - # create a temporary dataset - dataset = testing_lib.create_dataset(PROJECT_ID) - - testing_lib.import_data(dataset.name, datalabeling.DataType.VIDEO, INPUT_GCS_URI) - - yield dataset - - # tear down - testing_lib.delete_dataset(dataset.name) - - -@pytest.fixture(scope="module") -def annotation_spec_set(): - # create a temporary annotation_spec_set - response = testing_lib.create_annotation_spec_set(PROJECT_ID) - - yield response - - testing_lib.delete_annotation_spec_set(response.name) - - -@pytest.fixture(scope="module") -def instruction(): - # create a temporary instruction - instruction = testing_lib.create_instruction( - PROJECT_ID, datalabeling.DataType.VIDEO, INSTRUCTION_GCS_URI - ) - - yield instruction - - # tear down - testing_lib.delete_instruction(instruction.name) - - -@pytest.fixture(scope="module") -def cleaner(): - resource_names = [] - - yield resource_names - - for resource_name in resource_names: - testing_lib.cancel_operation(resource_name) - - -# Passing in dataset as the last argument in test_label_image since it needs -# to be deleted before the annotation_spec_set can be deleted. -@pytest.mark.skip(reason="currently unavailable") -def test_label_video(capsys, annotation_spec_set, instruction, dataset, cleaner): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - # Start labeling. - return label_video.label_video( - dataset.name, instruction.name, annotation_spec_set.name - ) - - response = run_sample() - cleaner.append(response.operation.name) - - out, _ = capsys.readouterr() - assert "Label_video operation name: " in out - - # Cancels the labeling operation. - response.cancel() - assert response.cancelled() is True diff --git a/datalabeling/snippets/manage_dataset.py b/datalabeling/snippets/manage_dataset.py deleted file mode 100644 index 357017ec86a..00000000000 --- a/datalabeling/snippets/manage_dataset.py +++ /dev/null @@ -1,186 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 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 argparse -import os - -from google.api_core.client_options import ClientOptions - - -# [START datalabeling_create_dataset_beta] -def create_dataset(project_id): - """Creates a dataset for the given Google Cloud project.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_create_dataset_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_create_dataset_beta] - - formatted_project_name = f"projects/{project_id}" - - dataset = datalabeling.Dataset( - display_name="YOUR_DATASET_SET_DISPLAY_NAME", description="YOUR_DESCRIPTION" - ) - - response = client.create_dataset( - request={"parent": formatted_project_name, "dataset": dataset} - ) - - # The format of resource name: - # project_id/{project_id}/datasets/{dataset_id} - print(f"The dataset resource name: {response.name}") - print(f"Display name: {response.display_name}") - print(f"Description: {response.description}") - print("Create time:") - print(f"\tseconds: {response.create_time.timestamp_pb().seconds}") - print(f"\tnanos: {response.create_time.timestamp_pb().nanos}\n") - - return response - - -# [END datalabeling_create_dataset_beta] - - -# [START datalabeling_list_datasets_beta] -def list_datasets(project_id): - """Lists datasets for the given Google Cloud project.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_list_datasets_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_list_datasets_beta] - - formatted_project_name = f"projects/{project_id}" - - response = client.list_datasets(request={"parent": formatted_project_name}) - for element in response: - # The format of resource name: - # project_id/{project_id}/datasets/{dataset_id} - print(f"The dataset resource name: {element.name}\n") - print(f"Display name: {element.display_name}") - print(f"Description: {element.description}") - print("Create time:") - print(f"\tseconds: {element.create_time.timestamp_pb().seconds}") - print(f"\tnanos: {element.create_time.timestamp_pb().nanos}") - - -# [END datalabeling_list_datasets_beta] - - -# [START datalabeling_get_dataset_beta] -def get_dataset(dataset_resource_name): - """Gets a dataset for the given Google Cloud project.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_get_dataset_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_get_dataset_beta] - - response = client.get_dataset(request={"name": dataset_resource_name}) - - print(f"The dataset resource name: {response.name}\n") - print(f"Display name: {response.display_name}") - print(f"Description: {response.description}") - print("Create time:") - print(f"\tseconds: {response.create_time.timestamp_pb().seconds}") - print(f"\tnanos: {response.create_time.timestamp_pb().nanos}") - - -# [END datalabeling_get_dataset_beta] - - -# [START datalabeling_delete_dataset_beta] -def delete_dataset(dataset_resource_name): - """Deletes a dataset for the given Google Cloud project.""" - from google.cloud import datalabeling_v1beta1 as datalabeling - - client = datalabeling.DataLabelingServiceClient() - # [END datalabeling_delete_dataset_beta] - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - # [START datalabeling_delete_dataset_beta] - - response = client.delete_dataset(request={"name": dataset_resource_name}) - - print(f"Dataset deleted. {response}\n") - - -# [END datalabeling_delete_dataset_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - subparsers = parser.add_subparsers(dest="command") - - create_parser = subparsers.add_parser("create", help="Create a new dataset.") - create_parser.add_argument( - "--project-id", help="Project ID. Required.", required=True - ) - - list_parser = subparsers.add_parser("list", help="List all datasets.") - list_parser.add_argument( - "--project-id", help="Project ID. Required.", required=True - ) - - get_parser = subparsers.add_parser( - "get", help="Get a dataset by the dataset resource name." - ) - get_parser.add_argument( - "--dataset-resource-name", - help="The dataset resource name. Used in the get or delete operation.", - required=True, - ) - - delete_parser = subparsers.add_parser( - "delete", help="Delete a dataset by the dataset resource name." - ) - delete_parser.add_argument( - "--dataset-resource-name", - help="The dataset resource name. Used in the get or delete operation.", - required=True, - ) - - args = parser.parse_args() - - if args.command == "create": - create_dataset(args.project_id) - elif args.command == "list": - list_datasets(args.project_id) - elif args.command == "get": - get_dataset(args.dataset_resource_name) - elif args.command == "delete": - delete_dataset(args.dataset_resource_name) diff --git a/datalabeling/snippets/manage_dataset_test.py b/datalabeling/snippets/manage_dataset_test.py deleted file mode 100644 index 43e6589f111..00000000000 --- a/datalabeling/snippets/manage_dataset_test.py +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2022 Google, Inc -# -# 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 backoff -from google.api_core.exceptions import RetryError, ServerError -import pytest - -import manage_dataset -import testing_lib - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -@pytest.mark.skip(reason="service is limited due to covid") -@pytest.fixture(scope="module") -def dataset(): - # create a temporary dataset - dataset = testing_lib.create_dataset(PROJECT_ID) - - yield dataset - - # tear down - testing_lib.delete_dataset(dataset.name) - - -@pytest.fixture(scope="module") -def cleaner(): - # First delete old datasets. - try: - testing_lib.delete_old_datasets(PROJECT_ID) - # We see occational RetryError while deleting old datasets. - # We can just ignore it and move on. - except RetryError as e: - print(f"delete_old_datasets failed: detail {e}") - - resource_names = [] - - yield resource_names - - for resource_name in resource_names: - testing_lib.delete_dataset(resource_name) - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_create_dataset(cleaner, capsys): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - return manage_dataset.create_dataset(PROJECT_ID) - - response = run_sample() - cleaner.append(response.name) - - out, _ = capsys.readouterr() - assert "The dataset resource name:" in out - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_list_dataset(capsys, dataset): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - manage_dataset.list_datasets(PROJECT_ID) - - run_sample() - out, _ = capsys.readouterr() - assert dataset.name in out - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_get_dataset(capsys, dataset): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - manage_dataset.get_dataset(dataset.name) - - run_sample() - out, _ = capsys.readouterr() - assert "The dataset resource name:" in out - - -@pytest.mark.skip(reason="service is limited due to covid") -def test_delete_dataset(capsys, dataset): - @backoff.on_exception( - backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE - ) - def run_sample(): - manage_dataset.delete_dataset(dataset.name) - - run_sample() - out, _ = capsys.readouterr() - assert "Dataset deleted." in out diff --git a/datalabeling/snippets/noxfile_config.py b/datalabeling/snippets/noxfile_config.py deleted file mode 100644 index af568dc67ca..00000000000 --- a/datalabeling/snippets/noxfile_config.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright 2022 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. - -# Default TEST_CONFIG_OVERRIDE for python repos. - -# You can copy this file into your directory, then it will be inported from -# the noxfile.py. - -# The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py - -TEST_CONFIG_OVERRIDE = { - # You can opt out from the test for specific Python versions. - "ignored_versions": ["3.8", "3.9", "3.11", "3.12", "3.13"], - # Declare optional test sessions you want to opt-in. Currently we - # have the following optional test sessions: - # 'cloud_run' # Test session for Cloud Run application. - "opt_in_sessions": [], - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - # 'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT', - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - "envs": { - # For Datalabeling samples to hit the testing endpoint - "DATALABELING_ENDPOINT": "test-datalabeling.sandbox.googleapis.com:443" - }, -} diff --git a/datalabeling/snippets/requirements-test.txt b/datalabeling/snippets/requirements-test.txt deleted file mode 100644 index 79932f83530..00000000000 --- a/datalabeling/snippets/requirements-test.txt +++ /dev/null @@ -1,2 +0,0 @@ -backoff==2.2.1 -pytest==9.0.3; python_version >= "3.10" diff --git a/datalabeling/snippets/requirements.txt b/datalabeling/snippets/requirements.txt deleted file mode 100644 index 2927b021a52..00000000000 --- a/datalabeling/snippets/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-datalabeling==1.11.1 diff --git a/datalabeling/snippets/testing_lib.py b/datalabeling/snippets/testing_lib.py deleted file mode 100644 index 267244d3df7..00000000000 --- a/datalabeling/snippets/testing_lib.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright 2022 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 time - -import backoff -from google.api_core.client_options import ClientOptions -from google.api_core.exceptions import DeadlineExceeded, FailedPrecondition -from google.cloud import datalabeling_v1beta1 as datalabeling - -import create_annotation_spec_set as annotation_spec_set_sample -import create_instruction as instruction_sample -import import_data as import_sample -import manage_dataset as dataset_sample - -RETRY_DEADLINE = 60 - - -def create_client(): - # If provided, use a provided test endpoint - this will prevent tests on - # this snippet from triggering any action by a real human - if "DATALABELING_ENDPOINT" in os.environ: - opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT")) - client = datalabeling.DataLabelingServiceClient(client_options=opts) - else: - client = datalabeling.DataLabelingServiceClient() - return client - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def create_dataset(project_id): - return dataset_sample.create_dataset(project_id) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def delete_dataset(name): - return dataset_sample.delete_dataset(name) - - -def delete_old_datasets(project_id): - client = create_client() - formatted_project_name = f"projects/{project_id}" - - response = client.list_datasets(request={"parent": formatted_project_name}) - # It will delete datasets created more than 2 hours ago - cutoff_time = time.time() - 7200 - for element in response: - if element.create_time.timestamp_pb().seconds < cutoff_time: - print(f"Deleting {element.name}") - try: - dataset_sample.delete_dataset(element.name) - except FailedPrecondition as e: - # We're always getting FailedPrecondition with 400 - # resource conflict. I don't know why. - print(f"Deleting {element.name} failed.") - print(f"Detail: {e}") - # To avoid quota error - time.sleep(1) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def create_annotation_spec_set(project_id): - return annotation_spec_set_sample.create_annotation_spec_set(project_id) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def delete_annotation_spec_set(name): - client = create_client() - client.delete_annotation_spec_set(request={"name": name}) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def create_instruction(project_id, data_type, gcs_uri): - return instruction_sample.create_instruction(project_id, data_type, gcs_uri) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def delete_instruction(name): - client = create_client() - client.delete_instruction(request={"name": name}) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def cancel_operation(name): - client = create_client() - client._transport.operations_client.cancel_operation(name) - - -@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE) -def import_data(dataset_name, data_type, gcs_uri): - import_sample.import_data(dataset_name, data_type, gcs_uri) diff --git a/iot/README.md b/iot/README.md deleted file mode 100644 index 409b19aaf0b..00000000000 --- a/iot/README.md +++ /dev/null @@ -1 +0,0 @@ -Cloud IoT Core was retired on August 16, 2023. After August 15, 2023, the documentation for IoT Core is no longer be available. See https://cloud.google.com/iot-core for a host of partner-led solutions, built on Google Cloud, that meet the needs of IoT customers. diff --git a/tables/README.rst b/tables/README.rst deleted file mode 100644 index 9de68506aa2..00000000000 --- a/tables/README.rst +++ /dev/null @@ -1,3 +0,0 @@ -These samples have been moved. - -https://github.com/googleapis/python-automl/tree/main/samples