Skip to content

Commit 0e16f91

Browse files
committed
Simply setup_routes functions
1 parent ece48a3 commit 0e16f91

File tree

3 files changed

+163
-154
lines changed

3 files changed

+163
-154
lines changed

deepaas/api/v2/models.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,35 @@ async def index(request):
5454
return web.json_response({"models": models})
5555

5656

57-
class Handler(object):
58-
model_name = None
59-
model_obj = None
60-
61-
def __init__(self, model_name, model_obj):
62-
self.model_name = model_name
63-
self.model_obj = model_obj
64-
65-
@aiohttp_apispec.docs(
66-
tags=["models"],
67-
summary="Return model's metadata",
68-
)
69-
@aiohttp_apispec.response_schema(responses.ModelMeta(), 200)
70-
async def get(self, request):
71-
m = {
72-
"id": self.model_name,
73-
"name": self.model_name,
74-
"links": [{
75-
"rel": "self",
76-
"href": "%s" % request.path,
77-
}]
78-
}
79-
meta = self.model_obj.get_metadata()
80-
m.update(meta)
57+
def _get_handler(model_name, model_obj):
58+
class Handler(object):
59+
model_name = None
60+
model_obj = None
61+
62+
def __init__(self, model_name, model_obj):
63+
self.model_name = model_name
64+
self.model_obj = model_obj
65+
66+
@aiohttp_apispec.docs(
67+
tags=["models"],
68+
summary="Return model's metadata",
69+
)
70+
@aiohttp_apispec.response_schema(responses.ModelMeta(), 200)
71+
async def get(self, request):
72+
m = {
73+
"id": self.model_name,
74+
"name": self.model_name,
75+
"links": [{
76+
"rel": "self",
77+
"href": "%s" % request.path,
78+
}]
79+
}
80+
meta = self.model_obj.get_metadata()
81+
m.update(meta)
82+
83+
return web.json_response(m)
8184

82-
return web.json_response(m)
85+
return Handler(model_name, model_obj)
8386

8487

8588
def setup_routes(app):
@@ -89,5 +92,5 @@ def setup_routes(app):
8992
# different resources for each model. This way we can also load the
9093
# expected parameters if needed (as in the training method).
9194
for model_name, model_obj in model.V2_MODELS.items():
92-
hdlr = Handler(model_name, model_obj)
95+
hdlr = _get_handler(model_name, model_obj)
9396
app.router.add_get("/models/%s" % model_name, hdlr.get)

deepaas/api/v2/predict.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,45 @@ def _get_model_response(model_name, model_obj):
3232
return responses.Prediction
3333

3434

35+
def _get_handler(model_name, model_obj):
36+
args = webargs.core.dict2schema(model_obj.get_predict_args())
37+
response = _get_model_response(model_name, model_obj)
38+
39+
class Handler(object):
40+
model_name = None
41+
model_obj = None
42+
43+
def __init__(self, model_name, model_obj):
44+
self.model_name = model_name
45+
self.model_obj = model_obj
46+
47+
@aiohttp_apispec.docs(
48+
tags=["models"],
49+
summary="Make a prediction given the input data"
50+
)
51+
@aiohttp_apispec.querystring_schema(args)
52+
@aiohttp_apispec.response_schema(response(), 200)
53+
@aiohttp_apispec.response_schema(responses.Failure(), 400)
54+
@aiohttpparser.parser.use_args(args)
55+
async def post(self, request, args):
56+
task = self.model_obj.predict(**args)
57+
await task
58+
59+
ret = task.result()
60+
61+
if self.model_obj.has_schema:
62+
self.model_obj.validate_response(ret)
63+
return web.json_response(ret)
64+
65+
return web.json_response({"status": "OK", "predictions": ret})
66+
67+
return Handler(model_name, model_obj)
68+
69+
3570
def setup_routes(app):
3671
# In the next lines we iterate over the loaded models and create the
3772
# different resources for each model. This way we can also load the
3873
# expected parameters if needed (as in the training method).
3974
for model_name, model_obj in model.V2_MODELS.items():
40-
args = webargs.core.dict2schema(model_obj.get_predict_args())
41-
response = _get_model_response(model_name, model_obj)
42-
43-
class Handler(object):
44-
model_name = None
45-
model_obj = None
46-
47-
def __init__(self, model_name, model_obj):
48-
self.model_name = model_name
49-
self.model_obj = model_obj
50-
51-
@aiohttp_apispec.docs(
52-
tags=["models"],
53-
summary="Make a prediction given the input data"
54-
)
55-
@aiohttp_apispec.querystring_schema(args)
56-
@aiohttp_apispec.response_schema(response(), 200)
57-
@aiohttp_apispec.response_schema(responses.Failure(), 400)
58-
@aiohttpparser.parser.use_args(args)
59-
async def post(self, request, args):
60-
task = self.model_obj.predict(**args)
61-
await task
62-
63-
ret = task.result()
64-
65-
if self.model_obj.has_schema:
66-
self.model_obj.validate_response(ret)
67-
return web.json_response(ret)
68-
69-
return web.json_response({"status": "OK", "predictions": ret})
70-
71-
hdlr = Handler(model_name, model_obj)
75+
hdlr = _get_handler(model_name, model_obj)
7276
app.router.add_post("/models/%s/predict" % model_name, hdlr.post)

