Skip to content

Commit 8298cda

Browse files
committed
some initial setup
1 parent 6976deb commit 8298cda

File tree

4 files changed

+203
-1
lines changed

4 files changed

+203
-1
lines changed

README.md

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,152 @@
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+

scale/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import requests
2+
3+
from . import tasks
4+
5+
DEFAULT_FIELDS = {'callback_url', 'instruction', 'urgency'}
6+
7+
class ScaleClient(object):
8+
def __init__(self, api_key, callback_key=None,
9+
endpoint='https://api.scaleapi.com/v1/'):
10+
self.api_key = api_key
11+
self.callback_key = callback_key
12+
self.endpoint = endpoint
13+
14+
def create_comparison_task(**kwargs):
15+
payload = generic_payload(kwargs)
16+
allowed_fields = DEFAULT_FIELDS + \
17+
{'attachment', 'attachment_type', 'categories', 'category_ids', 'allow_multiple'}
18+
if field in kwargs:
19+
payload[field] = kwargs[field]
20+
payload = {
21+
'callback_url': callback_url,
22+
'instruction': instruction,
23+
}
24+
return self._dotask(tasks.ComparisonTask(*args, **kwargs))
25+
26+
def create_transcription_task(*args, **kwargs):
27+
return self._dotask(tasks.TranscriptionTask(*args, **kwargs))
28+
29+
def create_phonecall_task(*args, **kwargs):
30+
return self._dotask(tasks.PhonecallTask(*args, **kwargs))
31+
32+
def create_comparison_task(*args, **kwargs):
33+
return self._dotask(tasks.TranscriptionTask(*args, **kwargs))
34+
35+
def create_annotation_task(*args, **kwargs):
36+
return self._dotask(tasks.AnnotationTask(*args, **kwargs))

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from distutils.core import setup
2+
setup(
3+
name = 'scaleapi',
4+
packages = ['scaleapi'],
5+
version = '0.1',
6+
description = 'A client library for interacting with the Scale API',
7+
author = 'Calvin Huang',
8+
author_email = 'c@lvin.me',
9+
url = 'https://github.com/scaleapi/scale-api-python-client',
10+
download_url = 'https://github.com/scaleapi/scale-api-python-client/tarball/0.1',
11+
keywords = ['scale', 'scaleapi'],
12+
classifiers = [],
13+
)

0 commit comments

Comments
 (0)