clean up prediction app and add logging

This commit is contained in:
Michael Duong 2025-11-03 23:11:26 +00:00
parent b333b80d5c
commit 87b3c27986

View file

@ -47,6 +47,23 @@ def upload_dataframe_to_s3(df, bucket, s3_file_name):
return False
def warming_up_invocation(model_filepath: str):
"""
Function to handle warm up invocations
"""
import pandas as pd
model = model_factory(settings.build_model["model_type"])
model_filepath = settings.build_model["model_save_filepath"]
model.load_model(model_filepath)
warmup_df = pd.DataFrame(columns=model.model.original_features)
warmup_df = pd.concat([warmup_df.T, pd.DataFrame([0] * len(warmup_df.T))], axis=1).T
warmup_df.fillna(0, inplace=True)
model.predict(data=warmup_df)
def handler(event, context):
"""
Take in event and trigger the prediction pipeline
@ -77,32 +94,27 @@ def handler(event, context):
model_filepath = build_model_params["model_save_filepath"]
if "testing" in body:
storage_filepath = body["file_location"].replace(
".parquet", "_output.parquet"
)
elif "warm" in body:
logger.info("Warm up invocation - skipping prediction")
if "warm" in body:
logger.info("Warm up invocation - synthetic prediction")
import pandas as pd
warming_up_invocation(model_filepath=model_filepath)
model.load_model(model_filepath)
warmup_df = pd.DataFrame(columns=model.model.original_features)
warmup_df = pd.concat(
[warmup_df.T, pd.DataFrame([0] * len(warmup_df.T))], axis=1
).T
warmup_df.fillna(0, inplace=True)
model.predict(data=warmup_df)
return {
"statusCode": 200,
"body": json.dumps(
{
"message": "Successfully processed warm up invocation",
"message": "Successfully warmed up invocation",
}
),
}
if "testing" in body:
logger.info(
"Testing invocation for CI/CD - save file to same location in S3"
)
storage_filepath = body["file_location"].replace(
".parquet", "_output.parquet"
)
else:
storage_filepath = f"s3://{PREDICTIONS_BUCKET}/{portfolio_id}/{property_id}/{created_at}.parquet"