Skip to content

Commit 076c421

Browse files
committed
automl: video beta samples move from branch to master
1 parent a3432a1 commit 076c421

File tree

6 files changed

+232
-0
lines changed

6 files changed

+232
-0
lines changed

automl/beta/delete_dataset.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def delete_dataset(project_id, dataset_id):
17+
"""Delete a dataset."""
18+
# [START automl_delete_dataset_beta]
19+
from google.cloud import automl_v1beta1 as automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# dataset_id = "YOUR_DATASET_ID"
24+
25+
client = automl.AutoMlClient()
26+
# Get the full path of the dataset
27+
dataset_full_id = client.dataset_path(
28+
project_id, "us-central1", dataset_id
29+
)
30+
response = client.delete_dataset(dataset_full_id)
31+
32+
print("Dataset deleted. {}".format(response.result()))
33+
# [END automl_delete_dataset_beta]

automl/beta/delete_dataset_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import os
17+
18+
from google.cloud import automl_v1beta1 as automl
19+
import pytest
20+
21+
import delete_dataset
22+
23+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
24+
BUCKET_ID = "{}-lcm".format(PROJECT_ID)
25+
26+
27+
@pytest.fixture(scope="function")
28+
def dataset_id():
29+
client = automl.AutoMlClient()
30+
project_location = client.location_path(PROJECT_ID, "us-central1")
31+
display_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
32+
metadata = automl.types.TextExtractionDatasetMetadata()
33+
dataset = automl.types.Dataset(
34+
display_name=display_name, text_extraction_dataset_metadata=metadata
35+
)
36+
response = client.create_dataset(project_location, dataset)
37+
dataset_id = response.result().name.split("/")[-1]
38+
39+
yield dataset_id
40+
41+
42+
def test_delete_dataset(capsys, dataset_id):
43+
# delete dataset
44+
delete_dataset.delete_dataset(PROJECT_ID, dataset_id)
45+
out, _ = capsys.readouterr()
46+
assert "Dataset deleted." in out

automl/beta/import_dataset.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def import_dataset(project_id, dataset_id, path):
17+
"""Import a dataset."""
18+
# [START automl_import_data_beta]
19+
from google.cloud import automl_v1beta1 as automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
# dataset_id = "YOUR_DATASET_ID"
24+
# path = "gs://YOUR_BUCKET_ID/path/to/data.csv"
25+
26+
client = automl.AutoMlClient()
27+
# Get the full path of the dataset.
28+
dataset_full_id = client.dataset_path(
29+
project_id, "us-central1", dataset_id
30+
)
31+
# Get the multiple Google Cloud Storage URIs
32+
input_uris = path.split(",")
33+
gcs_source = automl.types.GcsSource(input_uris=input_uris)
34+
input_config = automl.types.InputConfig(gcs_source=gcs_source)
35+
# Import data from the input URI
36+
response = client.import_data(dataset_full_id, input_config)
37+
38+
print("Processing import...")
39+
print("Data imported. {}".format(response.result()))
40+
# [END automl_import_data_beta]

automl/beta/import_dataset_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import import_dataset
18+
19+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
20+
BUCKET_ID = "{}-lcm".format(PROJECT_ID)
21+
DATASET_ID = "TEN0000000000000000000"
22+
23+
24+
def test_import_dataset(capsys):
25+
# As importing a dataset can take a long time and only four operations can
26+
# be run on a dataset at once. Try to import into a nonexistent dataset and
27+
# confirm that the dataset was not found, but other elements of the request
28+
# were valid.
29+
try:
30+
data = "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID)
31+
import_dataset.import_dataset(PROJECT_ID, DATASET_ID, data)
32+
out, _ = capsys.readouterr()
33+
assert (
34+
"The Dataset doesn't exist or is inaccessible for use with AutoMl."
35+
in out
36+
)
37+
except Exception as e:
38+
assert (
39+
"The Dataset doesn't exist or is inaccessible for use with AutoMl."
40+
in e.message
41+
)

automl/beta/list_datasets.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def list_datasets(project_id):
17+
"""List datasets."""
18+
# [START automl_video_classification_list_datasets_beta]
19+
from google.cloud import automl_v1beta1 as automl
20+
21+
# TODO(developer): Uncomment and set the following variables
22+
# project_id = "YOUR_PROJECT_ID"
23+
24+
client = automl.AutoMlClient()
25+
# A resource that represents Google Cloud Platform location.
26+
project_location = client.location_path(project_id, "us-central1")
27+
28+
# List all the datasets available in the region.
29+
response = client.list_datasets(project_location, "")
30+
31+
print("List of datasets:")
32+
for dataset in response:
33+
print("Dataset name: {}".format(dataset.name))
34+
print("Dataset id: {}".format(dataset.name.split("/")[-1]))
35+
print("Dataset display name: {}".format(dataset.display_name))
36+
print("Dataset create time:")
37+
print("\tseconds: {}".format(dataset.create_time.seconds))
38+
print("\tnanos: {}".format(dataset.create_time.nanos))
39+
40+
print(
41+
"Video classification dataset metadata:: {}".format(
42+
dataset.video_classification_dataset_metadata
43+
)
44+
)
45+
# [END automl_video_classification_list_datasets_beta]

automl/beta/list_datasets_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import list_datasets
18+
19+
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"]
20+
DATASET_ID = os.environ["ENTITY_EXTRACTION_DATASET_ID"]
21+
22+
23+
def test_list_dataset(capsys):
24+
# list datasets
25+
list_datasets.list_datasets(PROJECT_ID)
26+
out, _ = capsys.readouterr()
27+
assert "Dataset id: {}".format(DATASET_ID) in out

0 commit comments

Comments
 (0)