diff --git a/automl/beta/video_classification_create_dataset.py b/automl/beta/video_classification_create_dataset.py index 19bb271b9d6c..086f98f05f43 100644 --- a/automl/beta/video_classification_create_dataset.py +++ b/automl/beta/video_classification_create_dataset.py @@ -13,14 +13,14 @@ # limitations under the License. -def create_dataset(project_id, display_name): - """Create a dataset.""" - # [START automl_video_classification_create_dataset_beta] - from google.cloud import automl_v1beta1 as automl +# [START automl_video_classification_create_dataset_beta] +from google.cloud import automl_v1beta1 as automl - # TODO(developer): Uncomment and set the following variables - # project_id = "YOUR_PROJECT_ID" - # display_name = "your_datasets_display_name" + +def create_dataset( + project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name" +): + """Create a automl video classification dataset.""" client = automl.AutoMlClient() @@ -37,9 +37,10 @@ def create_dataset(project_id, display_name): # 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] +# [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 index 2851e42aa214..443f504251a0 100644 --- a/automl/beta/video_classification_create_dataset_test.py +++ b/automl/beta/video_classification_create_dataset_test.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime import os +import uuid from google.cloud import automl_v1beta1 as automl import pytest @@ -22,7 +22,7 @@ PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -pytest.DATASET_ID = None +DATASET_ID = None @pytest.fixture(scope="function", autouse=True) @@ -32,7 +32,7 @@ def teardown(): # Delete the created dataset client = automl.AutoMlClient() dataset_full_id = client.dataset_path( - PROJECT_ID, "us-central1", pytest.DATASET_ID + PROJECT_ID, "us-central1", DATASET_ID ) response = client.delete_dataset(dataset_full_id) response.result() @@ -40,12 +40,13 @@ def teardown(): def test_video_classification_create_dataset(capsys): # create dataset - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + 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 the created dataset id for deletion - pytest.DATASET_ID = out.splitlines()[1].split()[2] + # 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 index 7fbdfe738e72..5bf19b4e2c51 100644 --- a/automl/beta/video_classification_create_model.py +++ b/automl/beta/video_classification_create_model.py @@ -12,21 +12,21 @@ # 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, dataset_id, display_name): - """Create a model.""" - # [START automl_video_classification_create_model_beta] - from google.cloud import automl_v1beta1 as 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" +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, @@ -39,4 +39,4 @@ def create_model(project_id, dataset_id, display_name): print("Training operation name: {}".format(response.operation.name)) print("Training started...") - # [END automl_video_classification_create_model_beta] +# [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 index a91363007639..25acb7d6bd0c 100644 --- a/automl/beta/video_classification_create_model_test.py +++ b/automl/beta/video_classification_create_model_test.py @@ -13,6 +13,7 @@ # limitations under the License. import os +import uuid from google.cloud import automl_v1beta1 as automl import pytest @@ -21,26 +22,26 @@ PROJECT_ID = os.environ["GCLOUD_PROJECT"] DATASET_ID = "VCN510437278078730240" -pytest.OPERATION_ID = None +OPERATION_ID = None @pytest.fixture(scope="function", autouse=True) def teardown(): yield - # Cancel the operation + # Cancel the training operation client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(pytest.OPERATION_ID) + 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, "classification_test_create_model" + PROJECT_ID, DATASET_ID, model_name ) out, _ = capsys.readouterr() assert "Training started" in out - # Get the the operation id for cancellation - pytest.OPERATION_ID = out.split("Training operation name: ")[1].split( - "\n" - )[0] + # Cancel the operation + global OPERATION_ID + OPERATION_ID = out.split("Training operation name: ")[1].split("\n")[0]