Standalone lambda development ============================= Here is *standalone* lambda development description. .. note:: For a comprehensive reference of all available functions, classes, and constants for each lambda type, see the `Lambda reference table <./lambda_reference.html>`_. More information about lambda types and the differences between *standalone* and others available at `lambda types description <./lambda_types.html#standalone>`_. Standalone lambda requirements ------------------------------ A standalone lambda requirements has no extra requirements and determines only by user: if `lambda` suggests `Luna Events` usage - it requires `Luna Events` service. Standalone lambda development ----------------------------- A standalone lambda must be designed for detached from luna services requests processing, but it can access remote resources. The request to a standalone lambda can be used by user next ways: .. code-block:: :caption: lambda_main.py from luna_lambda_tools import StandaloneLambdaRequest, logger async def main(request: StandaloneLambdaRequest) -> dict: logger.info(request.json) # print request json to log logger.info(request.getBody()) # print request body as raw bytes logger.info(request.headers) # print request headers logger.info(request.args) # print dictionary with query arguments ... It is required to use `StandaloneLambdaRequest` imported from `luna_lambda_tools` request as argument to `main` function. Standalone lambda service clients --------------------------------- A *standalone* lambda can use the following luna services clients: ``events``, ``faces``, ``matcher``, ``faceSamplesStore``, ``bodySamplesStore``, ``imageOriginStore``, ``sdk``, ``videoAgent``, ``sender``, ``handlers`` and ``lambdas``. The ``lambdas`` client is always available, all other clients are initialized only when the corresponding service is enabled for the lambda by the *ADDITIONAL_SERVICES_USAGE* configurator setting; ``objectOriginStore`` and ``api`` clients are not available for this lambda type. Clients are accessed through the request object and have the same interface in every lambda type which provides them: .. code-block:: :caption: lambda_main.py from luna_lambda_tools import StandaloneLambdaRequest async def main(request: StandaloneLambdaRequest) -> dict: request.clients.events.saveEvents(events=[...]) request.clients.sender.publish(...) return {"status": "success"} Accessing a client of a disabled service raises ``UserTaskLambdaException``, so a service can be used as an optional dependency, see the `service availability check <./development.html#luna-services-clients>`_. Every client method with its parameters and the wrapper source code is described in the `clients reference <./luna_clients.html>`_. Standalone lambda examples -------------------------- - Here is an example of standalone lambda which works with numbers, that getting from external resources: .. literalinclude:: examples/201/standalone_lambda_development/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_development/make_request.py :caption: request example :language: python The `luna-lambda-tools` provides some stuff whose may be used to create a standalone lambda: .. raw:: html
standalone lambda stuff description .. autosummary:: :toctree: _autosummary :recursive: luna_lambda_tools.public.standalone .. raw:: html
- A lambda example which gets face detection and estimate mask directly using the LUNA SDK (available at http://pypi.vlabs/root/public/lunavl) The LUNA python SDK must be added it to requirements.txt (`requirements description available here `_) .. literalinclude:: examples/201/standalone_lambda_sdk/lambda/requirements.txt :caption: requirements.txt .. note:: To develop a lambda with lunasdk locally it needs LUNA FSDK python bindings to be installed previously. .. warning:: Such a user lambda requires the SDK data available from user lambda (the `fsdk/data` folder next to the `lambda_main.py` main file) The LUNA SDK is available on the company release portal which must be extracted to the `fsdk` folder (see the archive file structure below). The only thing which is needed is *data* folder with used fsdk plans and config files (*faceengine.conf* and *runtime.conf*). It is recommended to not include irrelevant plans from the folder in the archive to decrease result lambda image size. .. code-block:: :caption: Archive file structure with files required for the example ├──lambda_main.py ├──requirements.txt └──fsdk └──data ├──faceengine.conf ├──runtime.conf ├──FaceDet_v3_a5_cpu-avx2.plan ├──FaceDet_v3_redetect_v3_cpu-avx2.plan ├──LNet_precise_v2_cpu-avx2.plan ├──mask_clf_v3_cpu-avx2.plan └──slnet_v6_cpu-avx2.plan .. literalinclude:: examples/201/standalone_lambda_sdk/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_sdk/make_request.py :caption: request example :language: python - A lambda example which extracts faces from a video and matches them against faces from a list: .. literalinclude:: examples/201/standalone_lambda_match_faces_from_video/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_match_faces_from_video/make_request.py :caption: request example :language: python - A lambda example which checks whether the *Luna-Events* service is enabled and return reply with the *on*/*off* text if *Luna-Events* is enabled or disabled respectively. Any other additional service (*Luna-Handlers*/*Luna-Sender*/etc) status can be checked in the same way, see the `clients description `_ for details. .. literalinclude:: examples/201/service_disabled/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/service_disabled/make_request.py :caption: request example :language: python - A lambda example which serves static files, for example a custom UI. .. literalinclude:: examples/200/standalone_lambda_static_routes/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/200/standalone_lambda_static_routes/make_request.py :caption: request example :language: python .. code-block:: :caption: Archive file structure with files required for the example ├── lambda_main.py └── static └── my_ui └── index.html - A lambda example which uses `poetry` as dependencies installer. .. literalinclude:: examples/201/standalone_lambda_poetry_dependencies/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_poetry_dependencies/lambda/pyproject.toml :caption: pyproject.toml :language: text .. literalinclude:: examples/201/standalone_lambda_poetry_dependencies/make_request.py :caption: request example :language: python - A lambda example with an ML model This example demonstrates how to run minimalistic Lambda around image classification ONNX model. for more details please see (`"source demo" `_) .. note:: The example below uses an image classification model (`"resnet50-v2-7.onnx" `_) and a json file (`"imagenet-simple-labels.json" `_) from `github `_. To make this example works it needs to get this files from github and place into */model/data* folder of the lambda agent archive. .. code-block:: :caption: Archive file structure with files required for the example ├──lambda_main.py ├──poetry.lock ├──pyproject.toml └──model ├──config.py ├──model.py └──data ├──resnet50-v2-7.onnx └──imagenet-simple-labels.json Necessary dependencies for this example: .. raw:: html
*pyproject.toml* .. literalinclude:: examples/201/standalone_lambda_ml_model/lambda/pyproject.toml :caption: pyproject.toml :language: toml .. raw:: html
| .. literalinclude:: examples/201/standalone_lambda_ml_model/lambda/model/config.py :caption: config.py :language: python .. literalinclude:: examples/201/standalone_lambda_ml_model/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_ml_model/lambda/model/model.py :caption: model.py :language: text .. literalinclude:: examples/201/standalone_lambda_ml_model/make_request.py :caption: request example :language: python - A lambda example of parsing multipart input with 2 images and json. .. literalinclude:: examples/201/standalone_lambda_multipart_parsing/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_multipart_parsing/make_request.py :caption: request example :language: python - A lambda example which communicate with another lambda: .. literalinclude:: examples/201/standalone_lambda_communication/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_communication/other_lambda/lambda_main.py :caption: other_lambda_main.py :language: python .. literalinclude:: examples/201/standalone_lambda_communication/make_request.py :caption: request example :language: python - A lambda example which communicate with another lambda by another lambda id from the user config. To reproduce this example, you must add `user_config` with `otherLambdaId` to the request body of `create lambda <./_static/api.html#operation/createLambda>`_. .. code-block:: none { "archive": ..., "credentials": ..., "parameters": {"user_config": {"otherLambdaId": ...}}, } .. literalinclude:: examples/201/standalone_communication_user_config/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/standalone_communication_user_config/other_lambda/lambda_main.py :caption: other_lambda_main.py :language: python .. literalinclude:: examples/201/standalone_communication_user_config/make_request.py :caption: request example :language: python