making model api async so we can run from within fastapi endpoint and locally

This commit is contained in:
Khalim Conn-Kowlessar 2024-10-22 11:17:57 +01:00
parent 5e5609a7ca
commit ff44212f98

View file

@ -289,8 +289,7 @@ class ModelApi:
logger.info("Lambda functions are warmed up and ready to go!")
def async_paginated_predictions(self, data, bucket, batch_size, model_prefixes=None, extract_ids=True):
async def async_paginated_predictions(self, data, bucket, batch_size, model_prefixes=None, extract_ids=True):
all_predictions = self.predictions_template()
to_loop_over = range(0, data.shape[0], batch_size)
@ -306,7 +305,13 @@ class ModelApi:
for key, scored in predictions_dict.items():
all_predictions[key] = pd.concat([all_predictions[key], scored])
# Run the async function
asyncio.run(run_batches())
# Check if there is an existing event loop
try:
# If there is an existing event loop, await the coroutine directly
loop = asyncio.get_running_loop()
await run_batches()
except RuntimeError: # No running event loop
# If no event loop is running, use asyncio.run()
asyncio.run(run_batches())
return all_predictions