31
31
.. code-block :: python
32
32
33
33
import scaleapi
34
- client = scaleapi.ScaleClient(' YOUR_API_KEY_HERE' )
34
+
35
+ client = scaleapi.ScaleClient(" YOUR_API_KEY_HERE" )
35
36
36
37
Tasks
37
38
_____
@@ -62,22 +63,28 @@ __ https://docs.scale.com/reference
62
63
63
64
from scaleapi.tasks import TaskType
64
65
65
- client.create_task(
66
- TaskType.ImageAnnotation,
67
- project = ' test_project' ,
66
+ payload = dict (
67
+ project = " test_project" ,
68
68
callback_url = " http://www.example.com/callback" ,
69
- instruction = " Draw a box around each baby cow and big cow." ,
69
+ instruction = " Draw a box around each baby cow and big cow." ,
70
70
attachment_type = " image" ,
71
71
attachment = " http://i.imgur.com/v4cBreD.jpg" ,
72
+ unique_id = " c235d023af73" ,
72
73
geometries = {
73
74
" box" : {
74
- " objects_to_annotate" : [" Baby Cow" , " Big Cow" ],
75
- " min_height" : 10 ,
76
- " min_width" : 10
75
+ " objects_to_annotate" : [" Baby Cow" , " Big Cow" ],
76
+ " min_height" : 10 ,
77
+ " min_width" : 10 ,
77
78
}
78
- }
79
+ },
79
80
)
80
81
82
+ try :
83
+ client.create_task(TaskType.ImageAnnotation, ** payload)
84
+ except ScaleDuplicateTask as err:
85
+ print (err.message) # If unique_id is already used for a different task
86
+
87
+
81
88
Retrieve a task
82
89
^^^^^^^^^^^^^^^
83
90
@@ -87,8 +94,8 @@ __ https://docs.scale.com/reference#retrieve-tasks
87
94
88
95
.. code-block :: python
89
96
90
- task = client.get_task(' 30553edd0b6a93f8f05f0fee' )
91
- print(task.status) # Task status (' pending', ' completed', ' error', ' canceled' )
97
+ task = client.get_task(" 30553edd0b6a93f8f05f0fee" )
98
+ print(task.status) # Task status (" pending", " completed", " error", " canceled" )
92
99
print(task.response) # If task is complete
93
100
94
101
List Tasks
@@ -100,9 +107,9 @@ Retrieve a list of `Task` objects, with filters for: ``project_name``, ``batch_n
100
107
101
108
``get_tasks() `` is a **generator ** method and yields ``Task `` objects.
102
109
103
- ` A generator is another type of function, returns an iterable that you can loop over like a list.
110
+ * A generator is another type of function, returns an iterable that you can loop over like a list.
104
111
However, unlike lists, generators do not store the content in the memory.
105
- That helps you to process a large number of objects without increasing memory usage. `
112
+ That helps you to process a large number of objects without increasing memory usage. *
106
113
107
114
If you will iterate through the tasks and process them once, using a generator is the most efficient method.
108
115
However, if you need to process the list of tasks multiple times, you can wrap the generator in a ``list(...) ``
@@ -157,9 +164,9 @@ __ https://docs.scale.com/reference#batch-creation
157
164
.. code-block :: python
158
165
159
166
client.create_batch(
160
- project = ' test_project' ,
167
+ project = " test_project" ,
161
168
callback = " http://www.example.com/callback" ,
162
- name = ' batch_name_01_07_2021'
169
+ name = " batch_name_01_07_2021"
163
170
)
164
171
165
172
Finalize Batch
@@ -171,7 +178,11 @@ __ https://docs.scale.com/reference#batch-finalization
171
178
172
179
.. code-block :: python
173
180
174
- client.finalize_batch(batch_name = ' batch_name_01_07_2021' )
181
+ client.finalize_batch(batch_name = " batch_name_01_07_2021" )
182
+
183
+ # Alternative method
184
+ batch = client.get_batch(batch_name = " batch_name_01_07_2021" )
185
+ batch.finalize()
175
186
176
187
Check Batch Status
177
188
^^^^^^^^^^^^^^^^^^
@@ -182,10 +193,10 @@ __ https://docs.scale.com/reference#batch-status
182
193
183
194
.. code-block :: python
184
195
185
- client.batch_status(batch_name = ' batch_name_01_07_2021' )
196
+ client.batch_status(batch_name = " batch_name_01_07_2021" )
186
197
187
198
# Alternative via Batch.get_status()
188
- batch = client.get_batch(' batch_name_01_07_2021' )
199
+ batch = client.get_batch(" batch_name_01_07_2021" )
189
200
batch.get_status() # Refreshes tasks_{status} attributes of Batch
190
201
print (batch.tasks_pending, batch.tasks_completed)
191
202
@@ -198,7 +209,7 @@ __ https://docs.scale.com/reference#batch-retrieval
198
209
199
210
.. code-block :: python
200
211
201
- client.get_batch(batch_name = ' batch_name_01_07_2021' )
212
+ client.get_batch(batch_name = " batch_name_01_07_2021" )
202
213
203
214
List Batches
204
215
^^^^^^^^^^^^
@@ -207,9 +218,9 @@ Retrieve a list of Batches. Optional parameters are ``project_name``, ``batch_st
207
218
208
219
``get_batches() `` is a **generator ** method and yields ``Batch `` objects.
209
220
210
- ` A generator is another type of function, returns an iterable that you can loop over like a list.
221
+ * A generator is another type of function, returns an iterable that you can loop over like a list.
211
222
However, unlike lists, generators do not store the content in the memory.
212
- That helps you to process a large number of objects without increasing memory usage. `
223
+ That helps you to process a large number of objects without increasing memory usage. *
213
224
214
225
When wrapped in a ``list(...) `` statement, it returns a list of Batches by loading them into the memory.
215
226
@@ -229,7 +240,7 @@ __ https://docs.scale.com/reference#batch-list
229
240
counter = 0
230
241
for batch in batches:
231
242
counter += 1
232
- print(f' Downloading batch {counter} | {batch.name} | {batch.project}' )
243
+ print(f" Downloading batch {counter} | {batch.name} | {batch.project}" )
233
244
234
245
# Alternative for accessing as a Batch list
235
246
batch_list = list(batches)
@@ -247,12 +258,16 @@ __ https://docs.scale.com/reference#project-creation
247
258
248
259
.. code-block :: python
249
260
250
- client.create_project(
251
- project_name = ' test_project' ,
252
- type = ' imageannotation,
253
- params = {' instruction' :' Please label the kittens' }
261
+ from scaleapi.tasks import TaskType
262
+
263
+ project = client.create_project(
264
+ project_name = " Test_Project" ,
265
+ task_type = TaskType.ImageAnnotation,
266
+ params = {" instruction" : " Please label the kittens" },
254
267
)
255
268
269
+ print (project.name) # Test_Project
270
+
256
271
Retrieve Project
257
272
^^^^^^^^^^^^^^^^
258
273
@@ -262,7 +277,7 @@ __ https://docs.scale.com/reference#project-retrieval
262
277
263
278
.. code-block :: python
264
279
265
- client.get_project(project_name = ' test_project' )
280
+ client.get_project(project_name = " test_project" )
266
281
267
282
List Projects
268
283
^^^^^^^^^^^^^
@@ -290,9 +305,9 @@ __ https://docs.scale.com/reference#project-update-parameters
290
305
.. code-block :: python
291
306
292
307
data = client.update_project(
293
- project_name=' test_project' ,
308
+ project_name=" test_project" ,
294
309
patch = false,
295
- instruction=' update: Please label all the stuff' ,
310
+ instruction=" update: Please label all the stuff" ,
296
311
)
297
312
298
313
Error handling
@@ -319,7 +334,7 @@ For example:
319
334
from scaleapi.exceptions import ScaleException
320
335
321
336
try :
322
- client.create_task(TaskType.TextCollection, attachment = ' Some parameters are missing.' )
337
+ client.create_task(TaskType.TextCollection, attachment = " Some parameters are missing." )
323
338
except ScaleException as err:
324
339
print (err.code) # 400
325
340
print (err.message) # Parameter is invalid, reason: "attachments" is required
0 commit comments