-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisyphe.py
196 lines (167 loc) · 7.38 KB
/
sisyphe.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import http.server
import socketserver
from http import HTTPStatus
import os
import mimetypes
import json
import csv
import webbrowser
PORT = 8003
class Sisyphe:
"""Sisyphe: contains the data labelling logic.
This class has a few methods that enable the request handler to interact
with the data.
"""
def __init__(self,
features,
categories,
logfile,
save_callback=lambda x: x,
render_callback=lambda x: x,
resume=False,
multilabel=False,
model=None,
sep='\t',
label_sep='|'):
"""The Sisyphe constructor.
Args:
features (dict): A dictionnary that maps ids to example objects.
categories (:obj:`list` of :obj:`str`): A list of all possible
categories as strings.
logfile (str): File where the labels will be logged, line by line and
a tab separates the id and the label string.
save_callback (function): A callback that takes the label dictionnary
as a parameter and saves it in a user-defined way.
render_callback (function): A callback that takes the example object
and returns an html string that will be displayed in the UI.
resume (bool): Remove the ids that are present in the `logfile` to
continue the work from where it was stopped.
multilabel (bool): Informs whether multiple labels are allowed.
model (obj): An online prediction model that has `predict` and
`update` methods and understands the example objects and labels.
sep (str): A string to separate the id and the label in the log file.
label_sep (str): String to separate the labels if `multilabel` is set
to `True`.
"""
self.model = model
self.multilabel = multilabel
self.categories = categories
self.save_callback = save_callback
self.render_callback = render_callback
self.features = features
self.logfile = logfile
self.sep = sep
self.label_sep = label_sep
self.labels = {}
self.todo = set(features)
key_type = type(next(iter(self.todo)))
if resume:
with open(logfile, newline='') as csvfile:
previous = csv.reader(csvfile, delimiter=sep)
for row in previous:
try:
self.todo.discard(key_type(row[0]))
except (TypeError, ValueError, KeyError) as e:
raise ValueError("This exception was " + \
"caused by the fact that the index from `logfile` could " + \
"not be cast to the type of keys from `features`. Either " + \
"pass `resume=False`, check that the identifier types are " + \
"consistent or check that there is no header line in the " + \
"`logfile`.") from e
def update(self, identificator, label):
self.labels[identificator] = label
self.todo.discard(identificator)
if self.model:
self.model.update(self.features[identificator], label)
with open(self.logfile, 'a') as f:
f.write(f"{identificator}{self.sep}{label.replace(os.linesep, '')}" + '\n')
def save(self):
self.save_callback(self.labels)
def get(self, identificator=None):
if identificator == None:
if len(self.todo):
identificator = next(iter(self.todo))
else:
return {'progress': 'done'}
result = {'id': identificator,
'example': self.render_callback(self.features[identificator]),
'progress': self.progress()}
if self.model:
result['probabilities'] = self.model.predict(self.features[identificator])
return result
def progress(self):
return len(self.features), len(self.todo)
def description(self):
return {'categories': self.categories,
'multilabel': self.multilabel,
'labelsep': self.label_sep}
def create_handler(sisyphe):
class SisypheRequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
head, tail = os.path.split(self.path)
if head == '/':
if tail == '':
self.return_file('frontend/index.html')
elif '.' in tail:
self.return_file('frontend/' + tail)
elif tail == 'save':
sisyphe.save()
self.return_object({'saved': True})
elif tail == 'job':
self.return_object(sisyphe.description())
elif tail == 'example':
self.return_object(sisyphe.get())
elif head == '/example':
example = sisyphe.get(int(tail))
self.return_object(example)
else:
print("path not registered", self.path)
def do_POST(self):
if self.path == '/label':
obj = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
sisyphe.update(obj['id'], obj['label'])
self.success()
else:
raise ValueError('unknown path {self.path}')
def success(self):
self.send_response_only(HTTPStatus.OK)
self.send_header("Content-type", 'text/plain')
self.send_header("Content-Length", '6')
self.end_headers()
self.wfile.write(b'thanks')
def fail(self, message):
message = message.encode('utf8')
self.send_response_only(HTTPStatus.NOT_ACCEPTABLE)
self.send_header("Content-type", 'text/plain')
self.send_header("Content-Length", len(message))
self.end_headers()
self.wfile.write(message)
def return_file(self, file):
f = open(file, 'rb')
fs = os.fstat(f.fileno())
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", mimetypes.guess_type(file)[0])
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
self.wfile.write(f.read())
f.close()
def return_object(self, obj):
obj_str = json.dumps(obj).replace('NaN', 'null').encode('utf8')
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", 'application/json')
self.send_header("Content-Length", str(len(obj_str)))
self.end_headers()
self.wfile.write(obj_str)
def return_error(self, message):
self.send_response(HTTPStatus.NOT_OK)
self.send_header("Content-type", 'text/plain')
self.send_header("Content-Length", str(len(message)))
self.end_headers()
self.wfile.write(message)
return SisypheRequestHandler
def run(sisyphe):
webbrowser.open_new_tab(f"http://localhost:{PORT}/")
with socketserver.TCPServer(("", PORT), create_handler(sisyphe)) as httpd:
print("serving at port", PORT)
httpd.serve_forever()