diff --git a/automl/README.md b/automl/README.md new file mode 100644 index 00000000000..69cf0939fad --- /dev/null +++ b/automl/README.md @@ -0,0 +1 @@ +These samples have been moved to https://github.com/googleapis/python-automl/tree/master/samples/ \ No newline at end of file diff --git a/automl/beta/batch_predict.py b/automl/beta/batch_predict.py deleted file mode 100644 index 911eec733cb..00000000000 --- a/automl/beta/batch_predict.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. - - -# [START automl_batch_predict_beta] -from google.cloud import automl_v1beta1 as automl - - -def batch_predict( - 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/", -): - """Batch predict""" - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = prediction_client.model_path( - project_id, "us-central1", model_id - ) - - gcs_source = automl.types.GcsSource(input_uris=[input_uri]) - - input_config = automl.types.BatchPredictInputConfig(gcs_source=gcs_source) - gcs_destination = automl.types.GcsDestination(output_uri_prefix=output_uri) - output_config = automl.types.BatchPredictOutputConfig( - gcs_destination=gcs_destination - ) - - response = prediction_client.batch_predict( - model_full_id, input_config, output_config - ) - - print("Waiting for operation to complete...") - print( - "Batch Prediction results saved to Cloud Storage bucket. {}".format( - response.result() - ) - ) -# [END automl_batch_predict_beta] diff --git a/automl/beta/batch_predict_test.py b/automl/beta/batch_predict_test.py deleted file mode 100644 index 2869873a919..00000000000 --- a/automl/beta/batch_predict_test.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 ladnguage governing permissions and -# limitations under the License. - -import datetime -import os - -import batch_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) -MODEL_ID = "TEN0000000000000000000" -PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime( - "%Y%m%d%H%M%S" -) - - -def test_batch_predict(capsys): - # As batch prediction can take a long time. Try to batch predict on a model - # and confirm that the model was not found, but other elements of the - # request were valid. - try: - input_uri = "gs://{}/entity-extraction/input.jsonl".format(BUCKET_ID) - output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX) - batch_predict.batch_predict( - PROJECT_ID, MODEL_ID, input_uri, output_uri - ) - out, _ = capsys.readouterr() - assert ( - "does not exist" - in out - ) - except Exception as e: - assert ( - "does not exist" - in e.message - ) diff --git a/automl/beta/delete_dataset.py b/automl/beta/delete_dataset.py deleted file mode 100644 index 51647758738..00000000000 --- a/automl/beta/delete_dataset.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. - - -# [START automl_delete_dataset_beta] -from google.cloud import automl_v1beta1 as automl - - -def delete_dataset(project_id="YOUR_PROJECT_ID", dataset_id="YOUR_DATASET_ID"): - """Delete a dataset.""" - 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(dataset_full_id) - - print("Dataset deleted. {}".format(response.result())) -# [END automl_delete_dataset_beta] diff --git a/automl/beta/delete_dataset_test.py b/automl/beta/delete_dataset_test.py deleted file mode 100644 index 9781ad26066..00000000000 --- a/automl/beta/delete_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 os -import uuid - -from google.cloud import automl_v1beta1 as automl -import pytest - -import delete_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) - - -@pytest.fixture(scope="function") -def dataset_id(): - client = automl.AutoMlClient() - project_location = client.location_path(PROJECT_ID, "us-central1") - display_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] - metadata = automl.types.TextExtractionDatasetMetadata() - dataset = automl.types.Dataset( - display_name=display_name, text_extraction_dataset_metadata=metadata - ) - response = client.create_dataset(project_location, dataset) - dataset_id = response.name.split("/")[-1] - - yield dataset_id - - -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/beta/delete_model.py b/automl/beta/delete_model.py deleted file mode 100644 index 030a2900113..00000000000 --- a/automl/beta/delete_model.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_model(project_id, model_id): - """Delete a model.""" - # [START automl_delete_model_beta] - from google.cloud import automl_v1beta1 as 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(model_full_id) - - print("Model deleted. {}".format(response.result())) - # [END automl_delete_model_beta] diff --git a/automl/beta/delete_model_test.py b/automl/beta/delete_model_test.py deleted file mode 100644 index 1d3548f3d5e..00000000000 --- a/automl/beta/delete_model_test.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. - -import os - -import delete_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -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/beta/get_model.py b/automl/beta/get_model.py deleted file mode 100644 index 834dac0c4c3..00000000000 --- a/automl/beta/get_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 get_model(project_id, model_id): - """Get a model.""" - # [START automl_get_model_beta] - from google.cloud import automl_v1beta1 as 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(model_full_id) - - # Retrieve deployment state. - if model.deployment_state == automl.enums.Model.DeploymentState.DEPLOYED: - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - # Display the model information. - print("Model name: {}".format(model.name)) - print("Model id: {}".format(model.name.split("/")[-1])) - print("Model display name: {}".format(model.display_name)) - print("Model create time:") - print("\tseconds: {}".format(model.create_time.seconds)) - print("\tnanos: {}".format(model.create_time.nanos)) - print("Model deployment state: {}".format(deployment_state)) - # [END automl_get_model_beta] diff --git a/automl/beta/get_model_evaluation.py b/automl/beta/get_model_evaluation.py deleted file mode 100644 index ed540f2e1c3..00000000000 --- a/automl/beta/get_model_evaluation.py +++ /dev/null @@ -1,60 +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_model_evaluation(project_id, model_id, model_evaluation_id): - """Get model evaluation.""" - # [START automl_video_classification_get_model_evaluation_beta] - # [START automl_video_object_tracking_get_model_evaluation_beta] - from google.cloud import automl_v1beta1 as 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_evaluation_full_id = client.model_evaluation_path( - project_id, "us-central1", model_id, model_evaluation_id - ) - - # Get complete detail of the model evaluation. - response = client.get_model_evaluation(model_evaluation_full_id) - - print("Model evaluation name: {}".format(response.name)) - print("Model annotation spec id: {}".format(response.annotation_spec_id)) - print("Create Time:") - print("\tseconds: {}".format(response.create_time.seconds)) - print("\tnanos: {}".format(response.create_time.nanos / 1e9)) - print( - "Evaluation example count: {}".format(response.evaluated_example_count) - ) - - # [END automl_video_object_tracking_get_model_evaluation_beta] - - print( - "Classification model evaluation metrics: {}".format( - response.classification_evaluation_metrics - ) - ) - # [END automl_video_classification_get_model_evaluation_beta] - - # [START automl_video_object_tracking_get_model_evaluation_beta] - print( - "Video object tracking model evaluation metrics: {}".format( - response.video_object_tracking_evaluation_metrics - ) - ) - # [END automl_video_object_tracking_get_model_evaluation_beta] diff --git a/automl/beta/get_model_evaluation_test.py b/automl/beta/get_model_evaluation_test.py deleted file mode 100644 index 5b2ecf3631d..00000000000 --- a/automl/beta/get_model_evaluation_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.cloud import automl_v1beta1 as automl -import pytest - -import get_model_evaluation - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -@pytest.fixture(scope="function") -def model_evaluation_id(): - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - generator = client.list_model_evaluations(model_full_id, "").pages - page = next(generator) - evaluation = page.next() - model_evaluation_id = evaluation.name.split( - "{}/modelEvaluations/".format(MODEL_ID) - )[1].split("\n")[0] - yield model_evaluation_id - - -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/beta/get_model_test.py b/automl/beta/get_model_test.py deleted file mode 100644 index 237ad6daaff..00000000000 --- a/automl/beta/get_model_test.py +++ /dev/null @@ -1,26 +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 get_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -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/beta/get_operation_status.py b/automl/beta/get_operation_status.py deleted file mode 100644 index f376e2461e4..00000000000 --- a/automl/beta/get_operation_status.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. - -# [START automl_get_operation_status_beta] -from google.cloud import automl_v1beta1 as automl - - -def get_operation_status( - operation_full_id="projects/YOUR_PROJECT_ID/locations/us-central1/" - "operations/YOUR_OPERATION_ID", -): - """Get operation status.""" - client = automl.AutoMlClient() - - # Get the latest state of a long-running operation. - response = client.transport._operations_client.get_operation( - operation_full_id - ) - - print("Name: {}".format(response.name)) - print("Operation details:") - print(response) -# [END automl_get_operation_status_beta] diff --git a/automl/beta/get_operation_status_test.py b/automl/beta/get_operation_status_test.py deleted file mode 100644 index 7da9e7b3b0e..00000000000 --- a/automl/beta/get_operation_status_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 os - -from google.cloud import automl_v1beta1 as automl -import pytest - -import get_operation_status - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@pytest.fixture(scope="function") -def operation_id(): - client = automl.AutoMlClient() - project_location = client.location_path(PROJECT_ID, "us-central1") - generator = client.transport._operations_client.list_operations( - project_location, filter_="" - ).pages - page = next(generator) - operation = page.next() - yield operation.name - - -def test_get_operation_status(capsys, operation_id): - get_operation_status.get_operation_status(operation_id) - out, _ = capsys.readouterr() - assert "Operation details" in out diff --git a/automl/beta/import_dataset.py b/automl/beta/import_dataset.py deleted file mode 100644 index 97f1c0b8c9a..00000000000 --- a/automl/beta/import_dataset.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. - - -# [START automl_import_data_beta] -from google.cloud import automl_v1beta1 as automl - - -def import_dataset( - project_id="YOUR_PROJECT_ID", - dataset_id="YOUR_DATASET_ID", - path="gs://YOUR_BUCKET_ID/path/to/data.csv", -): - """Import a dataset.""" - 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.types.GcsSource(input_uris=input_uris) - input_config = automl.types.InputConfig(gcs_source=gcs_source) - # Import data from the input URI - response = client.import_data(dataset_full_id, input_config) - - print("Processing import...") - print("Data imported. {}".format(response.result())) -# [END automl_import_data_beta] diff --git a/automl/beta/import_dataset_test.py b/automl/beta/import_dataset_test.py deleted file mode 100644 index 35d23edc7e8..00000000000 --- a/automl/beta/import_dataset_test.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. - -import os - -import import_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) -DATASET_ID = "TEN0000000000000000000" - - -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 = "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) - 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/beta/list_datasets.py b/automl/beta/list_datasets.py deleted file mode 100644 index 5d5c83a3908..00000000000 --- a/automl/beta/list_datasets.py +++ /dev/null @@ -1,53 +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. - - -# [START automl_video_classification_list_datasets_beta] -# [START automl_video_object_tracking_list_datasets_beta] -from google.cloud import automl_v1beta1 as automl - - -def list_datasets(project_id="YOUR_PROJECT_ID"): - """List datasets.""" - client = automl.AutoMlClient() - # A resource that represents Google Cloud Platform location. - project_location = client.location_path(project_id, "us-central1") - - # List all the datasets available in the region. - response = client.list_datasets(project_location, "") - - print("List of datasets:") - for dataset in response: - print("Dataset name: {}".format(dataset.name)) - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print("Dataset display name: {}".format(dataset.display_name)) - print("Dataset create time:") - print("\tseconds: {}".format(dataset.create_time.seconds)) - print("\tnanos: {}".format(dataset.create_time.nanos)) - # [END automl_video_object_tracking_list_datasets_beta] - - print( - "Video classification dataset metadata: {}".format( - dataset.video_classification_dataset_metadata - ) - ) - # [END automl_video_classification_list_datasets_beta] - - # [START automl_video_object_tracking_list_datasets_beta] - print( - "Video object tracking dataset metadata: {}".format( - dataset.video_object_tracking_dataset_metadata - ) - ) - # [END automl_video_object_tracking_list_datasets_beta] diff --git a/automl/beta/list_datasets_test.py b/automl/beta/list_datasets_test.py deleted file mode 100644 index 7057af815d1..00000000000 --- a/automl/beta/list_datasets_test.py +++ /dev/null @@ -1,27 +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 list_datasets - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"] - - -def test_list_dataset(capsys): - # list datasets - list_datasets.list_datasets(PROJECT_ID) - out, _ = capsys.readouterr() - assert "Dataset id: {}".format(DATASET_ID) in out diff --git a/automl/beta/list_models.py b/automl/beta/list_models.py deleted file mode 100644 index 7e9c7e34280..00000000000 --- a/automl/beta/list_models.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 list_models(project_id): - """List models.""" - # [START automl_list_models_beta] - from google.cloud import automl_v1beta1 as automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - - client = automl.AutoMlClient() - # A resource that represents Google Cloud Platform location. - project_location = client.location_path(project_id, "us-central1") - response = client.list_models(project_location, "") - - print("List of models:") - for model in response: - # Display the model information. - if ( - model.deployment_state - == automl.enums.Model.DeploymentState.DEPLOYED - ): - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - print("Model name: {}".format(model.name)) - print("Model id: {}".format(model.name.split("/")[-1])) - print("Model display name: {}".format(model.display_name)) - print("Model create time:") - print("\tseconds: {}".format(model.create_time.seconds)) - print("\tnanos: {}".format(model.create_time.nanos)) - print("Model deployment state: {}".format(deployment_state)) - # [END automl_list_models_beta] diff --git a/automl/beta/list_models_test.py b/automl/beta/list_models_test.py deleted file mode 100644 index 75f8c40a038..00000000000 --- a/automl/beta/list_models_test.py +++ /dev/null @@ -1,25 +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 list_models - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -def test_list_models(capsys): - list_models.list_models(PROJECT_ID) - out, _ = capsys.readouterr() - assert "Model id: " in out diff --git a/automl/beta/requirements-test.txt b/automl/beta/requirements-test.txt deleted file mode 100644 index 7e460c8c866..00000000000 --- a/automl/beta/requirements-test.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==6.0.1 diff --git a/automl/beta/requirements.txt b/automl/beta/requirements.txt deleted file mode 100644 index 867dfc61e77..00000000000 --- a/automl/beta/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-automl==1.0.1 diff --git a/automl/beta/set_endpoint.py b/automl/beta/set_endpoint.py deleted file mode 100644 index 436e427ec82..00000000000 --- a/automl/beta/set_endpoint.py +++ /dev/null @@ -1,39 +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 set_endpoint(project_id): - """Change your endpoint""" - # [START automl_set_endpoint] - from google.cloud import automl_v1beta1 as automl - - # You must first create a dataset, using the `eu` endpoint, before you can - # call other operations such as: list, get, import, delete, etc. - client_options = {'api_endpoint': 'eu-automl.googleapis.com:443'} - - # Instantiates a client - client = automl.AutoMlClient(client_options=client_options) - - # A resource that represents Google Cloud Platform location. - # project_id = 'YOUR_PROJECT_ID' - project_location = client.location_path(project_id, 'eu') - # [END automl_set_endpoint] - - # List all the datasets available - # Note: Create a dataset in `eu`, before calling `list_datasets`. - response = client.list_datasets( - project_location, filter_='') - - for dataset in response: - print(dataset) diff --git a/automl/beta/set_endpoint_test.py b/automl/beta/set_endpoint_test.py deleted file mode 100644 index 02339311823..00000000000 --- a/automl/beta/set_endpoint_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 - -import set_endpoint - - -PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] - - -def test_set_endpoint(capsys): - set_endpoint.set_endpoint(PROJECT_ID) - - out, _ = capsys.readouterr() - # Look for the display name - assert 'do_not_delete_me' in out diff --git a/automl/beta/video_classification_create_dataset.py b/automl/beta/video_classification_create_dataset.py deleted file mode 100644 index 086f98f05f4..00000000000 --- a/automl/beta/video_classification_create_dataset.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. - - -# [START automl_video_classification_create_dataset_beta] -from google.cloud import automl_v1beta1 as automl - - -def create_dataset( - project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name" -): - """Create a automl video classification dataset.""" - - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = client.location_path(project_id, "us-central1") - metadata = automl.types.VideoClassificationDatasetMetadata() - dataset = automl.types.Dataset( - display_name=display_name, - video_classification_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - created_dataset = client.create_dataset(project_location, dataset) - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - - # To get the dataset id, you have to parse it out of the `name` field. - # As dataset Ids are required for other methods. - # Name Form: - # `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) -# [END automl_video_classification_create_dataset_beta] diff --git a/automl/beta/video_classification_create_dataset_test.py b/automl/beta/video_classification_create_dataset_test.py deleted file mode 100644 index 443f504251a..00000000000 --- a/automl/beta/video_classification_create_dataset_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 uuid - -from google.cloud import automl_v1beta1 as automl -import pytest - -import video_classification_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = None - - -@pytest.fixture(scope="function", autouse=True) -def teardown(): - yield - - # Delete the created dataset - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path( - PROJECT_ID, "us-central1", DATASET_ID - ) - response = client.delete_dataset(dataset_full_id) - response.result() - - -def test_video_classification_create_dataset(capsys): - # create dataset - dataset_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] - video_classification_create_dataset.create_dataset( - PROJECT_ID, dataset_name - ) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Get the dataset id for deletion - global DATASET_ID - DATASET_ID = out.splitlines()[1].split()[2] diff --git a/automl/beta/video_classification_create_model.py b/automl/beta/video_classification_create_model.py deleted file mode 100644 index 5bf19b4e2c5..00000000000 --- a/automl/beta/video_classification_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. - -# [START automl_video_classification_create_model_beta] -from google.cloud import automl_v1beta1 as automl - - -def create_model( - project_id="YOUR_PROJECT_ID", - dataset_id="YOUR_DATASET_ID", - display_name="your_models_display_name", -): - """Create a automl video classification model.""" - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = client.location_path(project_id, "us-central1") - # Leave model unset to use the default base model provided by Google - metadata = automl.types.VideoClassificationModelMetadata() - model = automl.types.Model( - display_name=display_name, - dataset_id=dataset_id, - video_classification_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") -# [END automl_video_classification_create_model_beta] diff --git a/automl/beta/video_classification_create_model_test.py b/automl/beta/video_classification_create_model_test.py deleted file mode 100644 index 593166cb647..00000000000 --- a/automl/beta/video_classification_create_model_test.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. - -import os -import uuid - -from google.cloud import automl_v1beta1 as automl -import pytest - -import video_classification_create_model - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -DATASET_ID = "VCN510437278078730240" -OPERATION_ID = None - - -@pytest.fixture(scope="function", autouse=True) -def teardown(): - yield - - # Cancel the training operation - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(OPERATION_ID) - - -def test_video_classification_create_model(capsys): - model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] - video_classification_create_model.create_model( - PROJECT_ID, DATASET_ID, model_name - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - global OPERATION_ID - OPERATION_ID = out.split("Training operation name: ")[1].split("\n")[0] diff --git a/automl/beta/video_object_tracking_create_dataset.py b/automl/beta/video_object_tracking_create_dataset.py deleted file mode 100644 index 2a651d0d406..00000000000 --- a/automl/beta/video_object_tracking_create_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. - -# [START automl_video_object_tracking_create_dataset_beta] -from google.cloud import automl_v1beta1 as automl - - -def create_dataset( - project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name" -): - """Create a automl video object tracking dataset.""" - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform location. - project_location = client.location_path(project_id, "us-central1") - metadata = automl.types.VideoObjectTrackingDatasetMetadata() - dataset = automl.types.Dataset( - display_name=display_name, - video_object_tracking_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - created_dataset = client.create_dataset(project_location, dataset) - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) -# [END automl_video_object_tracking_create_dataset_beta] diff --git a/automl/beta/video_object_tracking_create_dataset_test.py b/automl/beta/video_object_tracking_create_dataset_test.py deleted file mode 100644 index 96957f71013..00000000000 --- a/automl/beta/video_object_tracking_create_dataset_test.py +++ /dev/null @@ -1,51 +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 uuid - -from google.cloud import automl_v1beta1 as automl -import pytest - -import video_object_tracking_create_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = None - - -@pytest.fixture(scope="function", autouse=True) -def teardown(): - yield - - # Delete the created dataset - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path( - PROJECT_ID, "us-central1", DATASET_ID - ) - response = client.delete_dataset(dataset_full_id) - response.result() - - -def test_video_classification_create_dataset(capsys): - # create dataset - dataset_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] - video_object_tracking_create_dataset.create_dataset( - PROJECT_ID, dataset_name - ) - out, _ = capsys.readouterr() - assert "Dataset id: " in out - - # Get the dataset id for deletion - global DATASET_ID - DATASET_ID = out.splitlines()[1].split()[2] diff --git a/automl/beta/video_object_tracking_create_model.py b/automl/beta/video_object_tracking_create_model.py deleted file mode 100644 index 5ff8be9873e..00000000000 --- a/automl/beta/video_object_tracking_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. - -# [START automl_video_object_tracking_create_model_beta] -from google.cloud import automl_v1beta1 as automl - - -def create_model( - project_id="YOUR_PROJECT_ID", - dataset_id="YOUR_DATASET_ID", - display_name="your_models_display_name", -): - """Create a automl video classification model.""" - client = automl.AutoMlClient() - - # A resource that represents Google Cloud Platform loacation. - project_location = client.location_path(project_id, "us-central1") - # Leave model unset to use the default base model provided by Google - metadata = automl.types.VideoObjectTrackingModelMetadata() - model = automl.types.Model( - display_name=display_name, - dataset_id=dataset_id, - video_object_tracking_model_metadata=metadata, - ) - - # Create a model with the model metadata in the region. - response = client.create_model(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") -# [END automl_video_object_tracking_create_model_beta] diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py deleted file mode 100644 index a06d65bfe4d..00000000000 --- a/automl/beta/video_object_tracking_create_model_test.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. - -import os -import uuid - -from google.cloud import automl_v1beta1 as automl -import pytest - -import video_object_tracking_create_model - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -DATASET_ID = "VOT2823376535338090496" -OPERATION_ID = None - - -@pytest.fixture(scope="function", autouse=True) -def teardown(): - yield - - # Cancel the training operation - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(OPERATION_ID) - - -def test_video_classification_create_model(capsys): - model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] - video_object_tracking_create_model.create_model( - PROJECT_ID, DATASET_ID, model_name - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - global OPERATION_ID - OPERATION_ID = out.split("Training operation name: ")[1].split("\n")[0] diff --git a/automl/cloud-client/batch_predict.py b/automl/cloud-client/batch_predict.py deleted file mode 100644 index efe484f4350..00000000000 --- a/automl/cloud-client/batch_predict.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 batch_predict(project_id, model_id, input_uri, output_uri): - """Batch predict""" - # [START automl_batch_predict] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # model_id = "YOUR_MODEL_ID" - # input_uri = "gs://YOUR_BUCKET_ID/path/to/your/input/csv_or_jsonl" - # output_uri = "gs://YOUR_BUCKET_ID/path/to/save/results/" - - prediction_client = automl.PredictionServiceClient() - - # Get the full path of the model. - model_full_id = prediction_client.model_path( - project_id, "us-central1", model_id - ) - - gcs_source = automl.types.GcsSource(input_uris=[input_uri]) - - input_config = automl.types.BatchPredictInputConfig(gcs_source=gcs_source) - gcs_destination = automl.types.GcsDestination(output_uri_prefix=output_uri) - output_config = automl.types.BatchPredictOutputConfig( - gcs_destination=gcs_destination - ) - - response = prediction_client.batch_predict( - model_full_id, input_config, output_config - ) - - print("Waiting for operation to complete...") - print( - "Batch Prediction results saved to Cloud Storage bucket. {}".format( - response.result() - ) - ) - # [END automl_batch_predict] diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py deleted file mode 100644 index 2869873a919..00000000000 --- a/automl/cloud-client/batch_predict_test.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 ladnguage governing permissions and -# limitations under the License. - -import datetime -import os - -import batch_predict - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) -MODEL_ID = "TEN0000000000000000000" -PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime( - "%Y%m%d%H%M%S" -) - - -def test_batch_predict(capsys): - # As batch prediction can take a long time. Try to batch predict on a model - # and confirm that the model was not found, but other elements of the - # request were valid. - try: - input_uri = "gs://{}/entity-extraction/input.jsonl".format(BUCKET_ID) - output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX) - batch_predict.batch_predict( - PROJECT_ID, MODEL_ID, input_uri, output_uri - ) - out, _ = capsys.readouterr() - assert ( - "does not exist" - in out - ) - except Exception as e: - assert ( - "does not exist" - in e.message - ) diff --git a/automl/cloud-client/delete_dataset.py b/automl/cloud-client/delete_dataset.py deleted file mode 100644 index e6136c13b84..00000000000 --- a/automl/cloud-client/delete_dataset.py +++ /dev/null @@ -1,33 +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(dataset_full_id) - - print("Dataset deleted. {}".format(response.result())) - # [END automl_delete_dataset] diff --git a/automl/cloud-client/delete_dataset_test.py b/automl/cloud-client/delete_dataset_test.py deleted file mode 100644 index 6d204dde8c0..00000000000 --- a/automl/cloud-client/delete_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.cloud import automl -import pytest - -import delete_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) - - -@pytest.fixture(scope="function") -def dataset_id(): - client = automl.AutoMlClient() - project_location = client.location_path(PROJECT_ID, "us-central1") - display_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - metadata = automl.types.TextExtractionDatasetMetadata() - dataset = automl.types.Dataset( - display_name=display_name, text_extraction_dataset_metadata=metadata - ) - response = client.create_dataset(project_location, dataset) - dataset_id = response.result().name.split("/")[-1] - - yield dataset_id - - -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/cloud-client/delete_model.py b/automl/cloud-client/delete_model.py deleted file mode 100644 index cc6e7546882..00000000000 --- a/automl/cloud-client/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(model_full_id) - - print("Model deleted. {}".format(response.result())) - # [END automl_delete_model] diff --git a/automl/cloud-client/delete_model_test.py b/automl/cloud-client/delete_model_test.py deleted file mode 100644 index 8721e14b44b..00000000000 --- a/automl/cloud-client/delete_model_test.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. - -import os - -import delete_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -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/cloud-client/deploy_model.py b/automl/cloud-client/deploy_model.py deleted file mode 100644 index cc55cf1e312..00000000000 --- a/automl/cloud-client/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(model_full_id) - - print("Model deployment finished. {}".format(response.result())) - # [END automl_deploy_model] diff --git a/automl/cloud-client/deploy_model_test.py b/automl/cloud-client/deploy_model_test.py deleted file mode 100644 index d8a72f1eee0..00000000000 --- a/automl/cloud-client/deploy_model_test.py +++ /dev/null @@ -1,35 +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 - -import pytest - -import deploy_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "TRL0000000000000000000" - - -@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/cloud-client/export_dataset.py b/automl/cloud-client/export_dataset.py deleted file mode 100644 index 45f7ee6bdd7..00000000000 --- a/automl/cloud-client/export_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 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.types.GcsDestination(output_uri_prefix=gcs_uri) - output_config = automl.types.OutputConfig(gcs_destination=gcs_destination) - - response = client.export_data(dataset_full_id, output_config) - print("Dataset exported. {}".format(response.result())) - # [END automl_export_dataset] diff --git a/automl/cloud-client/export_dataset_test.py b/automl/cloud-client/export_dataset_test.py deleted file mode 100644 index de8bfe5aefa..00000000000 --- a/automl/cloud-client/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 - -import export_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) -PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime( - "%Y%m%d%H%M%S" -) -DATASET_ID = "TEN0000000000000000000" - - -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, "gs://{}/{}/".format(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/cloud-client/get_dataset.py b/automl/cloud-client/get_dataset.py deleted file mode 100644 index a1831903c17..00000000000 --- a/automl/cloud-client/get_dataset.py +++ /dev/null @@ -1,100 +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(dataset_full_id) - - # Display the dataset information - print("Dataset name: {}".format(dataset.name)) - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print("Dataset display name: {}".format(dataset.display_name)) - print("Dataset create time:") - print("\tseconds: {}".format(dataset.create_time.seconds)) - print("\tnanos: {}".format(dataset.create_time.nanos)) - # [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/cloud-client/get_dataset_test.py b/automl/cloud-client/get_dataset_test.py deleted file mode 100644 index 3cc688eb3f9..00000000000 --- a/automl/cloud-client/get_dataset_test.py +++ /dev/null @@ -1,26 +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 get_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"] - - -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/cloud-client/get_model.py b/automl/cloud-client/get_model.py deleted file mode 100644 index b1ea5154f60..00000000000 --- a/automl/cloud-client/get_model.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 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(model_full_id) - - # Retrieve deployment state. - if model.deployment_state == automl.enums.Model.DeploymentState.DEPLOYED: - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - # Display the model information. - print("Model name: {}".format(model.name)) - print("Model id: {}".format(model.name.split("/")[-1])) - print("Model display name: {}".format(model.display_name)) - print("Model create time:") - print("\tseconds: {}".format(model.create_time.seconds)) - print("\tnanos: {}".format(model.create_time.nanos)) - print("Model deployment state: {}".format(deployment_state)) - # [END automl_get_model] diff --git a/automl/cloud-client/get_model_evaluation.py b/automl/cloud-client/get_model_evaluation.py deleted file mode 100644 index 4a1a97a393e..00000000000 --- a/automl/cloud-client/get_model_evaluation.py +++ /dev/null @@ -1,92 +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_evaluation_full_id = client.model_evaluation_path( - project_id, "us-central1", model_id, model_evaluation_id - ) - - # Get complete detail of the model evaluation. - response = client.get_model_evaluation(model_evaluation_full_id) - - print("Model evaluation name: {}".format(response.name)) - print("Model annotation spec id: {}".format(response.annotation_spec_id)) - print("Create Time:") - print("\tseconds: {}".format(response.create_time.seconds)) - print("\tnanos: {}".format(response.create_time.nanos / 1e9)) - print( - "Evaluation example count: {}".format(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/cloud-client/get_model_evaluation_test.py b/automl/cloud-client/get_model_evaluation_test.py deleted file mode 100644 index f3fe1b2bc18..00000000000 --- a/automl/cloud-client/get_model_evaluation_test.py +++ /dev/null @@ -1,45 +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.cloud import automl -import pytest - -import get_model_evaluation - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -@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(model_full_id, ""): - evaluation = e - break - model_evaluation_id = evaluation.name.split( - "{}/modelEvaluations/".format(MODEL_ID) - )[1].split("\n")[0] - yield model_evaluation_id - - -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/cloud-client/get_model_test.py b/automl/cloud-client/get_model_test.py deleted file mode 100644 index c146e18cbfd..00000000000 --- a/automl/cloud-client/get_model_test.py +++ /dev/null @@ -1,26 +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 - -import get_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -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/cloud-client/get_operation_status.py b/automl/cloud-client/get_operation_status.py deleted file mode 100644 index 4e5c90f86e1..00000000000 --- a/automl/cloud-client/get_operation_status.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. - - -def get_operation_status(operation_full_id): - """Get operation status.""" - # [START automl_get_operation_status] - from google.cloud import automl - - # TODO(developer): Uncomment and set the following variables - # operation_full_id = \ - # "projects/[projectId]/locations/us-central1/operations/[operationId]" - - client = automl.AutoMlClient() - # Get the latest state of a long-running operation. - response = client.transport._operations_client.get_operation( - operation_full_id - ) - - print("Name: {}".format(response.name)) - print("Operation details:") - print(response) - # [END automl_get_operation_status] diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py deleted file mode 100644 index c08095fc2bf..00000000000 --- a/automl/cloud-client/get_operation_status_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 os - -from google.cloud import automl -import pytest - -import get_operation_status - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@pytest.fixture(scope="function") -def operation_id(): - client = automl.AutoMlClient() - project_location = client.location_path(PROJECT_ID, "us-central1") - generator = client.transport._operations_client.list_operations( - project_location, filter_="" - ).pages - page = next(generator) - operation = page.next() - yield operation.name - - -def test_get_operation_status(capsys, operation_id): - get_operation_status.get_operation_status(operation_id) - out, _ = capsys.readouterr() - assert "Operation details" in out diff --git a/automl/cloud-client/import_dataset.py b/automl/cloud-client/import_dataset.py deleted file mode 100644 index f465bdb1e02..00000000000 --- a/automl/cloud-client/import_dataset.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 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.types.GcsSource(input_uris=input_uris) - input_config = automl.types.InputConfig(gcs_source=gcs_source) - # Import data from the input URI - response = client.import_data(dataset_full_id, input_config) - - print("Processing import...") - print("Data imported. {}".format(response.result())) - # [END automl_import_data] diff --git a/automl/cloud-client/import_dataset_test.py b/automl/cloud-client/import_dataset_test.py deleted file mode 100644 index 35d23edc7e8..00000000000 --- a/automl/cloud-client/import_dataset_test.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. - -import os - -import import_dataset - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -BUCKET_ID = "{}-lcm".format(PROJECT_ID) -DATASET_ID = "TEN0000000000000000000" - - -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 = "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) - 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/cloud-client/language_entity_extraction_create_dataset.py b/automl/cloud-client/language_entity_extraction_create_dataset.py deleted file mode 100644 index 056ff22c9d5..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "us-central1") - metadata = automl.types.TextExtractionDatasetMetadata() - dataset = automl.types.Dataset( - display_name=display_name, text_extraction_dataset_metadata=metadata - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(project_location, dataset) - - created_dataset = response.result() - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_language_entity_extraction_create_dataset] diff --git a/automl/cloud-client/language_entity_extraction_create_dataset_test.py b/automl/cloud-client/language_entity_extraction_create_dataset_test.py deleted file mode 100644 index 044a0d50590..00000000000 --- a/automl/cloud-client/language_entity_extraction_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.cloud import automl - -import language_entity_extraction_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -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(dataset_full_id) - response.result() diff --git a/automl/cloud-client/language_entity_extraction_create_model.py b/automl/cloud-client/language_entity_extraction_create_model.py deleted file mode 100644 index 5e0748dd567..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "us-central1") - # Leave model unset to use the default base model provided by Google - metadata = automl.types.TextExtractionModelMetadata() - model = automl.types.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(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") - # [END automl_language_entity_extraction_create_model] diff --git a/automl/cloud-client/language_entity_extraction_create_model_test.py b/automl/cloud-client/language_entity_extraction_create_model_test.py deleted file mode 100644 index 0ff74c89b13..00000000000 --- a/automl/cloud-client/language_entity_extraction_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 - -import language_entity_extraction_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = "TEN0000000000000000000" - - -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/cloud-client/language_entity_extraction_predict.py b/automl/cloud-client/language_entity_extraction_predict.py deleted file mode 100644 index 40d7e89b280..00000000000 --- a/automl/cloud-client/language_entity_extraction_predict.py +++ /dev/null @@ -1,55 +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 = prediction_client.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.types.TextSnippet( - content=content, mime_type="text/plain" - ) - payload = automl.types.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(model_full_id, payload) - - for annotation_payload in response.payload: - print( - "Text Extract Entity Types: {}".format( - annotation_payload.display_name - ) - ) - print( - "Text Score: {}".format(annotation_payload.text_extraction.score) - ) - text_segment = annotation_payload.text_extraction.text_segment - print("Text Extract Entity Content: {}".format(text_segment.content)) - print("Text Start Offset: {}".format(text_segment.start_offset)) - print("Text End Offset: {}".format(text_segment.end_offset)) - # [END automl_language_entity_extraction_predict] diff --git a/automl/cloud-client/language_entity_extraction_predict_test.py b/automl/cloud-client/language_entity_extraction_predict_test.py deleted file mode 100644 index 35dfddefa05..00000000000 --- a/automl/cloud-client/language_entity_extraction_predict_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 os - -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(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(model_full_id) - response.result() - - -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/cloud-client/language_sentiment_analysis_create_dataset.py b/automl/cloud-client/language_sentiment_analysis_create_dataset.py deleted file mode 100644 index 2caae0656bb..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "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.types.TextSentimentDatasetMetadata( - sentiment_max=4 - ) # Possible max sentiment score: 1-10 - - dataset = automl.types.Dataset( - display_name=display_name, text_sentiment_dataset_metadata=metadata - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(project_location, dataset) - - created_dataset = response.result() - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_language_sentiment_analysis_create_dataset] diff --git a/automl/cloud-client/language_sentiment_analysis_create_dataset_test.py b/automl/cloud-client/language_sentiment_analysis_create_dataset_test.py deleted file mode 100644 index 239a154f84a..00000000000 --- a/automl/cloud-client/language_sentiment_analysis_create_dataset_test.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. - -import datetime -import os - -from google.cloud import automl - -import language_sentiment_analysis_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -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(dataset_full_id) - response.result() diff --git a/automl/cloud-client/language_sentiment_analysis_create_model.py b/automl/cloud-client/language_sentiment_analysis_create_model.py deleted file mode 100644 index 6eca50a7c55..00000000000 --- a/automl/cloud-client/language_sentiment_analysis_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_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 = client.location_path(project_id, "us-central1") - # Leave model unset to use the default base model provided by Google - metadata = automl.types.TextSentimentModelMetadata() - model = automl.types.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(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") - # [END automl_language_sentiment_analysis_create_model] diff --git a/automl/cloud-client/language_sentiment_analysis_create_model_test.py b/automl/cloud-client/language_sentiment_analysis_create_model_test.py deleted file mode 100644 index bf9d19788b5..00000000000 --- a/automl/cloud-client/language_sentiment_analysis_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.cloud import automl -import pytest - -import language_sentiment_analysis_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["SENTIMENT_ANALYSIS_DATASET_ID"] - - -@pytest.mark.slow -def test_sentiment_analysis_create_model(capsys): - language_sentiment_analysis_create_model.create_model( - PROJECT_ID, DATASET_ID, "sentiment_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - operation_id = out.split("Training operation name: ")[1].split("\n")[0] - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(operation_id) diff --git a/automl/cloud-client/language_sentiment_analysis_predict.py b/automl/cloud-client/language_sentiment_analysis_predict.py deleted file mode 100644 index cf459142ff1..00000000000 --- a/automl/cloud-client/language_sentiment_analysis_predict.py +++ /dev/null @@ -1,51 +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 = prediction_client.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.types.TextSnippet( - content=content, mime_type="text/plain" - ) - payload = automl.types.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(model_full_id, payload) - - for annotation_payload in response.payload: - print( - "Predicted class name: {}".format(annotation_payload.display_name) - ) - print( - "Predicted sentiment score: {}".format( - annotation_payload.text_sentiment.sentiment - ) - ) - # [END automl_language_sentiment_analysis_predict] diff --git a/automl/cloud-client/language_sentiment_analysis_predict_test.py b/automl/cloud-client/language_sentiment_analysis_predict_test.py deleted file mode 100644 index bfd35649cb1..00000000000 --- a/automl/cloud-client/language_sentiment_analysis_predict_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.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(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(model_full_id) - response.result() - - -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/cloud-client/language_text_classification_create_dataset.py b/automl/cloud-client/language_text_classification_create_dataset.py deleted file mode 100644 index f4a2add495e..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "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.types.TextClassificationDatasetMetadata( - classification_type=automl.enums.ClassificationType.MULTICLASS - ) - dataset = automl.types.Dataset( - display_name=display_name, - text_classification_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(project_location, dataset) - - created_dataset = response.result() - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_language_text_classification_create_dataset] diff --git a/automl/cloud-client/language_text_classification_create_dataset_test.py b/automl/cloud-client/language_text_classification_create_dataset_test.py deleted file mode 100644 index 771945eeafb..00000000000 --- a/automl/cloud-client/language_text_classification_create_dataset_test.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. - -import datetime -import os - -from google.cloud import automl - -import language_text_classification_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -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(dataset_full_id) - response.result() diff --git a/automl/cloud-client/language_text_classification_create_model.py b/automl/cloud-client/language_text_classification_create_model.py deleted file mode 100644 index b72ec8b4538..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "us-central1") - # Leave model unset to use the default base model provided by Google - metadata = automl.types.TextClassificationModelMetadata() - model = automl.types.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(project_location, model) - - print(u"Training operation name: {}".format(response.operation.name)) - print("Training started...") - # [END automl_language_text_classification_create_model] diff --git a/automl/cloud-client/language_text_classification_create_model_test.py b/automl/cloud-client/language_text_classification_create_model_test.py deleted file mode 100644 index 9b5c6f01f1e..00000000000 --- a/automl/cloud-client/language_text_classification_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.cloud import automl -import pytest - -import language_text_classification_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["TEXT_CLASSIFICATION_DATASET_ID"] - - -@pytest.mark.slow -def test_text_classification_create_model(capsys): - language_text_classification_create_model.create_model( - PROJECT_ID, DATASET_ID, "classification_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - operation_id = out.split("Training operation name: ")[1].split("\n")[0] - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(operation_id) diff --git a/automl/cloud-client/language_text_classification_predict.py b/automl/cloud-client/language_text_classification_predict.py deleted file mode 100644 index 6edac71eb10..00000000000 --- a/automl/cloud-client/language_text_classification_predict.py +++ /dev/null @@ -1,51 +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 = prediction_client.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.types.TextSnippet( - content=content, mime_type="text/plain" - ) - payload = automl.types.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(model_full_id, payload) - - for annotation_payload in response.payload: - print( - u"Predicted class name: {}".format(annotation_payload.display_name) - ) - print( - u"Predicted class score: {}".format( - annotation_payload.classification.score - ) - ) - # [END automl_language_text_classification_predict] diff --git a/automl/cloud-client/language_text_classification_predict_test.py b/automl/cloud-client/language_text_classification_predict_test.py deleted file mode 100644 index 36202f5b424..00000000000 --- a/automl/cloud-client/language_text_classification_predict_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.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(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(model_full_id) - response.result() - - -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/cloud-client/list_datasets.py b/automl/cloud-client/list_datasets.py deleted file mode 100644 index ae8c576e33c..00000000000 --- a/automl/cloud-client/list_datasets.py +++ /dev/null @@ -1,100 +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 = client.location_path(project_id, "us-central1") - - # List all the datasets available in the region. - response = client.list_datasets(project_location, "") - - print("List of datasets:") - for dataset in response: - print("Dataset name: {}".format(dataset.name)) - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print("Dataset display name: {}".format(dataset.display_name)) - print("Dataset create time:") - print("\tseconds: {}".format(dataset.create_time.seconds)) - print("\tnanos: {}".format(dataset.create_time.nanos)) - # [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/cloud-client/list_datasets_test.py b/automl/cloud-client/list_datasets_test.py deleted file mode 100644 index 7057af815d1..00000000000 --- a/automl/cloud-client/list_datasets_test.py +++ /dev/null @@ -1,27 +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 list_datasets - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"] - - -def test_list_dataset(capsys): - # list datasets - list_datasets.list_datasets(PROJECT_ID) - out, _ = capsys.readouterr() - assert "Dataset id: {}".format(DATASET_ID) in out diff --git a/automl/cloud-client/list_model_evaluations.py b/automl/cloud-client/list_model_evaluations.py deleted file mode 100644 index 3dcb7932a2f..00000000000 --- a/automl/cloud-client/list_model_evaluations.py +++ /dev/null @@ -1,94 +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(model_full_id, ""): - print("Model evaluation name: {}".format(evaluation.name)) - print( - "Model annotation spec id: {}".format( - evaluation.annotation_spec_id - ) - ) - print("Create Time:") - print("\tseconds: {}".format(evaluation.create_time.seconds)) - print("\tnanos: {}".format(evaluation.create_time.nanos / 1e9)) - print( - "Evaluation example count: {}".format( - 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/cloud-client/list_model_evaluations_test.py b/automl/cloud-client/list_model_evaluations_test.py deleted file mode 100644 index 9d8cbc5989d..00000000000 --- a/automl/cloud-client/list_model_evaluations_test.py +++ /dev/null @@ -1,27 +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 - -import list_model_evaluations - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] - - -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/cloud-client/list_models.py b/automl/cloud-client/list_models.py deleted file mode 100644 index 5c5dff6743f..00000000000 --- a/automl/cloud-client/list_models.py +++ /dev/null @@ -1,47 +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 = client.location_path(project_id, "us-central1") - response = client.list_models(project_location, "") - - print("List of models:") - for model in response: - # Display the model information. - if ( - model.deployment_state - == automl.enums.Model.DeploymentState.DEPLOYED - ): - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - print("Model name: {}".format(model.name)) - print("Model id: {}".format(model.name.split("/")[-1])) - print("Model display name: {}".format(model.display_name)) - print("Model create time:") - print("\tseconds: {}".format(model.create_time.seconds)) - print("\tnanos: {}".format(model.create_time.nanos)) - print("Model deployment state: {}".format(deployment_state)) - # [END automl_list_models] diff --git a/automl/cloud-client/list_models_test.py b/automl/cloud-client/list_models_test.py deleted file mode 100644 index 93f79d580e7..00000000000 --- a/automl/cloud-client/list_models_test.py +++ /dev/null @@ -1,25 +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 - -import list_models - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -def test_list_models(capsys): - list_models.list_models(PROJECT_ID) - out, _ = capsys.readouterr() - assert "Model id: " in out diff --git a/automl/cloud-client/list_operation_status.py b/automl/cloud-client/list_operation_status.py deleted file mode 100644 index 45534fda73a..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "us-central1") - # List all the operations names available in the region. - response = client.transport._operations_client.list_operations( - project_location, "" - ) - - print("List of operations:") - for operation in response: - print("Name: {}".format(operation.name)) - print("Operation details:") - print(operation) - # [END automl_list_operation_status] diff --git a/automl/cloud-client/list_operation_status_test.py b/automl/cloud-client/list_operation_status_test.py deleted file mode 100644 index aaeccaaf1ab..00000000000 --- a/automl/cloud-client/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/cloud-client/requirements-test.txt b/automl/cloud-client/requirements-test.txt deleted file mode 100644 index d0029c6de49..00000000000 --- a/automl/cloud-client/requirements-test.txt +++ /dev/null @@ -1,2 +0,0 @@ -backoff==1.10.0 -pytest==6.0.1 diff --git a/automl/cloud-client/requirements.txt b/automl/cloud-client/requirements.txt deleted file mode 100644 index 2654651722d..00000000000 --- a/automl/cloud-client/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -google-cloud-automl==1.0.1 -google-cloud-storage==1.30.0 diff --git a/automl/cloud-client/resources/input.txt b/automl/cloud-client/resources/input.txt deleted file mode 100644 index 5aecd6590fc..00000000000 --- a/automl/cloud-client/resources/input.txt +++ /dev/null @@ -1 +0,0 @@ -Tell me how this ends \ No newline at end of file diff --git a/automl/cloud-client/resources/salad.jpg b/automl/cloud-client/resources/salad.jpg deleted file mode 100644 index a7f960b5030..00000000000 Binary files a/automl/cloud-client/resources/salad.jpg and /dev/null differ diff --git a/automl/cloud-client/resources/test.png b/automl/cloud-client/resources/test.png deleted file mode 100644 index 653342a46e5..00000000000 Binary files a/automl/cloud-client/resources/test.png and /dev/null differ diff --git a/automl/cloud-client/translate_create_dataset.py b/automl/cloud-client/translate_create_dataset.py deleted file mode 100644 index 8f468679f92..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "us-central1") - # For a list of supported languages, see: - # https://cloud.google.com/translate/automl/docs/languages - dataset_metadata = automl.types.TranslationDatasetMetadata( - source_language_code="en", target_language_code="ja" - ) - dataset = automl.types.Dataset( - display_name=display_name, - translation_dataset_metadata=dataset_metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(project_location, dataset) - - created_dataset = response.result() - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_translate_create_dataset] diff --git a/automl/cloud-client/translate_create_dataset_test.py b/automl/cloud-client/translate_create_dataset_test.py deleted file mode 100644 index 9011da976ec..00000000000 --- a/automl/cloud-client/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.cloud import automl - -import translate_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -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(dataset_full_id) - response.result() diff --git a/automl/cloud-client/translate_create_model.py b/automl/cloud-client/translate_create_model.py deleted file mode 100644 index c83c1aa4ab4..00000000000 --- a/automl/cloud-client/translate_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_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 = client.location_path(project_id, "us-central1") - # Leave model unset to use the default base model provided by Google - translation_model_metadata = automl.types.TranslationModelMetadata() - model = automl.types.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(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") - # [END automl_translate_create_model] diff --git a/automl/cloud-client/translate_create_model_test.py b/automl/cloud-client/translate_create_model_test.py deleted file mode 100644 index b564d7e0701..00000000000 --- a/automl/cloud-client/translate_create_model_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 - -from google.cloud import automl - -import translate_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["TRANSLATION_DATASET_ID"] - - -def test_translate_create_model(capsys): - translate_create_model.create_model( - PROJECT_ID, DATASET_ID, "translate_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - operation_id = out.split("Training operation name: ")[1].split("\n")[0] - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(operation_id) diff --git a/automl/cloud-client/translate_predict.py b/automl/cloud-client/translate_predict.py deleted file mode 100644 index 31c965e8643..00000000000 --- a/automl/cloud-client/translate_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, 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 = prediction_client.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.types.TextSnippet(content=content) - payload = automl.types.ExamplePayload(text_snippet=text_snippet) - - response = prediction_client.predict(model_full_id, payload) - translated_content = response.payload[0].translation.translated_content - - print(u"Translated content: {}".format(translated_content.content)) - # [END automl_translate_predict] diff --git a/automl/cloud-client/translate_predict_test.py b/automl/cloud-client/translate_predict_test.py deleted file mode 100644 index cd31d98b1c4..00000000000 --- a/automl/cloud-client/translate_predict_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 os - -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(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(model_full_id) - response.result() - - -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/cloud-client/undeploy_model.py b/automl/cloud-client/undeploy_model.py deleted file mode 100644 index b737064dc5f..00000000000 --- a/automl/cloud-client/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(model_full_id) - - print("Model undeployment finished. {}".format(response.result())) - # [END automl_undeploy_model] diff --git a/automl/cloud-client/undeploy_model_test.py b/automl/cloud-client/undeploy_model_test.py deleted file mode 100644 index 49e9b1305e3..00000000000 --- a/automl/cloud-client/undeploy_model_test.py +++ /dev/null @@ -1,35 +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 - -import pytest - -import undeploy_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "TRL0000000000000000000" - - -@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/cloud-client/vision_classification_create_dataset.py b/automl/cloud-client/vision_classification_create_dataset.py deleted file mode 100644 index 8981a795679..00000000000 --- a/automl/cloud-client/vision_classification_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_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 = client.location_path(project_id, "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.types.ImageClassificationDatasetMetadata( - classification_type=automl.enums.ClassificationType.MULTILABEL - ) - dataset = automl.types.Dataset( - display_name=display_name, - image_classification_dataset_metadata=metadata, - ) - - # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(project_location, dataset) - - created_dataset = response.result() - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_vision_classification_create_dataset] diff --git a/automl/cloud-client/vision_classification_create_dataset_test.py b/automl/cloud-client/vision_classification_create_dataset_test.py deleted file mode 100644 index aaa8a575538..00000000000 --- a/automl/cloud-client/vision_classification_create_dataset_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 datetime -import os - -from google.cloud import automl -import pytest - -import vision_classification_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@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(dataset_full_id) - response.result() diff --git a/automl/cloud-client/vision_classification_create_model.py b/automl/cloud-client/vision_classification_create_model.py deleted file mode 100644 index 30505614e9e..00000000000 --- a/automl/cloud-client/vision_classification_create_model.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. - - -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 = client.location_path(project_id, "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.types.ImageClassificationModelMetadata( - train_budget_milli_node_hours=24000 - ) - model = automl.types.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(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") - # [END automl_vision_classification_create_model] diff --git a/automl/cloud-client/vision_classification_create_model_test.py b/automl/cloud-client/vision_classification_create_model_test.py deleted file mode 100644 index aea9926ab17..00000000000 --- a/automl/cloud-client/vision_classification_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.cloud import automl -import pytest - -import vision_classification_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["VISION_CLASSIFICATION_DATASET_ID"] - - -@pytest.mark.slow -def test_vision_classification_create_model(capsys): - vision_classification_create_model.create_model( - PROJECT_ID, DATASET_ID, "classification_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - operation_id = out.split("Training operation name: ")[1].split("\n")[0] - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(operation_id) diff --git a/automl/cloud-client/vision_classification_deploy_model_node_count.py b/automl/cloud-client/vision_classification_deploy_model_node_count.py deleted file mode 100644 index b89cec73ee8..00000000000 --- a/automl/cloud-client/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.types.ImageClassificationModelDeploymentMetadata( - node_count=2 - ) - response = client.deploy_model( - model_full_id, image_classification_model_deployment_metadata=metadata - ) - - print("Model deployment finished. {}".format(response.result())) - # [END automl_vision_classification_deploy_model_node_count] diff --git a/automl/cloud-client/vision_classification_deploy_model_node_count_test.py b/automl/cloud-client/vision_classification_deploy_model_node_count_test.py deleted file mode 100644 index 3f6ff430a4d..00000000000 --- a/automl/cloud-client/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 - -import pytest - -import vision_classification_deploy_model_node_count - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "ICN0000000000000000000" - - -@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/cloud-client/vision_classification_predict.py b/automl/cloud-client/vision_classification_predict.py deleted file mode 100644 index c42606ccece..00000000000 --- a/automl/cloud-client/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 = prediction_client.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.types.Image(image_bytes=content) - payload = automl.types.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"} - - response = prediction_client.predict(model_full_id, payload, params) - print("Prediction results:") - for result in response.payload: - print("Predicted class name: {}".format(result.display_name)) - print("Predicted class score: {}".format(result.classification.score)) - # [END automl_vision_classification_predict] diff --git a/automl/cloud-client/vision_classification_predict_test.py b/automl/cloud-client/vision_classification_predict_test.py deleted file mode 100644 index bc91796a980..00000000000 --- a/automl/cloud-client/vision_classification_predict_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.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(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(model_full_id) - response.result() - - -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/cloud-client/vision_object_detection_create_dataset.py b/automl/cloud-client/vision_object_detection_create_dataset.py deleted file mode 100644 index 9a4b1436b80..00000000000 --- a/automl/cloud-client/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 = client.location_path(project_id, "us-central1") - metadata = automl.types.ImageObjectDetectionDatasetMetadata() - dataset = automl.types.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(project_location, dataset) - - created_dataset = response.result() - - # Display the dataset information - print("Dataset name: {}".format(created_dataset.name)) - print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) - # [END automl_vision_object_detection_create_dataset] diff --git a/automl/cloud-client/vision_object_detection_create_dataset_test.py b/automl/cloud-client/vision_object_detection_create_dataset_test.py deleted file mode 100644 index 54fcf84d2bf..00000000000 --- a/automl/cloud-client/vision_object_detection_create_dataset_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 datetime -import os - -from google.cloud import automl -import pytest - -import vision_object_detection_create_dataset - - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] - - -@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(dataset_full_id) - response.result() diff --git a/automl/cloud-client/vision_object_detection_create_model.py b/automl/cloud-client/vision_object_detection_create_model.py deleted file mode 100644 index 980be41ce16..00000000000 --- a/automl/cloud-client/vision_object_detection_create_model.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. - - -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 = client.location_path(project_id, "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.types.ImageObjectDetectionModelMetadata( - train_budget_milli_node_hours=24000 - ) - model = automl.types.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(project_location, model) - - print("Training operation name: {}".format(response.operation.name)) - print("Training started...") - # [END automl_vision_object_detection_create_model] diff --git a/automl/cloud-client/vision_object_detection_create_model_test.py b/automl/cloud-client/vision_object_detection_create_model_test.py deleted file mode 100644 index af93a69b0bd..00000000000 --- a/automl/cloud-client/vision_object_detection_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.cloud import automl -import pytest - -import vision_object_detection_create_model - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -DATASET_ID = os.environ["OBJECT_DETECTION_DATASET_ID"] - - -@pytest.mark.slow -def test_vision_object_detection_create_model(capsys): - vision_object_detection_create_model.create_model( - PROJECT_ID, DATASET_ID, "object_test_create_model" - ) - out, _ = capsys.readouterr() - assert "Training started" in out - - # Cancel the operation - operation_id = out.split("Training operation name: ")[1].split("\n")[0] - client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(operation_id) diff --git a/automl/cloud-client/vision_object_detection_deploy_model_node_count.py b/automl/cloud-client/vision_object_detection_deploy_model_node_count.py deleted file mode 100644 index 2daa4101ac0..00000000000 --- a/automl/cloud-client/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.types.ImageObjectDetectionModelDeploymentMetadata( - node_count=2 - ) - response = client.deploy_model( - model_full_id, - image_object_detection_model_deployment_metadata=metadata, - ) - - print("Model deployment finished. {}".format(response.result())) - # [END automl_vision_object_detection_deploy_model_node_count] diff --git a/automl/cloud-client/vision_object_detection_deploy_model_node_count_test.py b/automl/cloud-client/vision_object_detection_deploy_model_node_count_test.py deleted file mode 100644 index 309b2525982..00000000000 --- a/automl/cloud-client/vision_object_detection_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 - -import pytest - -import vision_object_detection_deploy_model_node_count - -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -MODEL_ID = "0000000000000000000000" - - -@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/cloud-client/vision_object_detection_predict.py b/automl/cloud-client/vision_object_detection_predict.py deleted file mode 100644 index efd1534ecfb..00000000000 --- a/automl/cloud-client/vision_object_detection_predict.py +++ /dev/null @@ -1,58 +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 = prediction_client.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.types.Image(image_bytes=content) - payload = automl.types.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"} - - response = prediction_client.predict(model_full_id, payload, params) - print("Prediction results:") - for result in response.payload: - print("Predicted class name: {}".format(result.display_name)) - print( - "Predicted class score: {}".format( - result.image_object_detection.score - ) - ) - bounding_box = result.image_object_detection.bounding_box - print("Normalized Vertices:") - for vertex in bounding_box.normalized_vertices: - print("\tX: {}, Y: {}".format(vertex.x, vertex.y)) - # [END automl_vision_object_detection_predict] diff --git a/automl/cloud-client/vision_object_detection_predict_test.py b/automl/cloud-client/vision_object_detection_predict_test.py deleted file mode 100644 index 24532663ab2..00000000000 --- a/automl/cloud-client/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(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(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/tables/README.md b/tables/README.md new file mode 100644 index 00000000000..69cf0939fad --- /dev/null +++ b/tables/README.md @@ -0,0 +1 @@ +These samples have been moved to https://github.com/googleapis/python-automl/tree/master/samples/ \ No newline at end of file diff --git a/tables/automl/automl_tables_dataset.py b/tables/automl/automl_tables_dataset.py deleted file mode 100644 index 144f2ee6b65..00000000000 --- a/tables/automl/automl_tables_dataset.py +++ /dev/null @@ -1,306 +0,0 @@ -#!/usr/bin/env python - -# 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. - -"""This application demonstrates how to perform basic operations on dataset -with the Google AutoML Tables API. - -For more information, the documentation at -https://cloud.google.com/automl-tables/docs. -""" - -import argparse -import os - - -def create_dataset(project_id, compute_region, dataset_display_name): - """Create a dataset.""" - # [START automl_tables_create_dataset] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # dataset_display_name = 'DATASET_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Create a dataset with the given display name - dataset = client.create_dataset(dataset_display_name) - - # Display the dataset information. - print("Dataset name: {}".format(dataset.name)) - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print("Dataset display name: {}".format(dataset.display_name)) - print("Dataset metadata:") - print("\t{}".format(dataset.tables_dataset_metadata)) - print("Dataset example count: {}".format(dataset.example_count)) - print("Dataset create time:") - print("\tseconds: {}".format(dataset.create_time.seconds)) - print("\tnanos: {}".format(dataset.create_time.nanos)) - - # [END automl_tables_create_dataset] - - return dataset - - -def list_datasets(project_id, compute_region, filter_=None): - """List all datasets.""" - result = [] - # [START automl_tables_list_datasets] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # filter_ = 'filter expression here' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # List all the datasets available in the region by applying filter. - response = client.list_datasets(filter_=filter_) - - print("List of datasets:") - for dataset in response: - # Display the dataset information. - print("Dataset name: {}".format(dataset.name)) - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print("Dataset display name: {}".format(dataset.display_name)) - metadata = dataset.tables_dataset_metadata - print( - "Dataset primary table spec id: {}".format( - metadata.primary_table_spec_id - ) - ) - print( - "Dataset target column spec id: {}".format( - metadata.target_column_spec_id - ) - ) - print( - "Dataset target column spec id: {}".format( - metadata.target_column_spec_id - ) - ) - print( - "Dataset weight column spec id: {}".format( - metadata.weight_column_spec_id - ) - ) - print( - "Dataset ml use column spec id: {}".format( - metadata.ml_use_column_spec_id - ) - ) - print("Dataset example count: {}".format(dataset.example_count)) - print("Dataset create time:") - print("\tseconds: {}".format(dataset.create_time.seconds)) - print("\tnanos: {}".format(dataset.create_time.nanos)) - print("\n") - - # [END automl_tables_list_datasets] - result.append(dataset) - - return result - - -def get_dataset(project_id, compute_region, dataset_display_name): - """Get the dataset.""" - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # dataset_display_name = 'DATASET_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Get complete detail of the dataset. - dataset = client.get_dataset(dataset_display_name=dataset_display_name) - - # Display the dataset information. - print("Dataset name: {}".format(dataset.name)) - print("Dataset id: {}".format(dataset.name.split("/")[-1])) - print("Dataset display name: {}".format(dataset.display_name)) - print("Dataset metadata:") - print("\t{}".format(dataset.tables_dataset_metadata)) - print("Dataset example count: {}".format(dataset.example_count)) - print("Dataset create time:") - print("\tseconds: {}".format(dataset.create_time.seconds)) - print("\tnanos: {}".format(dataset.create_time.nanos)) - - return dataset - - -def import_data(project_id, compute_region, dataset_display_name, path): - """Import structured data.""" - # [START automl_tables_import_data] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # dataset_display_name = 'DATASET_DISPLAY_NAME' - # path = 'gs://path/to/file.csv' or 'bq://project_id.dataset.table_id' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - response = None - if path.startswith("bq"): - response = client.import_data( - dataset_display_name=dataset_display_name, bigquery_input_uri=path - ) - else: - # Get the multiple Google Cloud Storage URIs. - input_uris = path.split(",") - response = client.import_data( - dataset_display_name=dataset_display_name, - gcs_input_uris=input_uris, - ) - - print("Processing import...") - # synchronous check of operation status. - print("Data imported. {}".format(response.result())) - - # [END automl_tables_import_data] - - -def update_dataset( - project_id, - compute_region, - dataset_display_name, - target_column_spec_name=None, - weight_column_spec_name=None, - test_train_column_spec_name=None, -): - """Update dataset.""" - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # dataset_display_name = 'DATASET_DISPLAY_NAME_HERE' - # target_column_spec_name = 'TARGET_COLUMN_SPEC_NAME_HERE' or None - # weight_column_spec_name = 'WEIGHT_COLUMN_SPEC_NAME_HERE' or None - # test_train_column_spec_name = 'TEST_TRAIN_COLUMN_SPEC_NAME_HERE' or None - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - if target_column_spec_name is not None: - response = client.set_target_column( - dataset_display_name=dataset_display_name, - column_spec_display_name=target_column_spec_name, - ) - print("Target column updated. {}".format(response)) - if weight_column_spec_name is not None: - response = client.set_weight_column( - dataset_display_name=dataset_display_name, - column_spec_display_name=weight_column_spec_name, - ) - print("Weight column updated. {}".format(response)) - if test_train_column_spec_name is not None: - response = client.set_test_train_column( - dataset_display_name=dataset_display_name, - column_spec_display_name=test_train_column_spec_name, - ) - print("Test/train column updated. {}".format(response)) - - -def delete_dataset(project_id, compute_region, dataset_display_name): - """Delete a dataset""" - # [START automl_tables_delete_dataset] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # dataset_display_name = 'DATASET_DISPLAY_NAME_HERE - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Delete a dataset. - response = client.delete_dataset(dataset_display_name=dataset_display_name) - - # synchronous check of operation status. - print("Dataset deleted. {}".format(response.result())) - # [END automl_tables_delete_dataset] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter, - ) - subparsers = parser.add_subparsers(dest="command") - - create_dataset_parser = subparsers.add_parser( - "create_dataset", help=create_dataset.__doc__ - ) - create_dataset_parser.add_argument("--dataset_name") - - list_datasets_parser = subparsers.add_parser( - "list_datasets", help=list_datasets.__doc__ - ) - list_datasets_parser.add_argument("--filter_") - - get_dataset_parser = subparsers.add_parser( - "get_dataset", help=get_dataset.__doc__ - ) - get_dataset_parser.add_argument("--dataset_display_name") - - import_data_parser = subparsers.add_parser( - "import_data", help=import_data.__doc__ - ) - import_data_parser.add_argument("--dataset_display_name") - import_data_parser.add_argument("--path") - - update_dataset_parser = subparsers.add_parser( - "update_dataset", help=update_dataset.__doc__ - ) - update_dataset_parser.add_argument("--dataset_display_name") - update_dataset_parser.add_argument("--target_column_spec_name") - update_dataset_parser.add_argument("--weight_column_spec_name") - update_dataset_parser.add_argument("--ml_use_column_spec_name") - - delete_dataset_parser = subparsers.add_parser( - "delete_dataset", help=delete_dataset.__doc__ - ) - delete_dataset_parser.add_argument("--dataset_display_name") - - project_id = os.environ["PROJECT_ID"] - compute_region = os.environ["REGION_NAME"] - - args = parser.parse_args() - if args.command == "create_dataset": - create_dataset(project_id, compute_region, args.dataset_name) - if args.command == "list_datasets": - list_datasets(project_id, compute_region, args.filter_) - if args.command == "get_dataset": - get_dataset(project_id, compute_region, args.dataset_display_name) - if args.command == "import_data": - import_data( - project_id, compute_region, args.dataset_display_name, args.path - ) - if args.command == "update_dataset": - update_dataset( - project_id, - compute_region, - args.dataset_display_name, - args.target_column_spec_name, - args.weight_column_spec_name, - args.ml_use_column_spec_name, - ) - if args.command == "delete_dataset": - delete_dataset(project_id, compute_region, args.dataset_display_name) diff --git a/tables/automl/automl_tables_model.py b/tables/automl/automl_tables_model.py deleted file mode 100644 index a77dfe62d7a..00000000000 --- a/tables/automl/automl_tables_model.py +++ /dev/null @@ -1,514 +0,0 @@ -#!/usr/bin/env python - -# 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. - -"""This application demonstrates how to perform basic operations on model -with the Google AutoML Tables API. - -For more information, the documentation at -https://cloud.google.com/automl-tables/docs. -""" - -import argparse -import os - - -def create_model( - project_id, - compute_region, - dataset_display_name, - model_display_name, - train_budget_milli_node_hours, - include_column_spec_names=None, - exclude_column_spec_names=None, -): - """Create a model.""" - # [START automl_tables_create_model] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # dataset_display_name = 'DATASET_DISPLAY_NAME_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - # train_budget_milli_node_hours = 'TRAIN_BUDGET_MILLI_NODE_HOURS_HERE' - # include_column_spec_names = 'INCLUDE_COLUMN_SPEC_NAMES_HERE' - # or None if unspecified - # exclude_column_spec_names = 'EXCLUDE_COLUMN_SPEC_NAMES_HERE' - # or None if unspecified - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Create a model with the model metadata in the region. - response = client.create_model( - model_display_name, - train_budget_milli_node_hours=train_budget_milli_node_hours, - dataset_display_name=dataset_display_name, - include_column_spec_names=include_column_spec_names, - exclude_column_spec_names=exclude_column_spec_names, - ) - - print("Training model...") - print("Training operation name: {}".format(response.operation.name)) - print("Training completed: {}".format(response.result())) - - # [END automl_tables_create_model] - - -def get_operation_status(operation_full_id): - """Get operation status.""" - # [START automl_tables_get_operation_status] - # TODO(developer): Uncomment and set the following variables - # operation_full_id = - # 'projects//locations//operations/' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient() - - # Get the latest state of a long-running operation. - op = client.auto_ml_client.transport._operations_client.get_operation( - operation_full_id - ) - - print("Operation status: {}".format(op)) - - # [END automl_tables_get_operation_status] - - -def list_models(project_id, compute_region, filter_=None): - """List all models.""" - result = [] - # [START automl_tables_list_models] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # filter_ = 'DATASET_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - from google.cloud.automl_v1beta1 import enums - - client = automl.TablesClient(project=project_id, region=compute_region) - - # List all the models available in the region by applying filter. - response = client.list_models(filter_=filter_) - - print("List of models:") - for model in response: - # Retrieve deployment state. - if model.deployment_state == enums.Model.DeploymentState.DEPLOYED: - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - # Display the model information. - print("Model name: {}".format(model.name)) - print("Model id: {}".format(model.name.split("/")[-1])) - print("Model display name: {}".format(model.display_name)) - metadata = model.tables_model_metadata - print( - "Target column display name: {}".format( - metadata.target_column_spec.display_name - ) - ) - print( - "Training budget in node milli hours: {}".format( - metadata.train_budget_milli_node_hours - ) - ) - print( - "Training cost in node milli hours: {}".format( - metadata.train_cost_milli_node_hours - ) - ) - print("Model create time:") - print("\tseconds: {}".format(model.create_time.seconds)) - print("\tnanos: {}".format(model.create_time.nanos)) - print("Model deployment state: {}".format(deployment_state)) - print("\n") - - # [END automl_tables_list_models] - result.append(model) - - return result - - -def get_model(project_id, compute_region, model_display_name): - """Get model details.""" - # [START automl_tables_get_model] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - from google.cloud.automl_v1beta1 import enums - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Get complete detail of the model. - model = client.get_model(model_display_name=model_display_name) - - # Retrieve deployment state. - if model.deployment_state == enums.Model.DeploymentState.DEPLOYED: - deployment_state = "deployed" - else: - deployment_state = "undeployed" - - # get features of top importance - feat_list = [ - (column.feature_importance, column.column_display_name) - for column in model.tables_model_metadata.tables_model_column_info - ] - feat_list.sort(reverse=True) - if len(feat_list) < 10: - feat_to_show = len(feat_list) - else: - feat_to_show = 10 - - # Display the model information. - print("Model name: {}".format(model.name)) - print("Model id: {}".format(model.name.split("/")[-1])) - print("Model display name: {}".format(model.display_name)) - print("Features of top importance:") - for feat in feat_list[:feat_to_show]: - print(feat) - print("Model create time:") - print("\tseconds: {}".format(model.create_time.seconds)) - print("\tnanos: {}".format(model.create_time.nanos)) - print("Model deployment state: {}".format(deployment_state)) - - # [END automl_tables_get_model] - - return model - - -def list_model_evaluations( - project_id, compute_region, model_display_name, filter_=None -): - - """List model evaluations.""" - result = [] - # [START automl_tables_list_model_evaluations] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - # filter_ = 'filter expression here' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # List all the model evaluations in the model by applying filter. - response = client.list_model_evaluations( - model_display_name=model_display_name, filter_=filter_ - ) - - print("List of model evaluations:") - for evaluation in response: - print("Model evaluation name: {}".format(evaluation.name)) - print("Model evaluation id: {}".format(evaluation.name.split("/")[-1])) - print( - "Model evaluation example count: {}".format( - evaluation.evaluated_example_count - ) - ) - print("Model evaluation time:") - print("\tseconds: {}".format(evaluation.create_time.seconds)) - print("\tnanos: {}".format(evaluation.create_time.nanos)) - print("\n") - # [END automl_tables_list_model_evaluations] - result.append(evaluation) - - return result - - -def get_model_evaluation( - project_id, compute_region, model_id, model_evaluation_id -): - """Get model evaluation.""" - # [START automl_tables_get_model_evaluation] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_id = 'MODEL_ID_HERE' - # model_evaluation_id = 'MODEL_EVALUATION_ID_HERE' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient() - - # Get the full path of the model evaluation. - model_evaluation_full_id = client.auto_ml_client.model_evaluation_path( - project_id, compute_region, model_id, model_evaluation_id - ) - - # Get complete detail of the model evaluation. - response = client.get_model_evaluation( - model_evaluation_name=model_evaluation_full_id - ) - - print(response) - # [END automl_tables_get_model_evaluation] - return response - - -def display_evaluation( - project_id, compute_region, model_display_name, filter_=None -): - """Display evaluation.""" - # [START automl_tables_display_evaluation] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - # filter_ = 'filter expression here' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # List all the model evaluations in the model by applying filter. - response = client.list_model_evaluations( - model_display_name=model_display_name, filter_=filter_ - ) - - # Iterate through the results. - for evaluation in response: - # There is evaluation for each class in a model and for overall model. - # Get only the evaluation of overall model. - if not evaluation.annotation_spec_id: - model_evaluation_name = evaluation.name - break - - # Get a model evaluation. - model_evaluation = client.get_model_evaluation( - model_evaluation_name=model_evaluation_name - ) - - classification_metrics = model_evaluation.classification_evaluation_metrics - if str(classification_metrics): - confidence_metrics = classification_metrics.confidence_metrics_entry - - # Showing model score based on threshold of 0.5 - print("Model classification metrics (threshold at 0.5):") - for confidence_metrics_entry in confidence_metrics: - if confidence_metrics_entry.confidence_threshold == 0.5: - print( - "Model Precision: {}%".format( - round(confidence_metrics_entry.precision * 100, 2) - ) - ) - print( - "Model Recall: {}%".format( - round(confidence_metrics_entry.recall * 100, 2) - ) - ) - print( - "Model F1 score: {}%".format( - round(confidence_metrics_entry.f1_score * 100, 2) - ) - ) - print("Model AUPRC: {}".format(classification_metrics.au_prc)) - print("Model AUROC: {}".format(classification_metrics.au_roc)) - print("Model log loss: {}".format(classification_metrics.log_loss)) - - regression_metrics = model_evaluation.regression_evaluation_metrics - if str(regression_metrics): - print("Model regression metrics:") - print( - "Model RMSE: {}".format(regression_metrics.root_mean_squared_error) - ) - print("Model MAE: {}".format(regression_metrics.mean_absolute_error)) - print( - "Model MAPE: {}".format( - regression_metrics.mean_absolute_percentage_error - ) - ) - print("Model R^2: {}".format(regression_metrics.r_squared)) - - # [END automl_tables_display_evaluation] - - -def deploy_model(project_id, compute_region, model_display_name): - """Deploy model.""" - # [START automl_tables_deploy_model] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Deploy model - response = client.deploy_model(model_display_name=model_display_name) - - # synchronous check of operation status. - print("Model deployed. {}".format(response.result())) - - # [END automl_tables_deploy_model] - - -def undeploy_model(project_id, compute_region, model_display_name): - """Undeploy model.""" - # [START automl_tables_undeploy_model] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Undeploy model - response = client.undeploy_model(model_display_name=model_display_name) - - # synchronous check of operation status. - print("Model undeployed. {}".format(response.result())) - - # [END automl_tables_undeploy_model] - - -def delete_model(project_id, compute_region, model_display_name): - """Delete a model.""" - # [START automl_tables_delete_model] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Undeploy model - response = client.delete_model(model_display_name=model_display_name) - - # synchronous check of operation status. - print("Model deleted. {}".format(response.result())) - - # [END automl_tables_delete_model] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter, - ) - subparsers = parser.add_subparsers(dest="command") - - create_model_parser = subparsers.add_parser( - "create_model", help=create_model.__doc__ - ) - create_model_parser.add_argument("--dataset_display_name") - create_model_parser.add_argument("--model_display_name") - create_model_parser.add_argument( - "--train_budget_milli_node_hours", type=int - ) - - get_operation_status_parser = subparsers.add_parser( - "get_operation_status", help=get_operation_status.__doc__ - ) - get_operation_status_parser.add_argument("--operation_full_id") - - list_models_parser = subparsers.add_parser( - "list_models", help=list_models.__doc__ - ) - list_models_parser.add_argument("--filter_") - - get_model_parser = subparsers.add_parser( - "get_model", help=get_model.__doc__ - ) - get_model_parser.add_argument("--model_display_name") - - list_model_evaluations_parser = subparsers.add_parser( - "list_model_evaluations", help=list_model_evaluations.__doc__ - ) - list_model_evaluations_parser.add_argument("--model_display_name") - list_model_evaluations_parser.add_argument("--filter_") - - get_model_evaluation_parser = subparsers.add_parser( - "get_model_evaluation", help=get_model_evaluation.__doc__ - ) - get_model_evaluation_parser.add_argument("--model_id") - get_model_evaluation_parser.add_argument("--model_evaluation_id") - - display_evaluation_parser = subparsers.add_parser( - "display_evaluation", help=display_evaluation.__doc__ - ) - display_evaluation_parser.add_argument("--model_display_name") - display_evaluation_parser.add_argument("--filter_") - - deploy_model_parser = subparsers.add_parser( - "deploy_model", help=deploy_model.__doc__ - ) - deploy_model_parser.add_argument("--model_display_name") - - undeploy_model_parser = subparsers.add_parser( - "undeploy_model", help=undeploy_model.__doc__ - ) - undeploy_model_parser.add_argument("--model_display_name") - - delete_model_parser = subparsers.add_parser( - "delete_model", help=delete_model.__doc__ - ) - delete_model_parser.add_argument("--model_display_name") - - project_id = os.environ["PROJECT_ID"] - compute_region = os.environ["REGION_NAME"] - - args = parser.parse_args() - - if args.command == "create_model": - create_model( - project_id, - compute_region, - args.dataset_display_name, - args.model_display_name, - args.train_budget_milli_node_hours, - # Input columns are omitted here as argparse does not support - # column spec objects, but it is still included in function def. - ) - if args.command == "get_operation_status": - get_operation_status(args.operation_full_id) - if args.command == "list_models": - list_models(project_id, compute_region, args.filter_) - if args.command == "get_model": - get_model(project_id, compute_region, args.model_display_name) - if args.command == "list_model_evaluations": - list_model_evaluations( - project_id, compute_region, args.model_display_name, args.filter_ - ) - if args.command == "get_model_evaluation": - get_model_evaluation( - project_id, - compute_region, - args.model_display_name, - args.model_evaluation_id, - ) - if args.command == "display_evaluation": - display_evaluation( - project_id, compute_region, args.model_display_name, args.filter_ - ) - if args.command == "deploy_model": - deploy_model(project_id, compute_region, args.model_display_name) - if args.command == "undeploy_model": - undeploy_model(project_id, compute_region, args.model_display_name) - if args.command == "delete_model": - delete_model(project_id, compute_region, args.model_display_name) diff --git a/tables/automl/automl_tables_predict.py b/tables/automl/automl_tables_predict.py deleted file mode 100644 index e965427258a..00000000000 --- a/tables/automl/automl_tables_predict.py +++ /dev/null @@ -1,203 +0,0 @@ -#!/usr/bin/env python - -# 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. - -"""This application demonstrates how to perform basic operations on prediction -with the Google AutoML Tables API. - -For more information, the documentation at -https://cloud.google.com/automl-tables/docs. -""" - -import argparse -import os - - -def predict( - project_id, - compute_region, - model_display_name, - inputs, - feature_importance=None, -): - """Make a prediction.""" - # [START automl_tables_predict] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - # inputs = {'value': 3, ...} - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - if feature_importance: - response = client.predict( - model_display_name=model_display_name, - inputs=inputs, - feature_importance=True, - ) - else: - response = client.predict( - model_display_name=model_display_name, inputs=inputs - ) - - print("Prediction results:") - for result in response.payload: - print( - "Predicted class name: {}".format(result.tables.value.string_value) - ) - print("Predicted class score: {}".format(result.tables.score)) - - if feature_importance: - # get features of top importance - feat_list = [ - (column.feature_importance, column.column_display_name) - for column in result.tables.tables_model_column_info - ] - feat_list.sort(reverse=True) - if len(feat_list) < 10: - feat_to_show = len(feat_list) - else: - feat_to_show = 10 - - print("Features of top importance:") - for feat in feat_list[:feat_to_show]: - print(feat) - - # [END automl_tables_predict] - - -def batch_predict_bq( - project_id, - compute_region, - model_display_name, - bq_input_uri, - bq_output_uri, -): - """Make a batch of predictions.""" - # [START automl_tables_batch_predict_bq] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - # bq_input_uri = 'bq://my-project.my-dataset.my-table' - # bq_output_uri = 'bq://my-project' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Query model - response = client.batch_predict(bigquery_input_uri=bq_input_uri, - bigquery_output_uri=bq_output_uri, - model_display_name=model_display_name) - print("Making batch prediction... ") - # `response` is a async operation descriptor, - # you can register a callback for the operation to complete via `add_done_callback`: - # def callback(operation_future): - # result = operation_future.result() - # response.add_done_callback(callback) - # - # or block the thread polling for the operation's results: - response.result() - # AutoML puts predictions in a newly generated dataset with a name by a mask "prediction_" + model_id + "_" + timestamp - # here's how to get the dataset name: - dataset_name = response.metadata.batch_predict_details.output_info.bigquery_output_dataset - - print("Batch prediction complete.\nResults are in '{}' dataset.\n{}".format( - dataset_name, response.metadata)) - - # [END automl_tables_batch_predict_bq] - - -def batch_predict( - project_id, - compute_region, - model_display_name, - gcs_input_uri, - gcs_output_uri, -): - """Make a batch of predictions.""" - # [START automl_tables_batch_predict] - # TODO(developer): Uncomment and set the following variables - # project_id = 'PROJECT_ID_HERE' - # compute_region = 'COMPUTE_REGION_HERE' - # model_display_name = 'MODEL_DISPLAY_NAME_HERE' - # gcs_input_uri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv' - # gcs_output_uri = 'gs://YOUR_BUCKET_ID/path_to_save_results/' - - from google.cloud import automl_v1beta1 as automl - - client = automl.TablesClient(project=project_id, region=compute_region) - - # Query model - response = client.batch_predict( - gcs_input_uris=gcs_input_uri, - gcs_output_uri_prefix=gcs_output_uri, - model_display_name=model_display_name, - ) - print("Making batch prediction... ") - # `response` is a async operation descriptor, - # you can register a callback for the operation to complete via `add_done_callback`: - # def callback(operation_future): - # result = operation_future.result() - # response.add_done_callback(callback) - # - # or block the thread polling for the operation's results: - response.result() - - print("Batch prediction complete.\n{}".format(response.metadata)) - - # [END automl_tables_batch_predict] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter, - ) - subparsers = parser.add_subparsers(dest="command") - - predict_parser = subparsers.add_parser("predict", help=predict.__doc__) - predict_parser.add_argument("--model_display_name") - predict_parser.add_argument("--file_path") - - batch_predict_parser = subparsers.add_parser( - "batch_predict", help=predict.__doc__ - ) - batch_predict_parser.add_argument("--model_display_name") - batch_predict_parser.add_argument("--input_path") - batch_predict_parser.add_argument("--output_path") - - project_id = os.environ["PROJECT_ID"] - compute_region = os.environ["REGION_NAME"] - - args = parser.parse_args() - - if args.command == "predict": - predict( - project_id, compute_region, args.model_display_name, args.file_path - ) - - if args.command == "batch_predict": - batch_predict( - project_id, - compute_region, - args.model_display_name, - args.input_path, - args.output_path, - ) diff --git a/tables/automl/automl_tables_set_endpoint.py b/tables/automl/automl_tables_set_endpoint.py deleted file mode 100644 index d6ab898b4f5..00000000000 --- a/tables/automl/automl_tables_set_endpoint.py +++ /dev/null @@ -1,33 +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_client_with_endpoint(gcp_project_id): - """Create a Tables client with a non-default endpoint.""" - # [START automl_set_endpoint] - from google.cloud import automl_v1beta1 as automl - from google.api_core.client_options import ClientOptions - - # Set the endpoint you want to use via the ClientOptions. - # gcp_project_id = 'YOUR_PROJECT_ID' - client_options = ClientOptions(api_endpoint="eu-automl.googleapis.com:443") - client = automl.TablesClient( - project=gcp_project_id, region="eu", client_options=client_options - ) - # [END automl_set_endpoint] - - # do simple test to check client connectivity - print(client.list_datasets()) - - return client diff --git a/tables/automl/batch_predict_test.py b/tables/automl/batch_predict_test.py deleted file mode 100644 index f77404deefd..00000000000 --- a/tables/automl/batch_predict_test.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -from google.cloud.automl_v1beta1.gapic import enums - -import pytest - -import automl_tables_model -import automl_tables_predict -import model_test - - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -REGION = "us-central1" -STATIC_MODEL = model_test.STATIC_MODEL -GCS_INPUT = "gs://{}-automl-tables-test/bank-marketing.csv".format(PROJECT) -GCS_OUTPUT = "gs://{}-automl-tables-test/TABLE_TEST_OUTPUT/".format(PROJECT) -BQ_INPUT = "bq://{}.automl_test.bank_marketing".format(PROJECT) -BQ_OUTPUT = "bq://{}".format(PROJECT) - - -@pytest.mark.slow -def test_batch_predict(capsys): - ensure_model_online() - automl_tables_predict.batch_predict( - PROJECT, REGION, STATIC_MODEL, GCS_INPUT, GCS_OUTPUT - ) - out, _ = capsys.readouterr() - assert "Batch prediction complete" in out - - -@pytest.mark.slow -def test_batch_predict_bq(capsys): - ensure_model_online() - automl_tables_predict.batch_predict_bq( - PROJECT, REGION, STATIC_MODEL, BQ_INPUT, BQ_OUTPUT - ) - out, _ = capsys.readouterr() - assert "Batch prediction complete" in out - - -def ensure_model_online(): - model = model_test.ensure_model_ready() - if model.deployment_state != enums.Model.DeploymentState.DEPLOYED: - automl_tables_model.deploy_model(PROJECT, REGION, model.display_name) - - return automl_tables_model.get_model(PROJECT, REGION, model.display_name) diff --git a/tables/automl/dataset_test.py b/tables/automl/dataset_test.py deleted file mode 100644 index 27570f0bee9..00000000000 --- a/tables/automl/dataset_test.py +++ /dev/null @@ -1,112 +0,0 @@ -#!/usr/bin/env python - -# 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 -import random -import string -import time - -from google.api_core import exceptions -import pytest - -import automl_tables_dataset - - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -REGION = "us-central1" -STATIC_DATASET = "do_not_delete_this_table_python" -GCS_DATASET = ("gs://python-docs-samples-tests-automl-tables-test" - "/bank-marketing.csv") - -ID = "{rand}_{time}".format( - rand="".join( - [random.choice(string.ascii_letters + string.digits) for n in range(4)] - ), - time=int(time.time()), -) - - -def _id(name): - return "{}_{}".format(name, ID) - - -def ensure_dataset_ready(): - dataset = None - name = STATIC_DATASET - try: - dataset = automl_tables_dataset.get_dataset(PROJECT, REGION, name) - except exceptions.NotFound: - dataset = automl_tables_dataset.create_dataset(PROJECT, REGION, name) - - if dataset.example_count is None or dataset.example_count == 0: - automl_tables_dataset.import_data(PROJECT, REGION, name, GCS_DATASET) - dataset = automl_tables_dataset.get_dataset(PROJECT, REGION, name) - - automl_tables_dataset.update_dataset( - PROJECT, - REGION, - dataset.display_name, - target_column_spec_name="Deposit", - ) - - return dataset - - -@pytest.mark.slow -def test_dataset_create_import_delete(capsys): - name = _id("d_cr_dl") - dataset = automl_tables_dataset.create_dataset(PROJECT, REGION, name) - assert dataset is not None - assert dataset.display_name == name - - automl_tables_dataset.import_data(PROJECT, REGION, name, GCS_DATASET) - - out, _ = capsys.readouterr() - assert "Data imported." in out - - automl_tables_dataset.delete_dataset(PROJECT, REGION, name) - - with pytest.raises(exceptions.NotFound): - automl_tables_dataset.get_dataset(PROJECT, REGION, name) - - -def test_dataset_update(capsys): - dataset = ensure_dataset_ready() - automl_tables_dataset.update_dataset( - PROJECT, - REGION, - dataset.display_name, - target_column_spec_name="Deposit", - weight_column_spec_name="Balance", - ) - - out, _ = capsys.readouterr() - assert "Target column updated." in out - assert "Weight column updated." in out - - -def test_list_datasets(): - ensure_dataset_ready() - assert ( - next( - ( - d - for d in automl_tables_dataset.list_datasets(PROJECT, REGION) - if d.display_name == STATIC_DATASET - ), - None, - ) - is not None - ) diff --git a/tables/automl/endpoint_test.py b/tables/automl/endpoint_test.py deleted file mode 100644 index 5a20aba5c48..00000000000 --- a/tables/automl/endpoint_test.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python - -# 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 automl_tables_set_endpoint - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] - - -def test_client_creation(capsys): - automl_tables_set_endpoint.create_client_with_endpoint(PROJECT) - out, _ = capsys.readouterr() - assert "GRPCIterator" in out diff --git a/tables/automl/model_test.py b/tables/automl/model_test.py deleted file mode 100644 index 484eaf82487..00000000000 --- a/tables/automl/model_test.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env python - -# 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 -import random -import string -import time - -from google.api_core import exceptions - -import automl_tables_model -import dataset_test - - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -REGION = "us-central1" -STATIC_MODEL = "do_not_delete_this_model_0" -GCS_DATASET = "gs://cloud-ml-tables-data/bank-marketing.csv" - -ID = "{rand}_{time}".format( - rand="".join( - [random.choice(string.ascii_letters + string.digits) for n in range(4)] - ), - time=int(time.time()), -) - - -def _id(name): - return "{}_{}".format(name, ID) - - -def test_list_models(): - ensure_model_ready() - assert ( - next( - ( - m - for m in automl_tables_model.list_models(PROJECT, REGION) - if m.display_name == STATIC_MODEL - ), - None, - ) - is not None - ) - - -def test_list_model_evaluations(): - model = ensure_model_ready() - mes = automl_tables_model.list_model_evaluations( - PROJECT, REGION, model.display_name - ) - assert len(mes) > 0 - for me in mes: - assert me.name.startswith(model.name) - - -def test_get_model_evaluations(): - model = ensure_model_ready() - me = automl_tables_model.list_model_evaluations( - PROJECT, REGION, model.display_name - )[0] - mep = automl_tables_model.get_model_evaluation( - PROJECT, - REGION, - model.name.rpartition("/")[2], - me.name.rpartition("/")[2], - ) - - assert mep.name == me.name - - -def ensure_model_ready(): - name = STATIC_MODEL - try: - return automl_tables_model.get_model(PROJECT, REGION, name) - except exceptions.NotFound: - pass - - dataset = dataset_test.ensure_dataset_ready() - return automl_tables_model.create_model( - PROJECT, REGION, dataset.display_name, name, 1000 - ) diff --git a/tables/automl/predict_test.py b/tables/automl/predict_test.py deleted file mode 100644 index d608e182f1f..00000000000 --- a/tables/automl/predict_test.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python - -# 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.cloud.automl_v1beta1.gapic import enums - -import automl_tables_model -import automl_tables_predict -import model_test - - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -REGION = "us-central1" -STATIC_MODEL = model_test.STATIC_MODEL - - -def test_predict(capsys): - inputs = { - "Age": 31, - "Balance": 200, - "Campaign": 2, - "Contact": "cellular", - "Day": "4", - "Default": "no", - "Duration": 12, - "Education": "primary", - "Housing": "yes", - "Job": "blue-collar", - "Loan": "no", - "MaritalStatus": "divorced", - "Month": "jul", - "PDays": 4, - "POutcome": "0", - "Previous": 12, - } - - ensure_model_online() - automl_tables_predict.predict(PROJECT, REGION, STATIC_MODEL, inputs, True) - out, _ = capsys.readouterr() - assert "Predicted class name:" in out - assert "Predicted class score:" in out - assert "Features of top importance:" in out - - -def ensure_model_online(): - model = model_test.ensure_model_ready() - if model.deployment_state != enums.Model.DeploymentState.DEPLOYED: - automl_tables_model.deploy_model(PROJECT, REGION, model.display_name) - - return automl_tables_model.get_model(PROJECT, REGION, model.display_name) diff --git a/tables/automl/requirements-test.txt b/tables/automl/requirements-test.txt deleted file mode 100644 index 7e460c8c866..00000000000 --- a/tables/automl/requirements-test.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==6.0.1 diff --git a/tables/automl/requirements.txt b/tables/automl/requirements.txt deleted file mode 100644 index 867dfc61e77..00000000000 --- a/tables/automl/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-automl==1.0.1