@@ -10,6 +10,7 @@ from django.core.files import File from backend import settings from messages.models import BaseStore, Message +from ml_model.exceptions import RequestBlocked, GenerationException from ml_model.services.base import SimpleService from poller.models import Proxy @@ -83,7 +84,12 @@ class Gptimage(SimpleService): else: data = client.post('images/edits', data=json_data, files=files).json() process_time = timedelta(seconds=(time.time() - start_time)) - image = data['data'][0]['b64_json'] + try: + image = data['data'][0]['b64_json'] + except KeyError as exc: + if data['error']['code'] == 'moderation_blocked': + raise RequestBlocked + raise GenerationException from exc text_tokens = data['usage']['input_tokens_details']['text_tokens'] image_tokens = data['usage']['input_tokens_details']['image_tokens'] output_tokens = data['usage']['output_tokens'] @@ -20,7 +20,7 @@ from rest_framework.views import APIView from messages.models import Message from messages.serializers import MessageSerializer from ml_model.exceptions import DeploymentDisabled, TemplateNotFound, TemplateUnknownException, \ - FileExtensionNotSupported, ExceededContextLengthError + FileExtensionNotSupported, ExceededContextLengthError, RequestBlocked from ml_model.services.base import SimpleService from payments.exceptions.insufficient_balance import InsufficientBalance from tools.chats.models import Chat @@ -159,9 +159,7 @@ class MessagesAPIView(APIView): }, status=HTTP_503_SERVICE_UNAVAILABLE, ) - except FileExtensionNotSupported as exc: - return Response({'detail': f'{exc}'}, status=HTTP_400_BAD_REQUEST) - except ExceededContextLengthError as exc: + except (FileExtensionNotSupported, ExceededContextLengthError, RequestBlocked) as exc: return Response({'detail': f'{exc}'}, status=HTTP_400_BAD_REQUEST) except TemplateNotFound as exc: return Response({'detail': f'{exc}'}, status=HTTP_500_INTERNAL_SERVER_ERROR) @@ -14,8 +14,7 @@ from messages.serializers import MessageSerializer from ml_model.models import NeuronModel from ml_model.services.base import SimpleService from payments.exceptions.insufficient_balance import InsufficientBalance -from ml_model.exceptions import UnsupportedSize - +from ml_model.exceptions import UnsupportedSize, RequestBlocked from .models import Audio, Image, Video @@ -154,7 +153,7 @@ class MediaAPIView(APIView): input_message.save() if isinstance(exc, InsufficientBalance): return Response({'detail': f'{exc}'}, status=HTTP_402_PAYMENT_REQUIRED) - if isinstance(exc, UnsupportedSize): + if isinstance(exc, (UnsupportedSize, RequestBlocked)): return Response({'detail': f'{exc}'}, status=HTTP_400_BAD_REQUEST) return Response( {