|
| 1 | +=================== |
| 2 | +Scale AI | Python SDK |
| 3 | +=================== |
| 4 | + |
| 5 | +# Installation |
| 6 | + |
| 7 | +.. code-block:: bash |
| 8 | + |
| 9 | + $ pip install --upgrade scaleapi |
| 10 | + |
| 11 | +Note: We strongly suggest using `scaleapi` with Python version 2.7.9 or greater due to SSL issues with prior versions. |
| 12 | + |
| 13 | +# Usage |
| 14 | + |
| 15 | +.. code-block:: python |
| 16 | + |
| 17 | + import scaleapi |
| 18 | + client = scaleapi.ScaleClient('YOUR_API_KEY_HERE') |
| 19 | + |
| 20 | +# Tasks |
| 21 | + |
| 22 | +Most of these methods will return a `scaleapi.Task` object, which will contain information |
| 23 | +about the json response (task_id, status...). |
| 24 | + |
| 25 | +Any parameter available in the documentation\_ can be passed as an argument option with the corresponding type. |
| 26 | + |
| 27 | +.. \_documentation: https://docs.scale.com/reference#task-object |
| 28 | + |
| 29 | +The following endpoints for tasks are available: |
| 30 | + |
| 31 | +## Create Task |
| 32 | + |
| 33 | +Check `this`\_\_ for further information. |
| 34 | + |
| 35 | +\_\_ https://docs.scale.com/reference#general-image-annotation |
| 36 | + |
| 37 | +.. code-block:: python |
| 38 | + |
| 39 | + client.create_task( |
| 40 | + task_type = 'imageannotation', |
| 41 | + project = 'test_project', |
| 42 | + callback_url = "http://www.example.com/callback", |
| 43 | + instruction= "Draw a box around each baby cow and big cow.", |
| 44 | + attachment_type = "image", |
| 45 | + attachment = "http://i.imgur.com/v4cBreD.jpg", |
| 46 | + geometries = { |
| 47 | + "box": { |
| 48 | + "objects_to_annotate": ["Baby Cow", "Big Cow"], |
| 49 | + "min_height": 10, |
| 50 | + "min_width": 10 |
| 51 | + } |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | +## Retrieve task |
| 56 | + |
| 57 | +Check `this`\_\_ for further information. |
| 58 | + |
| 59 | +\_\_ https://docs.scale.com/reference#retrieve-tasks |
| 60 | + |
| 61 | +Retrieve a task given its id. |
| 62 | + |
| 63 | +.. code-block :: python |
| 64 | + |
| 65 | + task = client.fetch_task('asdfasdfasdfasdfasdfasdf') |
| 66 | + print(task.status) // Task status ('pending', 'completed', 'error', 'canceled') |
| 67 | + print(task.response) // If task is complete |
| 68 | + |
| 69 | +## List Tasks |
| 70 | + |
| 71 | +Check `this`\_\_ for further information. |
| 72 | + |
| 73 | +\_\_ https://docs.scale.com/reference#list-multiple-tasks |
| 74 | + |
| 75 | +Retrieve a list of tasks, with optional filter by stand and end date/type. Paginated with `next_token`. The return value is a `scaleapi.Tasklist`, which acts as a list, but also has fields for the total number of tasks, the limit and offset, and whether or not there's more. |
| 76 | + |
| 77 | +.. code-block :: python |
| 78 | + |
| 79 | + next_token = None; |
| 80 | + counter = 0 |
| 81 | + all_tasks =[] |
| 82 | + while True: |
| 83 | + tasks = client.tasks( |
| 84 | + start_time = "2020-09-08", |
| 85 | + end_time = "2021-01-01", |
| 86 | + customer_review_status = "accepted", |
| 87 | + next_token = next_token, |
| 88 | + ) |
| 89 | + for task in tasks: |
| 90 | + counter += 1 |
| 91 | + print('Downloading Task %s | %s' % (counter, task.task_id)) |
| 92 | + all_tasks.append(task.__dict__['param_dict']) |
| 93 | + next_token = tasks.next_token |
| 94 | + if next_token is None: |
| 95 | + break |
| 96 | + print(all_tasks) |
| 97 | + |
| 98 | +## Cancel Task |
| 99 | + |
| 100 | +Check `this`\_\_ for further information. |
| 101 | + |
| 102 | +\_\_ https://docs.scale.com/reference#cancel-task |
| 103 | + |
| 104 | +Cancel a task given its id if work has not stared on the task (task status is "que). |
| 105 | + |
| 106 | +.. code-block :: python |
| 107 | + |
| 108 | + task = client.cancel_task('asdfasdfasdfasdfasdfasdf') |
| 109 | + |
| 110 | +# Batches |
| 111 | + |
| 112 | +## Create Batch |
| 113 | + |
| 114 | +Check `this`\_\_ for further information. |
| 115 | + |
| 116 | +\_\_ https://docs.scale.com/reference#batch-creation |
| 117 | + |
| 118 | +.. code-block:: python |
| 119 | + |
| 120 | + client.create_batch( |
| 121 | + project = 'test_project', |
| 122 | + callback = "http://www.example.com/callback", |
| 123 | + name = 'batch_name_01_07_2021' |
| 124 | + ) |
| 125 | + |
| 126 | +## Finalize Batch |
| 127 | + |
| 128 | +Check `this`\_\_ for further information. |
| 129 | + |
| 130 | +\_\_ https://docs.scale.com/reference#batch-finalization |
| 131 | + |
| 132 | +.. code-block:: python |
| 133 | + |
| 134 | + client.create_batch(batch_name = 'batch_name_01_07_2021') |
| 135 | + |
| 136 | +## Check Batch Status |
| 137 | + |
| 138 | +Check `this`\_\_ for further information. |
| 139 | + |
| 140 | +\_\_ https://docs.scale.com/reference#batch-status |
| 141 | + |
| 142 | +.. code-block:: python |
| 143 | + |
| 144 | + client.batch_status(batch_name = 'batch_name_01_07_2021') |
| 145 | + |
| 146 | +## Retrieve Batch |
| 147 | + |
| 148 | +Check `this`\_\_ for further information. |
| 149 | + |
| 150 | +\_\_ https://docs.scale.com/reference#batch-retrieval |
| 151 | + |
| 152 | +.. code-block:: python |
| 153 | + |
| 154 | + client.get_batch( batch_name = "batch_name_01_07_2021" ) |
| 155 | + |
| 156 | +## List Batchs |
| 157 | + |
| 158 | +Check `this`\_\_ for further information. |
| 159 | + |
| 160 | +\_\_ https://docs.scale.com/reference#batch-list |
| 161 | + |
| 162 | +Retrieve a list of batches |
| 163 | + |
| 164 | +.. code-block :: python |
| 165 | + |
| 166 | + next_token = None; |
| 167 | + counter = 0 |
| 168 | + all_batchs =[] |
| 169 | + while True: |
| 170 | + batches = client.batches( |
| 171 | + status = "completed" |
| 172 | + ) |
| 173 | + for batch in batches: |
| 174 | + counter += 1 |
| 175 | + print('Downloading Batch %s | %s | %s' % (counter, batch.name, batch.param_dict['status'])) |
| 176 | + all_batchs.append(batch.__dict__['param_dict']) |
| 177 | + next_token = batches.next_token |
| 178 | + if next_token is None: |
| 179 | + break |
| 180 | + print(all_batchs) |
| 181 | + |
| 182 | +# Projects |
| 183 | + |
| 184 | +## Create Project |
| 185 | + |
| 186 | +Check `this`\_\_ for further information. |
| 187 | + |
| 188 | +\_\_ https://docs.scale.com/reference#project-creation |
| 189 | + |
| 190 | +.. code-block:: python |
| 191 | + |
| 192 | + client.create_project( |
| 193 | + project_name = 'test_project', |
| 194 | + type = 'imageannotation, |
| 195 | + params = {'instruction':'Please label the kittens'} |
| 196 | + ) |
| 197 | + |
| 198 | +## Retrieve Project |
| 199 | + |
| 200 | +Check `this`\_\_ for further information. |
| 201 | + |
| 202 | +\_\_ https://docs.scale.com/reference#project-retrieval |
| 203 | + |
| 204 | +.. code-block:: python |
| 205 | + |
| 206 | + client.get_projet(project_name = 'test_project') |
| 207 | + |
| 208 | +## List Projects |
| 209 | + |
| 210 | +Check `this`\_\_ for further information. |
| 211 | + |
| 212 | +\_\_ https://docs.scale.com/reference#batch-list |
| 213 | + |
| 214 | +Retrieve a list of batches |
| 215 | + |
| 216 | +.. code-block :: python |
| 217 | + |
| 218 | + counter = 0 |
| 219 | + projects = client.projects() |
| 220 | + for project in projects: |
| 221 | + counter += 1 |
| 222 | + print('Downloading project %s | %s | %s' % (counter, project['name'], project['type'])) |
| 223 | + |
| 224 | +## Update Project |
| 225 | + |
| 226 | +Check `this`\_\_ for further information. |
| 227 | + |
| 228 | +\_\_ https://docs.scale.com/reference#project-update-parameters |
| 229 | + |
| 230 | +Retrieve a list of batches |
| 231 | + |
| 232 | +.. code-block :: python |
| 233 | + |
| 234 | + data = client.update_project( |
| 235 | + project_name='test_project', |
| 236 | + pathc = false, |
| 237 | + instruction='update: Please label all the stuff', |
| 238 | + |
| 239 | +) |
| 240 | + |
| 241 | +# Error handling |
| 242 | + |
| 243 | +If something went wrong while making API calls, then exceptions will be raised automatically |
| 244 | +as a `scaleapi.ScaleException` or `scaleapi.ScaleInvalidRequest` runtime error. For example: |
| 245 | + |
| 246 | +.. code-block:: python |
| 247 | + |
| 248 | + try |
| 249 | + client.create_categorization_task('Some parameters are missing.') |
| 250 | + except scaleapi.ValidationError as e: |
| 251 | + print(e.code) # 400 |
| 252 | + print(e.message) # missing param X |
| 253 | + |
| 254 | +# Troubleshooting |
| 255 | + |
| 256 | +If you notice any problems, please email us at support@scale.com. |
0 commit comments