Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Update datalabeling samples to hit test endpoint. [(#2641)](GoogleClo…
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg authored and Doug Mahugh committed Dec 21, 2019
1 parent 92150bb commit b2e9343
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 2 deletions.
9 changes: 9 additions & 0 deletions samples/snippets/create_annotation_spec_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# limitations under the License.

import argparse
import os
from google.api_core.client_options import ClientOptions


# [START datalabeling_create_annotation_spec_set_beta]
Expand All @@ -24,6 +26,13 @@ def create_annotation_spec_set(project_id):
"""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_create_annotation_spec_set_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)
# [START datalabeling_create_annotation_spec_set_beta]

project_path = client.project_path(project_id)

Expand Down
8 changes: 8 additions & 0 deletions samples/snippets/create_annotation_spec_set_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import create_annotation_spec_set
from google.cloud import datalabeling_v1beta1 as datalabeling
from google.api_core.client_options import ClientOptions
import pytest

PROJECT_ID = os.getenv('GCLOUD_PROJECT')
Expand All @@ -33,4 +34,11 @@ def test_create_annotation_spec_set(capsys):
# Delete the created annotation spec set.
annotation_spec_set_name = response.name
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_annotation_spec_set(annotation_spec_set_name)
9 changes: 9 additions & 0 deletions samples/snippets/create_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# limitations under the License.

import argparse
import os
from google.api_core.client_options import ClientOptions


# [START datalabeling_create_instruction_beta]
Expand All @@ -25,6 +27,13 @@ def create_instruction(project_id, data_type, instruction_gcs_uri):
"""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_create_instruction_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)
# [START datalabeling_create_instruction_beta]

project_path = client.project_path(project_id)

Expand Down
8 changes: 8 additions & 0 deletions samples/snippets/create_instruction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os

import create_instruction
from google.api_core.client_options import ClientOptions
from google.cloud import datalabeling_v1beta1 as datalabeling
import pytest

Expand All @@ -38,4 +39,11 @@ def test_create_instruction(capsys):
# Delete the created instruction.
instruction_name = result.name
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_instruction(instruction_name)
8 changes: 8 additions & 0 deletions samples/snippets/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# limitations under the License.

import argparse
import os
from google.api_core.client_options import ClientOptions


# [START datalabeling_export_data_beta]
Expand All @@ -23,6 +25,12 @@ def export_data(dataset_resource_name, annotated_dataset_resource_name,
"""Exports a dataset from the given Google Cloud project."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_export_data_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
# [START datalabeling_export_data_beta]

gcs_destination = datalabeling.types.GcsDestination(
output_uri=export_gcs_uri, mime_type='text/csv')
Expand Down
8 changes: 8 additions & 0 deletions samples/snippets/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@
# limitations under the License.

import argparse
import os
from google.api_core.client_options import ClientOptions


# [START datalabeling_import_data_beta]
def import_data(dataset_resource_name, data_type, input_gcs_uri):
"""Imports data to the given Google Cloud project and dataset."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_import_data_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
# [START datalabeling_import_data_beta]

gcs_source = datalabeling.types.GcsSource(
input_uri=input_gcs_uri, mime_type='text/csv')
Expand Down
8 changes: 8 additions & 0 deletions samples/snippets/label_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# limitations under the License.

import argparse
import os
from google.api_core.client_options import ClientOptions


# [START datalabeling_label_image_beta]
Expand All @@ -23,6 +25,12 @@ def label_image(dataset_resource_name, instruction_resource_name,
"""Labels an image dataset."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_label_image_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
# [START datalabeling_label_image_beta]

basic_config = datalabeling.types.HumanAnnotationConfig(
instruction=instruction_resource_name,
Expand Down
22 changes: 22 additions & 0 deletions samples/snippets/label_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import create_annotation_spec_set
import create_instruction
from google.api_core.client_options import ClientOptions
from google.cloud import datalabeling_v1beta1 as datalabeling
import import_data
import label_image
Expand Down Expand Up @@ -52,6 +53,13 @@ def annotation_spec_set():

# tear down
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_annotation_spec_set(response.name)


Expand All @@ -66,6 +74,13 @@ def instruction():

# tear down
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_instruction(instruction.name)


Expand All @@ -89,5 +104,12 @@ def test_label_image(capsys, annotation_spec_set, instruction, dataset):
assert response.cancelled() is True

client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.transport._operations_client.cancel_operation(
operation_name)
9 changes: 8 additions & 1 deletion samples/snippets/label_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
# limitations under the License.

import argparse

import os
from google.api_core.client_options import ClientOptions

# [START datalabeling_label_text_beta]
def label_text(dataset_resource_name, instruction_resource_name,
annotation_spec_set_resource_name):
"""Labels a text dataset."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_export_data_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
# [START datalabeling_export_data_beta]

basic_config = datalabeling.types.HumanAnnotationConfig(
instruction=instruction_resource_name,
Expand Down
22 changes: 22 additions & 0 deletions samples/snippets/label_text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import create_annotation_spec_set
import create_instruction
from google.cloud import datalabeling_v1beta1 as datalabeling
from google.api_core.client_options import ClientOptions
import import_data
import label_text
import manage_dataset
Expand Down Expand Up @@ -52,6 +53,13 @@ def annotation_spec_set():

# tear down
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_annotation_spec_set(response.name)


Expand All @@ -66,6 +74,13 @@ def instruction():

# tear down
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_instruction(instruction.name)


Expand All @@ -89,5 +104,12 @@ def test_label_text(capsys, annotation_spec_set, instruction, dataset):
assert response.cancelled() is True

client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.transport._operations_client.cancel_operation(
operation_name)
9 changes: 8 additions & 1 deletion samples/snippets/label_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
# limitations under the License.

import argparse

import os
from google.api_core.client_options import ClientOptions

# [START datalabeling_label_video_beta]
def label_video(dataset_resource_name, instruction_resource_name,
annotation_spec_set_resource_name):
"""Labels a video dataset."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_export_data_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
# [START datalabeling_export_data_beta]

basic_config = datalabeling.types.HumanAnnotationConfig(
instruction=instruction_resource_name,
Expand Down
22 changes: 22 additions & 0 deletions samples/snippets/label_video_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import create_annotation_spec_set
import create_instruction
from google.api_core.client_options import ClientOptions
from google.cloud import datalabeling_v1beta1 as datalabeling
import import_data
import label_video
Expand Down Expand Up @@ -52,6 +53,13 @@ def annotation_spec_set():

# tear down
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_annotation_spec_set(response.name)


Expand All @@ -66,6 +74,13 @@ def instruction():

# tear down
client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.delete_instruction(instruction.name)


Expand All @@ -89,5 +104,12 @@ def test_label_video(capsys, annotation_spec_set, instruction, dataset):
assert response.cancelled() is True

client = datalabeling.DataLabelingServiceClient()

# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)

client.transport._operations_client.cancel_operation(
operation_name)
8 changes: 8 additions & 0 deletions samples/snippets/manage_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@
# limitations under the License.

import argparse
import os
from google.api_core.client_options import ClientOptions


# [START datalabeling_create_dataset_beta]
def create_dataset(project_id):
"""Creates a dataset for the given Google Cloud project."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
# [END datalabeling_create_dataset_beta]
# If provided, use a provided test endpoint - this will prevent tests on
# this snippet from triggering any action by a real human
if 'DATALABELING_ENDPOINT' in os.environ:
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
# [START datalabeling_create_dataset_beta]

formatted_project_name = client.project_path(project_id)

Expand Down

0 comments on commit b2e9343

Please sign in to comment.