deepaas/api/v2/train.py

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -30,107 +30,109 @@
3030
LOG = log.getLogger("deepaas.api.v2.train")
3131

3232

33-
def setup_routes(app):
34-
# In the next lines we iterate over the loaded models and create the
35-
# different resources for each model. This way we can also load the
36-
# expected parameters if needed (as in the training method).
37-
for model_name, model_obj in model.V2_MODELS.items():
38-
args = webargs.core.dict2schema(model_obj.get_train_args())
39-
40-
class Handler(object):
41-
model_name = None
42-
model_obj = None
43-
44-
def __init__(self, model_name, model_obj):
45-
self.model_name = model_name
46-
self.model_obj = model_obj
47-
self._trainings = {}
48-
49-
def build_train_response(self, uuid_):
50-
training = self._trainings.get(uuid_, None)
51-
if training:
52-
ret = {}
53-
ret["date"] = training["date"]
54-
ret["uuid"] = uuid_
55-
56-
if training["task"].cancelled():
57-
ret["status"] = "cancelled"
58-
elif training["task"].done():
59-
exc = training["task"].exception()
60-
if exc:
61-
ret["status"] = "error"
62-
ret["message"] = "%s" % exc
63-
else:
64-
ret["status"] = "done"
65-
else:
66-
ret["status"] = "running"
67-
return ret
33+
def _get_handler(model_name, model_obj): # noqa
34+
args = webargs.core.dict2schema(model_obj.get_train_args())
35+
36+
class Handler(object):
37+
model_name = None
38+
model_obj = None
39+
40+
def __init__(self, model_name, model_obj):
41+
self.model_name = model_name
42+
self.model_obj = model_obj
43+
self._trainings = {}
44+
45+
def build_train_response(self, uuid_):
46+
training = self._trainings.get(uuid_, None)
47+
48+
if not training:
49+
return
50+
51+
ret = {}
52+
ret["date"] = training["date"]
53+
ret["uuid"] = uuid_
54+
55+
if training["task"].cancelled():
56+
ret["status"] = "cancelled"
57+
elif training["task"].done():
58+
exc = training["task"].exception()
59+
if exc:
60+
ret["status"] = "error"
61+
ret["message"] = "%s" % exc
6862
else:
69-
return None
70-
71-
@aiohttp_apispec.docs(
72-
tags=["models"],
73-
summary="Retrain model with available data"
74-
)
75-
@aiohttp_apispec.querystring_schema(args)
76-
@aiohttpparser.parser.use_args(args)
77-
async def post(self, request, args):
78-
uuid_ = uuid.uuid4().hex
79-
train_task = self.model_obj.train(**args)
80-
self._trainings[uuid_] = {
81-
"date": str(datetime.datetime.now()),
82-
"task": train_task,
83-
}
84-
ret = self.build_train_response(uuid_)
63+
ret["status"] = "done"
64+
else:
65+
ret["status"] = "running"
66+
return ret
67+
68+
@aiohttp_apispec.docs(
69+
tags=["models"],
70+
summary="Retrain model with available data"
71+
)
72+
@aiohttp_apispec.querystring_schema(args)
73+
@aiohttpparser.parser.use_args(args)
74+
async def post(self, request, args):
75+
uuid_ = uuid.uuid4().hex
76+
train_task = self.model_obj.train(**args)
77+
self._trainings[uuid_] = {
78+
"date": str(datetime.datetime.now()),
79+
"task": train_task,
80+
}
81+
ret = self.build_train_response(uuid_)
82+
return web.json_response(ret)
83+
84+
@aiohttp_apispec.docs(
85+
tags=["models"],
86+
summary="Cancel a running training"
87+
)
88+
async def delete(self, request):
89+
uuid_ = request.match_info["uuid"]
90+
training = self._trainings.get(uuid_, None)
91+
if not training:
92+
raise web.HTTPNotFound()
93+
training["task"].cancel()
94+
try:
95+
await asyncio.wait_for(training["task"], 5)
96+
except asyncio.TimeoutError:
97+
pass
98+
LOG.info("Training %s has been cancelled" % uuid_)
99+
ret = self.build_train_response(uuid_)
100+
return web.json_response(ret)
101+
102+
@aiohttp_apispec.docs(
103+
tags=["models"],
104+
summary="Get a list of trainings (running or completed)"
105+
)
106+
@aiohttp_apispec.response_schema(responses.TrainingList(), 200)
107+
async def index(self, request):
108+
ret = []
109+
for uuid_, training in self._trainings.items():
110+
aux = self.build_train_response(uuid_)
111+
ret.append(aux)
112+
113+
return web.json_response(ret)
114+
115+
@aiohttp_apispec.docs(
116+
tags=["models"],
117+
summary="Get status of a training"
118+
)
119+
@aiohttp_apispec.response_schema(responses.Training(), 200)
120+
async def get(self, request):
121+
uuid_ = request.match_info["uuid"]
122+
ret = self.build_train_response(uuid_)
123+
if ret:
85124
return web.json_response(ret)
125+
raise web.HTTPNotFound()
86126

