forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_dataset.py
More file actions
155 lines (125 loc) · 5.57 KB
/
manage_dataset.py
File metadata and controls
155 lines (125 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# 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'))
client = datalabeling.DataLabelingServiceClient(client_options=opts)
# [START datalabeling_create_dataset_beta]
formatted_project_name = client.project_path(project_id)
dataset = datalabeling.types.Dataset(
display_name='YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME',
description='YOUR_DESCRIPTION'
)
response = client.create_dataset(formatted_project_name, dataset)
# The format of resource name:
# project_id/{project_id}/datasets/{dataset_id}
print('The dataset resource name: {}\n'.format(response.name))
print('Display name: {}'.format(response.display_name))
print('Description: {}'.format(response.description))
print('Create time:')
print('\tseconds: {}'.format(response.create_time.seconds))
print('\tnanos: {}'.format(response.create_time.nanos))
return response
# [END datalabeling_create_dataset_beta]
# [START datalabeling_list_datasets_beta]
def list_datasets(project_id):
"""Lists datasets for the given Google Cloud project."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
formatted_project_name = client.project_path(project_id)
response = client.list_datasets(formatted_project_name)
for element in response:
# The format of resource name:
# project_id/{project_id}/datasets/{dataset_id}
print('The dataset resource name: {}\n'.format(element.name))
print('Display name: {}'.format(element.display_name))
print('Description: {}'.format(element.description))
print('Create time:')
print('\tseconds: {}'.format(element.create_time.seconds))
print('\tnanos: {}'.format(element.create_time.nanos))
# [END datalabeling_list_datasets_beta]
# [START datalabeling_get_dataset_beta]
def get_dataset(dataset_resource_name):
"""Gets a dataset for the given Google Cloud project."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
response = client.get_dataset(dataset_resource_name)
print('The dataset resource name: {}\n'.format(response.name))
print('Display name: {}'.format(response.display_name))
print('Description: {}'.format(response.description))
print('Create time:')
print('\tseconds: {}'.format(response.create_time.seconds))
print('\tnanos: {}'.format(response.create_time.nanos))
# [END datalabeling_get_dataset_beta]
# [START datalabeling_delete_dataset_beta]
def delete_dataset(dataset_resource_name):
"""Deletes a dataset for the given Google Cloud project."""
from google.cloud import datalabeling_v1beta1 as datalabeling
client = datalabeling.DataLabelingServiceClient()
response = client.delete_dataset(dataset_resource_name)
print('Dataset deleted. {}\n'.format(response))
# [END datalabeling_delete_dataset_beta]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
subparsers = parser.add_subparsers(dest='command')
create_parser = subparsers.add_parser(
'create', help='Create a new dataset.')
create_parser.add_argument(
'--project-id',
help='Project ID. Required.',
required=True
)
list_parser = subparsers.add_parser('list', help='List all datasets.')
list_parser.add_argument(
'--project-id',
help='Project ID. Required.',
required=True
)
get_parser = subparsers.add_parser(
'get', help='Get a dataset by the dataset resource name.')
get_parser.add_argument(
'--dataset-resource-name',
help='The dataset resource name. Used in the get or delete operation.',
required=True
)
delete_parser = subparsers.add_parser(
'delete', help='Delete a dataset by the dataset resource name.')
delete_parser.add_argument(
'--dataset-resource-name',
help='The dataset resource name. Used in the get or delete operation.',
required=True
)
args = parser.parse_args()
if args.command == 'create':
create_dataset(args.project_id)
elif args.command == 'list':
list_datasets(args.project_id)
elif args.command == 'get':
get_dataset(args.dataset_resource_name)
elif args.command == 'delete':
delete_dataset(args.dataset_resource_name)