Skip to content

Commit 917f075

Browse files
author
David Eigen
committed
create error model whem model fails to load
1 parent 01398b7 commit 917f075

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clarifai/runners/models/model_builder.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ def create_model_instance(self, load_model=True):
104104
model.load_model()
105105
return model
106106

107+
def create_error_model_instance(self, exception: Exception):
108+
"""
109+
Create an instance of the model class that just raises the given exception.
110+
"""
111+
return ErrorModel(exception)
112+
107113
def _validate_folder(self, folder):
108114
if folder == ".":
109115
folder = "" # will getcwd() next which ends with /
@@ -641,6 +647,24 @@ def monitor_model_build(self):
641647
return False
642648

643649

650+
class ErrorModel(ModelClass):
651+
652+
def __init__(self, exception):
653+
self.exception = exception
654+
655+
def load_model(self):
656+
pass
657+
658+
def predict(self, *args, **kwargs):
659+
raise self.exception from self.exception
660+
661+
def generate(self, *args, **kwargs):
662+
raise self.exception from self.exception
663+
664+
def stream(self, *args, **kwargs):
665+
raise self.exception from self.exception
666+
667+
644668
def upload_model(folder, download_checkpoints, skip_dockerfile):
645669
builder = ModelBuilder(folder)
646670
if download_checkpoints:

clarifai/runners/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ def main():
7070

7171
builder = ModelBuilder(parsed_args.model_path)
7272

73-
model = builder.create_model_instance()
73+
try:
74+
model = builder.create_model_instance()
75+
except Exception as e:
76+
logger.exception("Error creating model instance")
77+
model = builder.create_error_model_instance(e)
7478

7579
# Setup the grpc server for local development.
7680
if parsed_args.grpc:

0 commit comments

Comments
 (0)