87-
@aiohttp_apispec.docs(
88-
tags=["models"],
89-
summary="Cancel a running training"
90-
)
91-
async def delete(self, request):
92-
uuid_ = request.match_info["uuid"]
93-
training = self._trainings.get(uuid_, None)
94-
if training:
95-
training["task"].cancel()
96-
try:
97-
await asyncio.wait_for(training["task"], 5)
98-
except asyncio.TimeoutError:
99-
pass
100-
LOG.info("Training %s has been cancelled" % uuid_)
101-
ret = self.build_train_response(uuid_)
102-
return web.json_response(ret)
103-
else:
104-
raise web.HTTPNotFound()
105-
106-
@aiohttp_apispec.docs(
107-
tags=["models"],
108-
summary="Get a list of trainings (running or completed)"
109-
)
110-
@aiohttp_apispec.response_schema(responses.TrainingList(), 200)
111-
async def index(self, request):
112-
113-
ret = []
114-
for uuid_, training in self._trainings.items():
115-
aux = self.build_train_response(uuid_)
116-
ret.append(aux)
127+
return Handler(model_name, model_obj)
117128

118-
return web.json_response(ret)
119-
120-
@aiohttp_apispec.docs(
121-
tags=["models"],
122-
summary="Get status of a training"
123-
)
124-
@aiohttp_apispec.response_schema(responses.Training(), 200)
125-
async def get(self, request):
126-
uuid_ = request.match_info["uuid"]
127-
ret = self.build_train_response(uuid_)
128-
if ret:
129-
return web.json_response(ret)
130-
else:
131-
raise web.HTTPNotFound()
132129

133-
hdlr = Handler(model_name, model_obj)
130+
def setup_routes(app):
131+
# In the next lines we iterate over the loaded models and create the
132+
# different resources for each model. This way we can also load the
133+
# expected parameters if needed (as in the training method).
134+
for model_name, model_obj in model.V2_MODELS.items():
135+
hdlr = _get_handler(model_name, model_obj)
134136
app.router.add_post("/models/%s/train" % model_name, hdlr.post)
135137
app.router.add_get("/models/%s/train" % model_name, hdlr.index)
136138
app.router.add_get("/models/%s/train/{uuid}" % model_name, hdlr.get)

0 commit comments

Comments
 (0)