Skip to content

Commit dba12bc

Browse files
authored
Merge pull request #8456 from GoogleCloudPlatform/python-datalabeling-migration
Migrate code from googleapis/python-datalabeling
2 parents c6b6bc1 + b9844f5 commit dba12bc

25 files changed

+1652
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
/container/**/* @GoogleCloudPlatform/dee-platform-ops @GoogleCloudPlatform/python-samples-reviewers
3434
/data-science-onramp/ @leahecole @bradmiro @GoogleCloudPlatform/python-samples-reviewers
3535
/dataflow/**/* @davidcavazos @GoogleCloudPlatform/python-samples-reviewers
36+
/datalabeling/**/* @GoogleCloudPlatform/python-samples-reviewers @ivanmkc
3637
/datastore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers
3738
/dns/**/* @GoogleCloudPlatform/python-samples-reviewers
3839
/endpoints/**/* @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ assign_issues_by:
112112
- 'api: translate'
113113
to:
114114
- nicain
115+
- labels:
116+
- 'api: datalabeling'
117+
to:
118+
- GoogleCloudPlatform/python-samples-reviewers
119+
- ivanmkc
115120
- labels:
116121
- 'api: monitoring'
117122
to:

datalabeling/AUTHORING_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md

datalabeling/CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md

datalabeling/snippets/README.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Data Labeling Service Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=datalabeling/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Data Labeling Service. `Google Cloud Data Labeling Service`_ allows developers to request having human labelers label a collection of data that you plan to use to train a custom machine learning model.
11+
12+
13+
14+
15+
.. _Google Cloud Data Labeling Service: https://cloud.google.com/data-labeling/docs/
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
62+
63+
The client library
64+
-------------------------------------------------------------------------------
65+
66+
This sample uses the `Google Cloud Client Library for Python`_.
67+
You can read the documentation for more details on API usage and use GitHub
68+
to `browse the source`_ and `report issues`_.
69+
70+
.. _Google Cloud Client Library for Python:
71+
https://googlecloudplatform.github.io/google-cloud-python/
72+
.. _browse the source:
73+
https://github.com/GoogleCloudPlatform/google-cloud-python
74+
.. _report issues:
75+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
76+
77+
78+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

