|
30 | 30 | LOG = log.getLogger("deepaas.api.v2.train")
|
31 | 31 |
|
32 | 32 |
|
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 |
68 | 62 | 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: |
85 | 124 | return web.json_response(ret)
|
| 125 | + raise web.HTTPNotFound() |
86 | 126 |
|
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) |
117 | 128 |
|
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() |
132 | 129 |
|
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) |
134 | 136 | app.router.add_post("/models/%s/train" % model_name, hdlr.post)
|
135 | 137 | app.router.add_get("/models/%s/train" % model_name, hdlr.index)
|
136 | 138 | app.router.add_get("/models/%s/train/{uuid}" % model_name, hdlr.get)
|
|
0 commit comments