Skip to content

Commit

Permalink
create error model whem model fails to load
Browse files Browse the repository at this point in the history
  • Loading branch information
deigen committed Jan 31, 2025
1 parent cd45884 commit 7493195
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 24 additions & 0 deletions clarifai/runners/models/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def create_model_instance(self, load_model=True):
model.load_model()
return model

def create_error_model_instance(self, exception: Exception):
"""
Create an instance of the model class that just raises the given exception.
"""
return ErrorModel(exception)

def _validate_folder(self, folder):
if folder == ".":
folder = "" # will getcwd() next which ends with /
Expand Down Expand Up @@ -641,6 +647,24 @@ def monitor_model_build(self):
return False


class ErrorModel(ModelClass):

def __init__(self, exception):
self.exception = exception

def load_model(self):
pass

def predict(self, *args, **kwargs):
raise self.exception from self.exception

def generate(self, *args, **kwargs):
raise self.exception from self.exception

def stream(self, *args, **kwargs):
raise self.exception from self.exception


def upload_model(folder, download_checkpoints, skip_dockerfile):
builder = ModelBuilder(folder)
if download_checkpoints:
Expand Down
6 changes: 5 additions & 1 deletion clarifai/runners/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def main():

builder = ModelBuilder(parsed_args.model_path)

model = builder.create_model_instance()
try:
model = builder.create_model_instance()
except Exception as e:
logger.exception("Error creating model instance")
model = builder.create_error_model_instance(e)

# Setup the grpc server for local development.
if parsed_args.grpc:
Expand Down

0 comments on commit 7493195

Please sign in to comment.