Skip to content

Commit 9793e83

Browse files
feat!: migrate to use microgen (#34)
* feat!: migrate to use microgen * update sample * update sample * update sample * Update UPGRADING.md Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 524c5f1 commit 9793e83

17 files changed

+393
-362
lines changed

datalabeling/snippets/create_annotation_spec_set.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,61 +26,58 @@ def create_annotation_spec_set(project_id):
2626
Google Cloud project.
2727
"""
2828
from google.cloud import datalabeling_v1beta1 as datalabeling
29+
2930
client = datalabeling.DataLabelingServiceClient()
3031
# [END datalabeling_create_annotation_spec_set_beta]
3132
# If provided, use a provided test endpoint - this will prevent tests on
3233
# this snippet from triggering any action by a real human
33-
if 'DATALABELING_ENDPOINT' in os.environ:
34-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
34+
if "DATALABELING_ENDPOINT" in os.environ:
35+
opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT"))
3536
client = datalabeling.DataLabelingServiceClient(client_options=opts)
3637
# [START datalabeling_create_annotation_spec_set_beta]
3738

38-
project_path = client.project_path(project_id)
39+
project_path = f"projects/{project_id}"
3940

40-
annotation_spec_1 = datalabeling.types.AnnotationSpec(
41-
display_name='label_1',
42-
description='label_description_1'
41+
annotation_spec_1 = datalabeling.AnnotationSpec(
42+
display_name="label_1", description="label_description_1"
4343
)
4444

45-
annotation_spec_2 = datalabeling.types.AnnotationSpec(
46-
display_name='label_2',
47-
description='label_description_2'
45+
annotation_spec_2 = datalabeling.AnnotationSpec(
46+
display_name="label_2", description="label_description_2"
4847
)
4948

50-
annotation_spec_set = datalabeling.types.AnnotationSpecSet(
51-
display_name='YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME',
52-
description='YOUR_DESCRIPTION',
53-
annotation_specs=[annotation_spec_1, annotation_spec_2]
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],
5453
)
5554

5655
response = client.create_annotation_spec_set(
57-
project_path, annotation_spec_set)
56+
request={"parent": project_path, "annotation_spec_set": annotation_spec_set}
57+
)
5858

5959
# The format of the resource name:
6060
# 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:')
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:")
6565
for annotation_spec in response.annotation_specs:
66-
print('\tDisplay name: {}'.format(annotation_spec.display_name))
67-
print('\tDescription: {}\n'.format(annotation_spec.description))
66+
print("\tDisplay name: {}".format(annotation_spec.display_name))
67+
print("\tDescription: {}\n".format(annotation_spec.description))
6868

6969
return response
70+
71+
7072
# [END datalabeling_create_annotation_spec_set_beta]
7173

7274

73-
if __name__ == '__main__':
75+
if __name__ == "__main__":
7476
parser = argparse.ArgumentParser(
75-
description=__doc__,
76-
formatter_class=argparse.RawDescriptionHelpFormatter
77+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
7778
)
7879

79-
parser.add_argument(
80-
'--project-id',
81-
help='Project ID. Required.',
82-
required=True
83-
)
80+
parser.add_argument("--project-id", help="Project ID. Required.", required=True)
8481

8582
args = parser.parse_args()
8683

datalabeling/snippets/create_annotation_spec_set_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import testing_lib
2525

2626

27-
PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
27+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2828

2929

30-
@pytest.fixture(scope='module')
30+
@pytest.fixture(scope="module")
3131
def cleaner():
3232
resource_names = []
3333

@@ -38,9 +38,9 @@ def cleaner():
3838

3939

4040
def test_create_annotation_spec_set(cleaner, capsys):
41-
4241
@backoff.on_exception(
43-
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
42+
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE
43+
)
4444
def run_sample():
4545
return create_annotation_spec_set.create_annotation_spec_set(PROJECT_ID)
4646

@@ -50,4 +50,4 @@ def run_sample():
5050
cleaner.append(response.name)
5151

5252
out, _ = capsys.readouterr()
53-
assert 'The annotation_spec_set resource name:' in out
53+
assert "The annotation_spec_set resource name:" in out

datalabeling/snippets/create_instruction.py

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,77 +27,70 @@ def create_instruction(project_id, data_type, instruction_gcs_uri):
2727
Google Cloud Storage.
2828
"""
2929
from google.cloud import datalabeling_v1beta1 as datalabeling
30+
3031
client = datalabeling.DataLabelingServiceClient()
3132
# [END datalabeling_create_instruction_beta]
3233
# If provided, use a provided test endpoint - this will prevent tests on
3334
# 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'))
35+
if "DATALABELING_ENDPOINT" in os.environ:
36+
opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT"))
3637
client = datalabeling.DataLabelingServiceClient(client_options=opts)
3738
# [START datalabeling_create_instruction_beta]
3839

39-
project_path = client.project_path(project_id)
40+
project_path = f"projects/{project_id}"
4041

41-
pdf_instruction = datalabeling.types.PdfInstruction(
42-
gcs_file_uri=instruction_gcs_uri)
42+
pdf_instruction = datalabeling.PdfInstruction(gcs_file_uri=instruction_gcs_uri)
4343

44-
instruction = datalabeling.types.Instruction(
45-
display_name='YOUR_INSTRUCTION_DISPLAY_NAME',
46-
description='YOUR_DESCRIPTION',
44+
instruction = datalabeling.Instruction(
45+
display_name="YOUR_INSTRUCTION_DISPLAY_NAME",
46+
description="YOUR_DESCRIPTION",
4747
data_type=data_type,
48-
pdf_instruction=pdf_instruction
48+
pdf_instruction=pdf_instruction,
4949
)
5050

51-
operation = client.create_instruction(project_path, instruction)
51+
operation = client.create_instruction(
52+
request={"parent": project_path, "instruction": instruction}
53+
)
5254

5355
result = operation.result()
5456

5557
# The format of the resource name:
5658
# project_id/{project_id}/instruction/{instruction_id}
57-
print('The instruction resource name: {}'.format(result.name))
58-
print('Display name: {}'.format(result.display_name))
59-
print('Description: {}'.format(result.description))
60-
print('Create time:')
61-
print('\tseconds: {}'.format(result.create_time.seconds))
62-
print('\tnanos: {}'.format(result.create_time.nanos))
63-
print('Data type: {}'.format(
64-
datalabeling.enums.DataType(result.data_type).name))
65-
print('Pdf instruction:')
66-
print('\tGcs file uri: {}\n'.format(
67-
result.pdf_instruction.gcs_file_uri))
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))
6868