datalabeling/snippets/README.rst.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Data Labeling Service
5+
short_name: Cloud Data Labeling
6+
url: https://cloud.google.com/data-labeling/docs/
7+
description: >
8+
`Google Cloud Data Labeling Service`_ allows developers to request having
9+
human labelers label a collection of data that you plan to use to train a
10+
custom machine learning model.
11+
12+
setup:
13+
- auth
14+
- install_deps
15+
16+
cloud_client_library: true
17+
18+
folder: datalabeling
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import argparse
18+
import os
19+
20+
from google.api_core.client_options import ClientOptions
21+
22+
23+
# [START datalabeling_create_annotation_spec_set_beta]
24+
def create_annotation_spec_set(project_id):
25+
"""Creates a data labeling annotation spec set for the given
26+
Google Cloud project.
27+
"""
28+
from google.cloud import datalabeling_v1beta1 as datalabeling
29+
30+
client = datalabeling.DataLabelingServiceClient()
31+
# [END datalabeling_create_annotation_spec_set_beta]
32+
# If provided, use a provided test endpoint - this will prevent tests on
33+
# this snippet from triggering any action by a real human
34+
if "DATALABELING_ENDPOINT" in os.environ:
35+
opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT"))
36+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
37+
# [START datalabeling_create_annotation_spec_set_beta]
38+
39+
project_path = f"projects/{project_id}"
40+
41+
annotation_spec_1 = datalabeling.AnnotationSpec(
42+
display_name="label_1", description="label_description_1"
43+
)
44+
45+
annotation_spec_2 = datalabeling.AnnotationSpec(
46+
display_name="label_2", description="label_description_2"
47+
)
48+
49+
annotation_spec_set = datalabeling.AnnotationSpecSet(
50+
display_name="YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME",
51+
description="YOUR_DESCRIPTION",
52+
annotation_specs=[annotation_spec_1, annotation_spec_2],
53+
)
54+
55+
response = client.create_annotation_spec_set(
56+
request={"parent": project_path, "annotation_spec_set": annotation_spec_set}
57+
)
58+
59+
# The format of the resource name:
60+
# project_id/{project_id}/annotationSpecSets/{annotationSpecSets_id}
61+
print("The annotation_spec_set resource name: {}".format(response.name))
62+
print("Display name: {}".format(response.display_name))
63+
print("Description: {}".format(response.description))
64+
print("Annotation specs:")
65+
for annotation_spec in response.annotation_specs:
66+
print("\tDisplay name: {}".format(annotation_spec.display_name))
67+
print("\tDescription: {}\n".format(annotation_spec.description))
68+
69+
return response
70+
71+
72+
# [END datalabeling_create_annotation_spec_set_beta]
73+
74+
75+
if __name__ == "__main__":
76+
parser = argparse.ArgumentParser(
77+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
78+
)
79+
80+
parser.add_argument("--project-id", help="Project ID. Required.", required=True)
81+
82+
args = parser.parse_args()
83+
84+
create_annotation_spec_set(args.project_id)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google, Inc
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
import backoff
20+
from google.api_core.exceptions import ServerError
21+
import pytest
22+
23+
import create_annotation_spec_set
24+
import testing_lib
25+
26+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
28+
29+
@pytest.fixture(scope="module")
30+
def cleaner():
31+
resource_names = []
32+
33+
yield resource_names
34+
35+
for resource_name in resource_names:
36+
testing_lib.delete_annotation_spec_set(resource_name)
37+
38+
39+
@pytest.mark.skip(reason="service is limited due to covid")
40+
def test_create_annotation_spec_set(cleaner, capsys):
41+
@backoff.on_exception(
42+
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE
43+
)
44+
def run_sample():
45+
return create_annotation_spec_set.create_annotation_spec_set(PROJECT_ID)
46+
47+
response = run_sample()
48+
49+
# For cleanup
50+
cleaner.append(response.name)
51+
52+
out, _ = capsys.readouterr()
53+
assert "The annotation_spec_set resource name:" in out
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import argparse
18+
import os
19+
20+
from google.api_core.client_options import ClientOptions
21+
22+
23+
# [START datalabeling_create_instruction_beta]
24+
def create_instruction(project_id, data_type, instruction_gcs_uri):
25+
"""Creates a data labeling PDF instruction for the given Google Cloud
26+
project. The PDF file should be uploaded to the project in
27+
Google Cloud Storage.
28+
"""
29+
from google.cloud import datalabeling_v1beta1 as datalabeling
30+
31+
client = datalabeling.DataLabelingServiceClient()
32+
# [END datalabeling_create_instruction_beta]
33+
# If provided, use a provided test endpoint - this will prevent tests on
34+
# this snippet from triggering any action by a real human
35+
if "DATALABELING_ENDPOINT" in os.environ:
36+
opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT"))
37+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
38+
# [START datalabeling_create_instruction_beta]
39+
40+
project_path = f"projects/{project_id}"
41+
42+
pdf_instruction = datalabeling.PdfInstruction(gcs_file_uri=instruction_gcs_uri)
43+
44+
instruction = datalabeling.Instruction(
45+
display_name="YOUR_INSTRUCTION_DISPLAY_NAME",
46+
description="YOUR_DESCRIPTION",
47+
data_type=data_type,
48+
pdf_instruction=pdf_instruction,
49+
)
50+
51+
operation = client.create_instruction(
52+
request={"parent": project_path, "instruction": instruction}
53+
)
54+
55+
result = operation.result()
56+
57+
# The format of the resource name:
58+
# project_id/{project_id}/instruction/{instruction_id}
59+
print("The instruction resource name: {}".format(result.name))
60+
print("Display name: {}".format(result.display_name))
61+
print("Description: {}".format(result.description))
62+
print("Create time:")
63+
print("\tseconds: {}".format(result.create_time.timestamp_pb().seconds))
64+
print("\tnanos: {}".format(result.create_time.timestamp_pb().nanos))
65+
print("Data type: {}".format(datalabeling.DataType(result.data_type).name))
66+
print("Pdf instruction:")
67+
print("\tGcs file uri: {}\n".format(result.pdf_instruction.gcs_file_uri))
68+
69+
return result
70+
71+
72+
# [END datalabeling_create_instruction_beta]
73+
74+
75+
if __name__ == "__main__":
76+
parser = argparse.ArgumentParser(
77+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
78+
)
79+
80+
parser.add_argument("--project-id", help="Project ID. Required.", required=True)
81+
82+
parser.add_argument(
83+
"--data-type",
84+
help="Data type. Only support IMAGE, VIDEO, TEXT and AUDIO. Required.",
85+
required=True,
86+
)
87+
88+
parser.add_argument(
89+
"--instruction-gcs-uri",
90+
help="The URI of Google Cloud Storage of the instruction. Required.",
91+
required=True,
92+
)
93+
94+
args = parser.parse_args()
95+
96+
create_instruction(args.project_id, args.data_type, args.instruction_gcs_uri)

0 commit comments

Comments
 (0)