Ejemplo n.º 1
0
    async def configure_model(self, request):
        model_name = request.match_info["model"]
        label = request.match_info["label"]

        config = await request.json()

        try:
            model = Model.load_labeled(f"{label}={model_name}")
        except EntrypointNotFound as error:
            self.logger.error(
                f"/configure/model/ failed to load model: {error}")
            return web.json_response(
                {"error": f"model {model_name} not found"},
                status=HTTPStatus.NOT_FOUND,
            )

        try:
            model = model.withconfig(config)
        except MissingConfig as error:
            self.logger.error(
                f"failed to configure model {model_name}: {error}")
            return web.json_response({"error": str(error)},
                                     status=HTTPStatus.BAD_REQUEST)

        # DFFML objects all follow a double context entry pattern
        exit_stack = request.app["exit_stack"]
        model = await exit_stack.enter_async_context(model)
        request.app["models"][label] = model

        return web.json_response(OK)
Ejemplo n.º 2
0
 async def list_models(self, request):
     return web.json_response(
         {
             model.ENTRY_POINT_ORIG_LABEL: model.args({})
             for model in Model.load()
         },
         dumps=partial(json.dumps, cls=JSONEncoder),
     )