-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
110 lines (72 loc) · 2.65 KB
/
service.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
import asyncio
import json
import signal
from tornado.options import parse_command_line, define
import uvloop
import torchvision.models as models
from classifier import ImageClassifier
from web_app import make_app
class PredicateService(object):
def __init__(self):
self.web_app = None
self.model = None
# close your eyes and go forward
# TODO: move to constants or options
with open('imagenet_class_index.json', 'r') as f:
self.classes_map = json.load(f)
self._define_options()
self.options = self._parse_options()
self._setup_signal_handlers()
self._initialize()
@classmethod
def _define_options(cls):
define('host', type=str, default='127.0.0.1')
define('port', type=int, default=8000)
define('debug', type=bool, default=False)
@classmethod
def _parse_options(cls):
# it's igly but I don't want to define tornado options in global space
from tornado.options import options as tornado_options
parse_command_line()
return tornado_options
def _shutdown_service(self):
loop = asyncio.get_running_loop()
for task in asyncio.all_tasks():
task.cancel()
loop.stop()
def _setup_signal_handlers(self):
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, self._shutdown_service)
loop.add_signal_handler(signal.SIGTERM, self._shutdown_service)
def _initialize_web_app(self):
return make_app(self, self.options)
def _initialize_model(self):
# freeze the loop in first time
model = models.alexnet(pretrained=True)
model.eval()
return model
def _initialize(self):
self.web_app = self._initialize_web_app()
self.model = self._initialize_model()
async def process_image(self, image_bytes):
loop = asyncio.get_running_loop()
classifier = ImageClassifier(self.model, self.classes_map)
# run in separate process, GIL will work, but in future we can change it for another executor
result = await loop.run_in_executor(None, classifier.classify, image_bytes)
prod, class_ = result
if not class_:
return {'result': None}
return {'result': class_[1]}
def run(self):
self.web_app.listen(self.options.port, address=self.options.host)
def main():
# asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
service = PredicateService()
service.run()
try:
loop.run_forever()
finally:
loop.close()
if __name__ == '__main__':
main()