@@ -1144,3 +1144,6 @@ msgstr "При отправке письма произошла неизвест msgid "You cannot change the password of an unconfirmed e-mail user." msgstr "Вы не можете изменить пароль неподтвержденного по e-mail пользователя." + +msgid "Format is not supported. Supported formats: %(formats)s" +msgstr "Формат не поддерживается. Поддерживаемые форматы: %(formats)s" @@ -35,7 +35,7 @@ from PyPDF2 import PdfReader from backend import settings from messages.models import BaseStore, Message from ml_model.constants import TEMPORARY_TEST_TEXT -from ml_model.exceptions import GenerationException +from ml_model.exceptions import GenerationException, FormatNotSupported from ml_model.models import ( ModelConfiguration, NeuronModel, @@ -122,6 +122,9 @@ class Chatgpt(SimpleService): kind = filetype.guess(input_message.file.read(20)) mime = kind.mime if kind else 'application/octet-stream' normalized_image = Image.open(image) + supported_formats = ['png', 'jpg', 'jpeg'] + if kind.extension not in supported_formats: + raise FormatNotSupported(', '.join(supported_formats)) format = 'jpeg' if kind.extension == 'jpg' else kind.extension buf = BytesIO() normalized_image.save(buf, format=format) @@ -20,3 +20,10 @@ class DeploymentDisabled(Exception): class ModelTimeoutError(Exception): def __str__(self): return _('The model is not responding') + + +class FormatNotSupported(Exception): + def __init__(self, formats: str) -> None: + self.formats = formats + def __str__(self) -> str: + return _('Format is not supported. Supported formats: %(formats)s') % {'formats': self.formats} \ No newline at end of file @@ -10,15 +10,16 @@ from rest_framework.generics import ( from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.status import ( + HTTP_400_BAD_REQUEST, HTTP_402_PAYMENT_REQUIRED, HTTP_500_INTERNAL_SERVER_ERROR, - HTTP_503_SERVICE_UNAVAILABLE + HTTP_503_SERVICE_UNAVAILABLE, ) from rest_framework.views import APIView from messages.models import Message from messages.serializers import MessageSerializer -from ml_model.exceptions import DeploymentDisabled +from ml_model.exceptions import DeploymentDisabled, FormatNotSupported from ml_model.services.base import SimpleService from payments.exceptions.insufficient_balance import InsufficientBalance from tools.chats.models import Chat @@ -150,6 +151,13 @@ class MessagesAPIView(APIView): ) try: output_messages = service(chat).make(input_message) + except FormatNotSupported as exc: + return Response( + { + 'detail': f'{exc}' + }, + status=HTTP_400_BAD_REQUEST, + ) except DeploymentDisabled as exc: return Response( { @@ -17,6 +17,8 @@ router = Router(auth=SyncAuthBearer(), tags=['media']) def get_links(request): return ( NeuronModel.objects.filter(category__slug='images') - .filter(model_settings__isnull=False, model_settings__is_active=True) + .filter( + model_settings__isnull=False, model_settings__is_active=True, private_models_hosts__isnull=True + ) .order_by('order') )