change image size model

This commit is contained in:
SlimusMinus
2026-10-15 01:27:20 +03:00
parent 9d94963ccc
commit e029a1cfdd
4 changed files with 28 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
from PIL import Image, UnidentifiedImageError
from PIL import Image, ImageOps, UnidentifiedImageError
from fastapi import APIRouter, UploadFile, File, Depends, HTTPException
from app.config.settings import settings
from app.core.dependencies import get_image_moderation_service
from app.models.response.image_moderation_response import \
ImageModerationResponse
@@ -11,8 +12,6 @@ router = APIRouter(
tags=["Moderation"]
)
MAX_SIZE = 5 * 1024 * 1024 # например, 5 МБ
ALLOWED_CONTENT_TYPES = (
"image/jpeg",
"image/png",
@@ -35,7 +34,7 @@ def moderate_image(
# 2. Проверка размера — читаем файл целиком
contents = file.file.read()
if len(contents) > MAX_SIZE:
if len(contents) > settings.MAX_IMAGE_BYTES:
raise HTTPException(
status_code=413,
detail="Image too large"
@@ -46,8 +45,10 @@ def moderate_image(
# 3. Только теперь пытаемся открыть изображение
try:
image = Image.open(file.file)
# Фото с телефона хранят поворот в EXIF — без этого модели видят его боком
image = ImageOps.exif_transpose(image)
image = image.convert("RGB")
except UnidentifiedImageError:
except (UnidentifiedImageError, Image.DecompressionBombError):
raise HTTPException(
status_code=400,
detail="Invalid image"