Standalone lambda development
Here is standalone lambda development description.
More information about lambda types and differences of standalone and others available at lambda types description.
Standalone lambda requirements
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
The standalone lambda must be designed for detached from luna services requests processing, but it can access remote resources.
The request to standalone lambda can be used by user next ways:
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
...
It is required to use StandaloneLambdaRequest imported from luna_lambda_tools request as argument to main function.
Standalone lambda examples
Here is an example of standalone lambda which works with numbers, that getting from external resources:
import aiohttp from luna_lambda_tools import StandaloneLambdaRequest, logger class UserCtx: def __init__(self): self.session = None async def onStart(self): logger.info("start lambda") self.session = aiohttp.ClientSession() async def onShutdown(self): logger.info("shutdown lambda") await self.session.close() async def main(request: StandaloneLambdaRequest) -> dict: """ Supposed request structure: ```json {"url": "http://very-useful-url"} ``` """ logger.info("Request processing starts") url = request.json.get("url") async with request.userCtx.session.get(url) as resp: statusCode = resp.status logger.info("Request processing finished") result = f"{url} available" if statusCode == 200 else f"{url} is not available" return {"result": result}
from luna3.luna_lambda.luna_lambda import LambdaApi SERVER_ORIGIN = "http://lambda_address:lambda_port" # Replace by your values before start SERVER_API_VERSION = 1 lambdaApi = LambdaApi(origin=SERVER_ORIGIN, api=SERVER_API_VERSION) lambdaId, accountId = "your_lambda_id", "your_account_id" # Replace by your values before start def makeRequest(): data = { "url": "https://docs.visionlabs.ai/luna", } reply = lambdaApi.proxyLambdaPost(lambdaId=lambdaId, path="main", accountId=accountId, body=data) return reply if __name__ == "__main__": response = makeRequest() print(response.json)
The luna-lambda-tools provides some stuff whose may be used to create standalone lambda:
standalone lambda stuff description
Module contains some stuff for standalone lambda
The lambda example which gets face detection and estimate mask directly using the LUNA SDK (available at https://github.com/VisionLabs/lunasdk)
LUNA python SDK must be added it to requirements.txt (requirements description available here)
git+https://github.com/VisionLabs/lunasdk.git
Note
To develop lambda with lunasdk locally it needs LUNA FSDK python bindings to be installed previously.
Warning
Such user lambda requires LUNA SDK data available from user lambda (the fsdk/data folder near the lambda_main.py main file) The LUNA SDK is available on VL release portal which must be extracted to fsdk folder (see 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 not using plans from folder in archive to decrease result lambda image size.
├──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_v5_cpu-avx2.plan
from luna_lambda_tools import StandaloneLambdaRequest, UserException from lunavl.sdk.faceengine.engine import VLFaceEngine from lunavl.sdk.faceengine.setting_provider import DetectorType from lunavl.sdk.image_utils.image import VLImage class FaceDetectionException(UserException): statusCode = 400 errorText = "failed to get face detection from image" async def main(request: StandaloneLambdaRequest) -> dict: image = VLImage(body=request.getBody()) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) detections = detector.detect([image]) if not detections: raise FaceDetectionException faceDetections = detections[0] warper = faceEngine.createFaceWarper() warps = [warper.warp(faceDetection) for faceDetection in faceDetections] maskEstimator = faceEngine.createMaskEstimator() mask = await maskEstimator.estimate(warps[0].warpedImage, asyncEstimate=True) return {"results": mask.asDict()}
from luna3.luna_lambda.luna_lambda import LambdaApi SERVER_ORIGIN = "http://lambda_address:lambda_port" # Replace by your values before start SERVER_API_VERSION = 1 lambdaApi = LambdaApi(origin=SERVER_ORIGIN, api=SERVER_API_VERSION) lambdaId, accountId = "your_lambda_id", "your_account_id" # Replace by your values before start def getImage(pathToImage): """ Make sure pathToImage is valid path to specified image """ with open(pathToImage, "rb") as file: return file.read() def makeRequest(): reply = lambdaApi.proxyLambdaPost(lambdaId=lambdaId, path="main", accountId=accountId, body=getImage("empty.jpeg")) return reply if __name__ == "__main__": response = makeRequest() print(response.json)