Steven C
commited on
Commit
•
e868a55
1
Parent(s):
8e71bd4
Lambda - Add handler module
Browse files- app.py +1 -0
- handler.py +25 -0
app.py
CHANGED
@@ -32,6 +32,7 @@ class DocumentParserModel:
|
|
32 |
onnx.checker.check_model(onnx_model)
|
33 |
return rt.InferenceSession(model_path)
|
34 |
|
|
|
35 |
def predict_text(self, image_path):
|
36 |
try:
|
37 |
with Image.open(image_path) as img_org:
|
|
|
32 |
onnx.checker.check_model(onnx_model)
|
33 |
return rt.InferenceSession(model_path)
|
34 |
|
35 |
+
# TODO: test with image blob
|
36 |
def predict_text(self, image_path):
|
37 |
try:
|
38 |
with Image.open(image_path) as img_org:
|
handler.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Ref. [Deploying a Machine Learning Model to AWS Lambda | TestDriven.io](https://testdriven.io/blog/ml-model-aws-lambda/)
|
2 |
+
import logging
|
3 |
+
from app import DocumentParserModel
|
4 |
+
|
5 |
+
# initialize logger and model during Lambda's cold start
|
6 |
+
LOGGER = logging.getLogger()
|
7 |
+
LOGGER.setLevel(logging.INFO)
|
8 |
+
|
9 |
+
model_path = "captcha.onnx"
|
10 |
+
img_size = (32, 128)
|
11 |
+
charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
12 |
+
model = DocumentParserModel(
|
13 |
+
model_path=model_path,
|
14 |
+
img_size=img_size,
|
15 |
+
charset=charset,
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
def lambda_handle(event, context):
|
20 |
+
# Only used to keep the Lambda warm
|
21 |
+
if event.get("source") == "KEEP_LAMBDA_WARM":
|
22 |
+
LOGGER.info("No ML work to do. Just staying warm...")
|
23 |
+
return "Keeping Lambda warm"
|
24 |
+
|
25 |
+
return {"statusCode": 200, "vc": model.predict_text(image_path=event["image_path"])}
|