|
14 | 14 | # License for the specific language governing permissions and limitations
|
15 | 15 | # under the License.
|
16 | 16 |
|
17 |
| -import urllib.parse |
18 |
| - |
19 |
| -from aiohttp import web |
20 |
| -import aiohttp_apispec |
| 17 | +import fastapi |
21 | 18 |
|
22 | 19 | from deepaas.api.v2 import responses
|
23 | 20 | from deepaas import model
|
24 | 21 |
|
25 | 22 |
|
26 |
| -@aiohttp_apispec.docs( |
27 |
| - tags=["models"], |
| 23 | +router = fastapi.APIRouter(prefix="/models") |
| 24 | + |
| 25 | + |
| 26 | +@router.get( |
| 27 | + "/", |
28 | 28 | summary="Return loaded models and its information",
|
29 |
| - description="DEEPaaS can load several models and server them on the same " |
30 |
| - "endpoint, making a call to the root of the models namespace " |
31 |
| - "will return the loaded models, as long as their basic " |
32 |
| - "metadata.", |
| 29 | + description="Return list of DEEPaaS loaded models. In previous versions, DEEPaaS " |
| 30 | + "could load several models and serve them on the same endpoint.", |
| 31 | + tags=["models"], |
| 32 | + response_model=responses.ModelList, |
33 | 33 | )
|
34 |
| -@aiohttp_apispec.response_schema(responses.ModelMeta(), 200) |
35 |
| -async def index(request): |
36 |
| - """Return loaded models and its information. |
| 34 | +async def index_models( |
| 35 | + request: fastapi.Request, |
| 36 | +): |
| 37 | + """Return loaded models and its information.""" |
| 38 | + |
| 39 | + name = model.V2_MODEL_NAME |
| 40 | + model_obj = model.V2_MODEL |
| 41 | + m = { |
| 42 | + "id": name, |
| 43 | + "name": name, |
| 44 | + "links": [ |
| 45 | + { |
| 46 | + "rel": "self", |
| 47 | + "href": str(request.url_for("get_model/" + name)), |
| 48 | + } |
| 49 | + ], |
| 50 | + } |
| 51 | + meta = model_obj.get_metadata() |
| 52 | + m.update(meta) |
| 53 | + return {"models": [m]} |
37 | 54 |
|
38 |
| - DEEPaaS can load several models and server them on the same endpoint, |
39 |
| - making a call to the root of the models namespace will return the |
40 |
| - loaded models, as long as their basic metadata. |
41 |
| - """ |
42 | 55 |
|
43 |
| - models = [] |
44 |
| - for name, obj in model.V2_MODELS.items(): |
45 |
| - m = { |
46 |
| - "id": name, |
47 |
| - "name": name, |
48 |
| - "links": [ |
49 |
| - { |
50 |
| - "rel": "self", |
51 |
| - "href": urllib.parse.urljoin("%s/" % request.path, name), |
52 |
| - } |
53 |
| - ], |
54 |
| - } |
55 |
| - meta = obj.get_metadata() |
56 |
| - m.update(meta) |
57 |
| - models.append(m) |
58 |
| - return web.json_response({"models": models}) |
59 |
| - |
60 |
| - |
61 |
| -def _get_handler(model_name, model_obj): |
| 56 | +def _get_handler_for_model(model_name, model_obj): |
| 57 | + """Auxiliary function to get the handler for a model. |
| 58 | +
|
| 59 | + This function returns a handler for a model that can be used to |
| 60 | + register the routes in the router. |
| 61 | + """ |
62 | 62 | class Handler(object):
|
| 63 | + """Class to handle the model metadata endpoints.""" |
63 | 64 | model_name = None
|
64 | 65 | model_obj = None
|
65 | 66 |
|
66 | 67 | def __init__(self, model_name, model_obj):
|
67 | 68 | self.model_name = model_name
|
68 | 69 | self.model_obj = model_obj
|
69 | 70 |
|
70 |
| - @aiohttp_apispec.docs( |
71 |
| - tags=["models"], |
72 |
| - summary="Return model's metadata", |
73 |
| - ) |
74 |
| - @aiohttp_apispec.response_schema(responses.ModelMeta(), 200) |
75 |
| - async def get(self, request): |
| 71 | + async def get(self, request: fastapi.Request): |
| 72 | + """Return model's metadata.""" |
76 | 73 | m = {
|
77 | 74 | "id": self.model_name,
|
78 | 75 | "name": self.model_name,
|
79 | 76 | "links": [
|
80 | 77 | {
|
81 | 78 | "rel": "self",
|
82 |
| - "href": request.path.rstrip("/"), |
| 79 | + "href": str(request.url), |
83 | 80 | }
|
84 | 81 | ],
|
85 | 82 | }
|
86 | 83 | meta = self.model_obj.get_metadata()
|
87 | 84 | m.update(meta)
|
88 | 85 |
|
89 |
| - return web.json_response(m) |
| 86 | + return m |
| 87 | + |
| 88 | + def register_routes(self, router): |
| 89 | + """Register routes for the model in the router.""" |
| 90 | + router.add_api_route( |
| 91 | + f"/{self.model_name}", |
| 92 | + self.get, |
| 93 | + name="get_model/" + self.model_name, |
| 94 | + summary="Return model's metadata", |
| 95 | + tags=["models"], |
| 96 | + response_model=responses.ModelMeta, |
| 97 | + ) |
90 | 98 |
|
91 | 99 | return Handler(model_name, model_obj)
|
92 | 100 |
|
93 | 101 |
|
94 |
| -def setup_routes(app): |
95 |
| - app.router.add_get("/models/", index, allow_head=False) |
| 102 | +def get_router() -> fastapi.APIRouter: |
| 103 | + """Auxiliary function to get the router. |
| 104 | +
|
| 105 | + We use this function to be able to include the router in the main |
| 106 | + application and do things before it gets included. |
| 107 | +
|
| 108 | + In this case we explicitly include the model's endpoints. |
| 109 | +
|
| 110 | + """ |
| 111 | + model_name = model.V2_MODEL_NAME |
| 112 | + model_obj = model.V2_MODEL |
| 113 | + |
| 114 | + hdlr = _get_handler_for_model(model_name, model_obj) |
| 115 | + hdlr.register_routes(router) |
96 | 116 |
|
97 |
| - # In the next lines we iterate over the loaded models and create the |
98 |
| - # different resources for each model. This way we can also load the |
99 |
| - # expected parameters if needed (as in the training method). |
100 |
| - for model_name, model_obj in model.V2_MODELS.items(): |
101 |
| - hdlr = _get_handler(model_name, model_obj) |
102 |
| - app.router.add_get("/models/%s/" % model_name, hdlr.get, allow_head=False) |
| 117 | + return router |
0 commit comments