| # Ref. [Deploying a Machine Learning Model to AWS Lambda | TestDriven.io](https://testdriven.io/blog/ml-model-aws-lambda/) | |
| import logging | |
| from app import DocumentParserModel | |
| # initialize logger and model during Lambda's cold start | |
| LOGGER = logging.getLogger() | |
| LOGGER.setLevel(logging.INFO) | |
| model = DocumentParserModel() | |
| def lambda_handle(event, context): | |
| # Only used to keep the Lambda warm | |
| if event.get("source") == "KEEP_LAMBDA_WARM": | |
| LOGGER.info("No ML work to do. Just staying warm...") | |
| return "Keeping Lambda warm" | |
| return {"statusCode": 200, "result": model.predict_text(image_blob=event["image_blob"])} | |