From 5d1b81e54be239008aa20a443e0ba0c4b8198c9b Mon Sep 17 00:00:00 2001 From: Michael Duong Date: Tue, 29 Aug 2023 19:35:18 +0100 Subject: [PATCH] add basic handler for now --- .../Dockerfiles/Dockerfile.prediction.lambda | 14 ++++++++++ .../Dockerfiles/Dockerfile.training.lambda | 14 ++++++++++ .../handlers/predictions_app.py | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 model_data/simulation_system/Dockerfiles/Dockerfile.prediction.lambda create mode 100644 model_data/simulation_system/Dockerfiles/Dockerfile.training.lambda create mode 100644 model_data/simulation_system/handlers/predictions_app.py diff --git a/model_data/simulation_system/Dockerfiles/Dockerfile.prediction.lambda b/model_data/simulation_system/Dockerfiles/Dockerfile.prediction.lambda new file mode 100644 index 00000000..8f9a045e --- /dev/null +++ b/model_data/simulation_system/Dockerfiles/Dockerfile.prediction.lambda @@ -0,0 +1,14 @@ +FROM public.ecr.aws/lambda/python:3.10 + +# Install python packages +COPY ../requirements/predictions/predictions.txt requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the project code to user home directory +COPY ./core ${LAMBDA_TASK_ROOT}/simulation_system/core +COPY ./MLModel ${LAMBDA_TASK_ROOT}/simulation_system/MLModel +COPY ./predictions.py ${LAMBDA_TASK_ROOT}/simulation_system/predictions.py +COPY ./handlers/predictions_app.py ${LAMBDA_TASK_ROOT}/simulation_system/predictions_app.py + +# Run off a lambda trigger +CMD [ "prediction_app.handler" ] diff --git a/model_data/simulation_system/Dockerfiles/Dockerfile.training.lambda b/model_data/simulation_system/Dockerfiles/Dockerfile.training.lambda new file mode 100644 index 00000000..34d3fe42 --- /dev/null +++ b/model_data/simulation_system/Dockerfiles/Dockerfile.training.lambda @@ -0,0 +1,14 @@ +FROM public.ecr.aws/lambda/python:3.10 + +# Install python packages +COPY ../requirements/training/training.txt requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the project code to user home directory +COPY ./core ${LAMBDA_TASK_ROOT}/simulation_system/core +COPY ./MLModel ${LAMBDA_TASK_ROOT}/simulation_system/MLModel +COPY ./training.py ${LAMBDA_TASK_ROOT}/simulation_system/training.py +COPY ./handlers/training_app.py ${LAMBDA_TASK_ROOT}/simulation_system/training_app.py + +# Run off a lambda trigger +CMD [ "training_app.handler" ] diff --git a/model_data/simulation_system/handlers/predictions_app.py b/model_data/simulation_system/handlers/predictions_app.py new file mode 100644 index 00000000..10d5b81e --- /dev/null +++ b/model_data/simulation_system/handlers/predictions_app.py @@ -0,0 +1,27 @@ +import os +import urllib.parse +from predictions import prediction + + +def handler(event, context): + """ + Take in event and trigger the prediction pipeline + """ + + # Assuming a file in a bucket landing for now? + # Assuming we have a model to use + + bucket = event["Records"][0]["s3"]["bucket"]["name"] + key = urllib.parse.unquote_plus( + event["Records"][0]["s3"]["bucket"]["key"], encoding="utf-8" + ) + + prediction_file = bucket + "/" + key + + # TODO: put a model into s3, both locally and in aws + model_path = os.environ.get("MODEL_PATH", "http://minio:9000/data/model_directory/") + + try: + prediction(model_path=model_path, data_path=prediction_file) + except (Exception, KeyError, ValueError): + print("Prediction failed")