@@ -477,6 +477,8 @@ if (SENTRY_URL := env.str('SENTRY_URL', '')) and RELEASE and ENVIRONMENT: 'RequestBlocked', 'PredictionInterruptedError', 'ImageContentNotFound', + 'InvalidParameterError', + 'UnsupportedSize' ], ) @@ -7,6 +7,7 @@ import requests from django.core.files import File from messages.models import Message +from ml_model.exceptions import RequestBlocked, PredictionInterruptedError, GenerationException from ml_model.models import ( NeuronModel, ) @@ -69,10 +70,17 @@ class Flux(SimpleService): **input_message.info, } ) - runner = replicate_run( - f'{self._CALLBACK_BASE}{callback_data.get("version", "flux-schnell")}', - callback_data, - ) + try: + runner = replicate_run( + f'{self._CALLBACK_BASE}{callback_data.get("version", "flux-schnell")}', + callback_data, + ) + except Exception as exc: + if any(error in str(exc) for error in ('E005', 'E006', 'sexual', 'NSFW')): + raise RequestBlocked + elif 'PA' in str(exc): + raise PredictionInterruptedError + raise GenerationException from exc images = runner if isinstance(runner, list) else [runner] process_time = timedelta(seconds=(time.time() - start_time)) self.handle_invoice(input_message.content_object.model, input_message=input_message, version=version) @@ -7,9 +7,10 @@ from io import BytesIO import filetype import requests from django.core.files import File +from replicate.exceptions import ModelError from messages.models import Message -from ml_model.exceptions import FileNotProvided +from ml_model.exceptions import FileNotProvided, RequestBlocked, GenerationException from ml_model.services.base import SimpleService from ml_model.tasks import replicate_run @@ -60,7 +61,12 @@ class Runway(SimpleService): 'image': media } start_time = time.time() - video = replicate_run(f'runwayml/{version}', callback_data) + try: + video = replicate_run(f'runwayml/{version}', callback_data) + except ModelError as exc: + if any(error in str(exc) for error in ('E005', 'E006', 'sexual')): + raise RequestBlocked + raise GenerationException from exc process_time = timedelta(seconds=(time.time() - start_time)) self.handle_invoice(input_message.content_object.model, duration=duration, version=version) msgs = self.save_results(input_message.content, process_time, video, save)