Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update datalabeling samples to use test endpoint. #2641

Merged
merged 1 commit into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .kokoro/tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ SECRETS_PASSWORD=$(cat "${KOKORO_GFILE_DIR}/secrets-password.txt")
source ./testing/test-env.sh
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json
export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json
# For Datalabeling samples to hit the testing endpoint
export DATALABELING_ENDPOINT="test-datalabeling.sandbox.googleapis.com:443"

# Run Cloud SQL proxy (background process exit when script does)
wget --quiet https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy && chmod +x cloud_sql_proxy
Expand Down
9 changes: 9 additions & 0 deletions datalabeling/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 datalabeling/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)
crwilcox marked this conversation as resolved.
Show resolved Hide resolved

client.delete_annotation_spec_set(annotation_spec_set_name)
9 changes: 9 additions & 0 deletions datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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 datalabeling/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