6969
return result
70+
71+
7072
# [END datalabeling_create_instruction_beta]
7173

7274

73-
if __name__ == '__main__':
75+
if __name__ == "__main__":
7476
parser = argparse.ArgumentParser(
75-
description=__doc__,
76-
formatter_class=argparse.RawDescriptionHelpFormatter
77+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
7778
)
7879

79-
parser.add_argument(
80-
'--project-id',
81-
help='Project ID. Required.',
82-
required=True
83-
)
80+
parser.add_argument("--project-id", help="Project ID. Required.", required=True)
8481

8582
parser.add_argument(
86-
'--data-type',
87-
help='Data type. Only support IMAGE, VIDEO, TEXT and AUDIO. Required.',
88-
required=True
83+
"--data-type",
84+
help="Data type. Only support IMAGE, VIDEO, TEXT and AUDIO. Required.",
85+
required=True,
8986
)
9087

9188
parser.add_argument(
92-
'--instruction-gcs-uri',
93-
help='The URI of Google Cloud Storage of the instruction. Required.',
94-
required=True
89+
"--instruction-gcs-uri",
90+
help="The URI of Google Cloud Storage of the instruction. Required.",
91+
required=True,
9592
)
9693

9794
args = parser.parse_args()
9895

99-
create_instruction(
100-
args.project_id,
101-
args.data_type,
102-
args.instruction_gcs_uri
103-
)
96+
create_instruction(args.project_id, args.data_type, args.instruction_gcs_uri)

datalabeling/snippets/create_instruction_test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818

1919
import backoff
2020
from google.api_core.exceptions import ServerError
21+
from google.cloud import datalabeling
2122
import pytest
2223

2324
import create_instruction
2425
import testing_lib
2526

2627

27-
PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
28-
INSTRUCTION_GCS_URI = ('gs://cloud-samples-data/datalabeling'
29-
'/instruction/test.pdf')
28+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
29+
INSTRUCTION_GCS_URI = "gs://cloud-samples-data/datalabeling" "/instruction/test.pdf"
3030

3131

32-
@pytest.fixture(scope='module')
32+
@pytest.fixture(scope="module")
3333
def cleaner():
3434
resource_names = []
3535

@@ -40,15 +40,16 @@ def cleaner():
4040

4141

