@@ -478,6 +478,8 @@ if (SENTRY_URL := env.str('SENTRY_URL', '')) and RELEASE and ENVIRONMENT: 'PredictionInterruptedError', 'ImageContentNotFound', 'PromptLengthExceeded' + 'InvalidParameterError', + 'UnsupportedSize' ], ) @@ -963,6 +963,10 @@ msgstr "Не найдено моделей по этому ID" msgid "No matching version found" msgstr "Соответствующая версия не найдена" +#: ml_model/services/minimaxmusic.py:57 +msgid "Lyrics is too long" +msgstr "Текст песни слишком длинный" + #: ml_model/services/minio_service.py:36 ml_model/services/minio_service.py:54 #: ml_model/services/minio_service.py:62 ml_model/services/minio_service.py:71 msgid "Unknown bucket destination" @@ -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) @@ -5,8 +5,11 @@ from io import BytesIO import requests from django.core.files import File +from django.utils.translation import gettext as _ +from replicate.exceptions import ModelError from messages.models import Message +from ml_model.exceptions import RequestBlocked, InvalidParameterError, GenerationException from ml_model.services.base import SimpleService from ml_model.tasks import replicate_run @@ -47,8 +50,15 @@ class Minimaxmusic(SimpleService): file_url = Preset.objects.get(slug=instrumental).file.url callback_data.update({'instrumental_file': file_url}) start_time = time.time() - video = replicate_run(f'minimax/music-01', callback_data) + try: + audio = replicate_run(f'minimax/music-01', callback_data) + except ModelError as exc: + if 'lyrics is too long' in str(exc): + raise InvalidParameterError(_('Lyrics is too long')) + 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) - msgs = self.save_results(input_message.content, process_time, video, save) + msgs = self.save_results(input_message.content, process_time, audio, save) return msgs @@ -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) @@ -7,8 +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 RequestBlocked, GenerationException from ml_model.services.base import SimpleService from ml_model.tasks import replicate_run @@ -53,10 +55,14 @@ class Speedance(SimpleService): input_message.file.close() callback_data.update({'image': image}) start_time = time.time() - video = replicate_run( - f'bytedance/{input_message.info.get("version", "seedance-1-pro-fast")}', - callback_data - ) + try: + video = replicate_run( + f'bytedance/{input_message.info.get("version", "seedance-1-pro-fast")}', 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, resolution=resolution, duration=duration) msgs = self.save_results(input_message.content, process_time, video, save) @@ -96,6 +96,13 @@ class PredictionInterruptedError(Exception): return _('Prediction interrupted. Please retry again') +class InvalidParameterError(Exception): + def __init__(self, error_text: str): + self.error_text = error_text + + def __str__(self): + return self.error_text + class PromptLengthExceeded(Exception): def __init__(self, max_length: int = 3000) -> None: self.max_length = max_length @@ -15,6 +15,7 @@ from ml_model.exceptions import ( UnsupportedSize, FileNotProvided, ImageContentNotFound, + InvalidParameterError, InvalidStyleCombinationError, PromptLengthExceeded, ) @@ -167,6 +168,7 @@ class MediaAPIView(APIView): FileNotProvided, ImageContentNotFound, InvalidStyleCombinationError, + InvalidParameterError, PromptLengthExceeded, ), ):