forked from scaleapi/scaleapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
174 lines (141 loc) · 7.13 KB
/
__init__.py
File metadata and controls
174 lines (141 loc) · 7.13 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from collections import namedtuple
import requests
from .tasks import Task
DEFAULT_FIELDS = {'callback_url', 'instruction', 'urgency', 'metadata'}
ALLOWED_FIELDS = {'categorization': {'attachment', 'attachment_type', 'categories',
'category_ids', 'allow_multiple'},
'transcription': {'attachment', 'attachment_type',
'fields', 'repeatable_fields'},
'phonecall': {'attachment', 'attachment_type', 'phone_number',
'script', 'entity_name', 'fields', 'choices'},
'comparison': {'attachments', 'attachment_type',
'fields', 'choices'},
'annotation': {'attachment', 'attachment_type', 'instruction',
'objects_to_annotate', 'with_labels', 'examples',
'min_width', 'min_height', 'layers'},
'polygonannotation': {'attachment', 'attachment_type', 'instruction',
'objects_to_annotate', 'with_labels', 'layers'},
'lineannotation': {'attachment', 'attachment_type', 'instruction',
'objects_to_annotate', 'with_labels', 'splines', 'layers'},
'cuboidannotation': {'attachment', 'attachment_type', 'instruction',
'objects_to_annotate', 'min_width', 'min_height', 'with_labels', 'layers'},
'datacollection': {'attachment', 'attachment_type', 'fields'},
'audiotranscription': {'attachment', 'attachment_type', 'verbatim', 'phrases'}}
SCALE_ENDPOINT = 'https://api.scaleapi.com/v1/'
DEFAULT_LIMIT = 100
DEFAULT_OFFSET = 0
def validate_payload(task_type, kwargs):
allowed_fields = DEFAULT_FIELDS | ALLOWED_FIELDS[task_type]
for k in kwargs:
if k not in allowed_fields:
raise ScaleInvalidRequest('Illegal parameter %s for task_type %s'
% (k, task_type), None)
class ScaleException(Exception):
def __init__(self, message, errcode):
super(ScaleException, self).__init__(message)
self.code = errcode
class ScaleInvalidRequest(ScaleException, ValueError):
pass
class Tasklist(list):
def __init__(self, docs, total, limit, offset, has_more):
super(Tasklist, self).__init__(docs)
self.docs = docs
self.total = total
self.limit = limit
self.offset = offset
self.has_more = has_more
class ScaleClient(object):
def __init__(self, api_key):
self.api_key = api_key
def _getrequest(self, endpoint, params={}):
"""Makes a get request to an endpoint.
If an error occurs, assumes that endpoint returns JSON as:
{ 'status_code': XXX,
'error': 'I failed' }
"""
r = requests.get(SCALE_ENDPOINT + endpoint,
headers={"Content-Type": "application/json"},
auth=(self.api_key, ''), params=params)
if r.status_code == 200:
return r.json()
raise ScaleException(r.json()['error'], r.status_code)
def _postrequest(self, endpoint, payload=None):
"""Makes a post request to an endpoint.
If an error occurs, assumes that endpoint returns JSON as:
{ 'status_code': XXX,
'error': 'I failed' }
"""
payload = payload or {}
r = requests.post(SCALE_ENDPOINT + endpoint, json=payload,
headers={"Content-Type": "application/json"},
auth=(self.api_key, ''))
if r.status_code == 200:
return r.json()
if r.status_code == 400:
raise ScaleInvalidRequest(r.json()['error'], r.status_code)
raise ScaleException(r.json()['error'], r.status_code)
def fetch_task(self, task_id):
"""Fetches a task.
Returns the associated task.
"""
return Task(self._getrequest('task/%s' % task_id), self)
def cancel_task(self, task_id):
"""Cancels a task.
Returns the associated task.
Raises a ScaleException if it has already been canceled.
"""
return Task(self._postrequest('task/%s/cancel' % task_id), self)
def tasks(self, **kwargs):
"""Returns a list of your tasks.
Returns up to 100 at a time, to get more use the offset param.
start/end_time are ISO8601 dates, the time range of tasks to fetch.
status can be 'completed', 'pending', or 'canceled'.
type is the task type.
limit is the max number of results to display per page,
offset is the number of results to skip (for showing more pages).
"""
allowed_kwargs = {'start_time', 'end_time', 'status', 'type', 'limit', 'offset'}
for key in kwargs:
if key not in allowed_kwargs:
raise ScaleInvalidRequest('Illegal parameter %s for ScaleClient.tasks()'
% key, None)
response = self._getrequest('tasks', params=kwargs)
docs = [Task(json, self) for json in response['docs']]
return Tasklist(docs, response['total'], response['limit'],
response['offset'], response['has_more'])
def create_categorization_task(self, **kwargs):
validate_payload('categorization', kwargs)
taskdata = self._postrequest('task/categorize', payload=kwargs)
return Task(taskdata, self)
def create_transcription_task(self, **kwargs):
validate_payload('transcription', kwargs)
taskdata = self._postrequest('task/transcription', payload=kwargs)
return Task(taskdata, self)
def create_phonecall_task(self, **kwargs):
validate_payload('phonecall', kwargs)
taskdata = self._postrequest('task/phonecall', payload=kwargs)
return Task(taskdata, self)
def create_comparison_task(self, **kwargs):
validate_payload('comparison', kwargs)
taskdata = self._postrequest('task/comparison', payload=kwargs)
return Task(taskdata, self)
def create_annotation_task(self, **kwargs):
validate_payload('annotation', kwargs)
taskdata = self._postrequest('task/annotation', payload=kwargs)
return Task(taskdata, self)
def create_polygonannotation_task(self, **kwargs):
validate_payload('polygonannotation', kwargs)
taskdata = self._postrequest('task/polygonannotation', payload=kwargs)
return Task(taskdata, self)
def create_lineannotation_task(self, **kwargs):
validate_payload('lineannotation', kwargs)
taskdata = self._postrequest('task/lineannotation', payload=kwargs)
return Task(taskdata, self)
def create_datacollection_task(self, **kwargs):
validate_payload('datacollection', kwargs)
taskdata = self._postrequest('task/datacollection', payload=kwargs)
return Task(taskdata, self)
def create_audiotranscription_task(self, **kwargs):
validate_payload('audiotranscription', kwargs)
taskdata = self._postrequest('task/audiotranscription', payload=kwargs)
return Task(taskdata, self)