4242
def test_create_instruction(cleaner, capsys):
43-
4443
@backoff.on_exception(
45-
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
44+
backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE
45+
)
4646
def run_sample():
4747
return create_instruction.create_instruction(
48-
PROJECT_ID, 'IMAGE', INSTRUCTION_GCS_URI)
48+
PROJECT_ID, datalabeling.DataType.IMAGE, INSTRUCTION_GCS_URI
49+
)
4950

5051
instruction = run_sample()
5152
cleaner.append(instruction.name)
5253

5354
out, _ = capsys.readouterr()
54-
assert 'The instruction resource name: ' in out
55+
assert "The instruction resource name: " in out

datalabeling/snippets/export_data.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,67 +21,71 @@
2121

2222

2323
# [START datalabeling_export_data_beta]
24-
def export_data(dataset_resource_name, annotated_dataset_resource_name,
25-
export_gcs_uri):
24+
def export_data(dataset_resource_name, annotated_dataset_resource_name, export_gcs_uri):
2625
"""Exports a dataset from the given Google Cloud project."""
2726
from google.cloud import datalabeling_v1beta1 as datalabeling
27+
2828
client = datalabeling.DataLabelingServiceClient()
2929
# [END datalabeling_export_data_beta]
3030
# If provided, use a provided test endpoint - this will prevent tests on
3131
# this snippet from triggering any action by a real human
32-
if 'DATALABELING_ENDPOINT' in os.environ:
33-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
32+
if "DATALABELING_ENDPOINT" in os.environ:
33+
opts = ClientOptions(api_endpoint=os.getenv("DATALABELING_ENDPOINT"))
3434
client = datalabeling.DataLabelingServiceClient(client_options=opts)
3535
# [START datalabeling_export_data_beta]
3636

37-
gcs_destination = datalabeling.types.GcsDestination(
38-
output_uri=export_gcs_uri, mime_type='text/csv')
37+
gcs_destination = datalabeling.GcsDestination(
38+
output_uri=export_gcs_uri, mime_type="text/csv"
39+
)
3940

40-
output_config = datalabeling.types.OutputConfig(
41-
gcs_destination=gcs_destination)
41+
output_config = datalabeling.OutputConfig(gcs_destination=gcs_destination)
4242

4343
response = client.export_data(
44-
dataset_resource_name,
45-
annotated_dataset_resource_name,
46-
output_config
44+
request={
45+
"name": dataset_resource_name,
46+
"annotated_dataset": annotated_dataset_resource_name,
47+
"output_config": output_config,
48+
}
4749
)
4850

49-
print('Dataset ID: {}\n'.format(response.result().dataset))
50-
print('Output config:')
51-
print('\tGcs destination:')
52-
print('\t\tOutput URI: {}\n'.format(
53-
response.result().output_config.gcs_destination.output_uri))
51+
print("Dataset ID: {}\n".format(response.result().dataset))
52+
print("Output config:")
53+
print("\tGcs destination:")
54+
print(
55+
"\t\tOutput URI: {}\n".format(
56+
response.result().output_config.gcs_destination.output_uri
57+
)
58+
)
59+
60+
5461
# [END datalabeling_export_data_beta]
5562

5663

57-
if __name__ == '__main__':
64+
if __name__ == "__main__":
5865
parser = argparse.ArgumentParser(
59-
description=__doc__,
60-
formatter_class=argparse.RawDescriptionHelpFormatter
66+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
6167
)
6268

6369
parser.add_argument(
64-
'--dataset-resource-name',
65-
help='Dataset resource name. Required.',
66-
required=True
70+
"--dataset-resource-name",
71+
help="Dataset resource name. Required.",
72+
required=True,
6773
)
6874

6975
parser.add_argument(
70-
'--annotated-dataset-resource-name',
71-
help='Annotated Dataset resource name. Required.',
72-
required=True
76+
"--annotated-dataset-resource-name",
77+
help="Annotated Dataset resource name. Required.",
78+
required=True,
7379
)
7480

7581
parser.add_argument(
76-
'--export-gcs-uri',
77-
help='The export GCS URI. Required.',
78-
required=True
82+
"--export-gcs-uri", help="The export GCS URI. Required.", required=True
7983
)
8084

8185
args = parser.parse_args()
8286

8387
export_data(
8488
args.dataset_resource_name,
8589
args.annotated_dataset_resource_name,
86-
args.export_gcs_uri
90+
args.export_gcs_uri,
8791
)

0 commit comments

Comments
 (0)