|
1 |
| -# scaleapi-python-client |
| 1 | +# ScaleAPI for Python |
| 2 | + |
| 3 | +## Installation |
| 4 | +```sh |
| 5 | +pip install scale |
| 6 | +``` |
| 7 | + |
| 8 | +## Usage |
| 9 | +```python |
| 10 | +import scale |
| 11 | +client = scale.ScaleClient('YOUR_API_KEY_HERE', callback_key='OPTIONAL_CALLBACK_KEY_HERE') |
| 12 | +``` |
| 13 | + |
| 14 | +### Tasks |
| 15 | + |
| 16 | +Most of these methods will return a `Scale::Resources::Task` object, which will contain information |
| 17 | +about the json response (task_id, status...). |
| 18 | + |
| 19 | +Any parameter available in the [documentation](https://docs.scaleapi.com) can be passed as an argument |
| 20 | +option with the corresponding type. |
| 21 | + |
| 22 | +The following endpoints for tasks are available: |
| 23 | + |
| 24 | +#### Create categorization task |
| 25 | + |
| 26 | +Check [this](https://docs.scaleapi.com/#create-categorization-task) for further information. |
| 27 | + |
| 28 | +```python |
| 29 | +task = client.create_categorization_task( |
| 30 | + callback_url='http://www.example.com/callback', |
| 31 | + instruction='Is this company public or private?', |
| 32 | + attachment_type='website', |
| 33 | + attachment='http://www.google.com/', |
| 34 | + categories=['public', 'private'] |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +#### Create transcription task |
| 39 | + |
| 40 | +Check [this](https://docs.scaleapi.com/#create-transcription-task) for further information. |
| 41 | + |
| 42 | +```python |
| 43 | +task = client.create_transcription_task( |
| 44 | + callback_url='http://www.example.com/callback', |
| 45 | + instruction='Transcribe the given fields. Then for each news item on the page, transcribe the information for the row.', |
| 46 | + attachment_type='website', |
| 47 | + attachment='http://www.google.com/', |
| 48 | + fields={ 'title': 'Title of Webpage', 'top_result': 'Title of the top result' }, |
| 49 | + row_fields: { 'username': 'Username of submitter', 'comment_count': 'Number of comments' } |
| 50 | +) |
| 51 | +``` |
| 52 | + |
| 53 | +#### Create phone call task |
| 54 | + |
| 55 | +Check [this](https://docs.scaleapi.com/#create-phone-call-task) for further information. |
| 56 | + |
| 57 | +```python |
| 58 | +client.create_phonecall_task( |
| 59 | + callback_url='http://www.example.com/callback', |
| 60 | + instruction="Call this person and tell me his email address. Ask if he's happy too.", |
| 61 | + phone_number='5055006865', |
| 62 | + entity_name='Alexandr Wang', |
| 63 | + fields={ 'email': 'Email Address' }, |
| 64 | + choices=['He is happy', 'He is not happy'] |
| 65 | +) |
| 66 | +``` |
| 67 | + |
| 68 | +#### Create comparison task |
| 69 | + |
| 70 | +Check [this](https://docs.scaleapi.com/#create-comparison-task) for further information. |
| 71 | + |
| 72 | +```python |
| 73 | +client.create_comparison_task( |
| 74 | + callback_url='http://www.example.com/callback', |
| 75 | + instruction='Do the objects in these images have the same pattern?', |
| 76 | + attachment_type='image', |
| 77 | + choices=['yes', 'no'], |
| 78 | + attachments=[ |
| 79 | + 'http://i.ebayimg.com/00/$T2eC16dHJGwFFZKjy5ZjBRfNyMC4Ig~~_32.JPG', |
| 80 | + 'http://images.wisegeek.com/checkered-tablecloth.jpg' |
| 81 | + ] |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +#### Create annotation task |
| 86 | + |
| 87 | +Check [this](https://docs.scaleapi.com/#create-annotation-task-bounding-box) for further information. |
| 88 | + |
| 89 | +```python |
| 90 | +client.create_annotation_task( |
| 91 | + callback_url='http://www.example.com/callback', |
| 92 | + instruction='Draw a box around each baby cow and big cow.', |
| 93 | + attachment_type="image", |
| 94 | + attachment="http://i.imgur.com/v4cBreD.jpg", |
| 95 | + objects_to_annotate=["baby cow", "big cow"] |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +#### Retrieve task |
| 100 | + |
| 101 | +Check [this](https://docs.scaleapi.com/#retrieve-a-task) for further information. |
| 102 | + |
| 103 | +Retrieve a task given its id. |
| 104 | + |
| 105 | +```python |
| 106 | +task = client.retrieve_task('asdfasdfasdfasdfasdfasdf') |
| 107 | +task.id == 'asdfasdfasdfasdfasdfasdf' # true |
| 108 | +``` |
| 109 | + |
| 110 | +#### Cancel task |
| 111 | + |
| 112 | +Check [this](https://docs.scaleapi.com/#cancel-a-task) for further information. |
| 113 | + |
| 114 | +Cancel a task given its id, only if it's not completed. |
| 115 | + |
| 116 | +```python |
| 117 | +task = client.cancel_task('asdfasdfasdfasdfasdfasdf') |
| 118 | +``` |
| 119 | + |
| 120 | +#### List tasks |
| 121 | + |
| 122 | +Check [this](https://docs.scaleapi.com/#list-all-tasks) for further information. |
| 123 | + |
| 124 | +Retrieve a list of all tasks. |
| 125 | + |
| 126 | +```python |
| 127 | +tasks = client.tasks() |
| 128 | +``` |
| 129 | + |
| 130 | +## Error handling |
| 131 | + |
| 132 | +If something went wrong while making API calls, then exceptions will be raised automatically |
| 133 | +as a `scale.ScaleError` or `scale.ValidationError` runtime error. For example: |
| 134 | + |
| 135 | +```python |
| 136 | +try |
| 137 | + scale.create_categorization_task('Some parameters are missing.') |
| 138 | +except scale.ValidationError as e: |
| 139 | + print(e.code) # 400 |
| 140 | + print(e.message) # missing param X |
| 141 | +``` |
| 142 | + |
| 143 | +## Custom options |
| 144 | + |
| 145 | +The api initialization accepts the following options: |
| 146 | + |
| 147 | +| Name | Description | Default | |
| 148 | +| ---- | ----------- | ------- | |
| 149 | +| `endpoint` | Endpoint used in the http requests. | `'https://api.scaleapi.com/v1/'` | |
| 150 | +| `api_key` | API key used in the http requests. | `nil` | |
| 151 | +| `callback_key` | API key used to validate callback POST requests. | `nil` | |
| 152 | + |
0 commit comments