Model/model_data/simulation_system/handlers/predictions_app.py
2023-09-04 18:33:51 +01:00

46 lines
1.3 KiB
Python

import json
import os
import logging
from predictions import prediction
logger = logging.getLogger()
logger.setLevel(logging.INFO)
RUNTIME_ENVIRONMENT = os.environ.get("RUNTIME_ENVIRONMENT", "dev")
def handler(event, context):
"""
Take in event and trigger the prediction pipeline
"""
logger.info("received event: " + str(event))
# Assuming a file in a bucket landing for now?
# Assuming we have a model to use
body = json.loads(event["body"])
data_path = body["file_location"]
property_id = body["property_id"]
portfolio_id = body["portfolio_id"]
created_at = body["created_at"]
try:
# We could fix the model path but for the moment, we just take the best model path based on the registry
outputs = prediction(model_path=None, data_path=data_path)
# Store into s3, with key of {portfolio_id}-{property_id}
storage_filepath = f"s3://retrofit-sap-predictions-{RUNTIME_ENVIRONMENT}/{portfolio_id}/{property_id}/" \
f"{created_at}.csv"
outputs.to_csv(storage_filepath)
return storage_filepath
except (Exception, KeyError, ValueError) as e:
logger.info("Prediction failed")
logger.info(e)
if __name__ == "__main__":
handler()