Skip to content

Commit

Permalink
filter invalid content FF
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Jun 26, 2024
1 parent 2c575c2 commit a93767a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
46 changes: 25 additions & 21 deletions app/usecases/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from PIL import ImageFile

import settings
from app.adapters import rekognition
from app.adapters import s3
from app.errors import Error
Expand Down Expand Up @@ -109,28 +110,31 @@ async def upload_image(
)
return Error("Invalid Image", ErrorCode.INVALID_CONTENT)

if (
# TODO: we do not yet support video types from a technical lens
# we'll likely want to add this (at least) for gif profile backgrounds
image_mime_type not in VIDEO_MIME_TYPES
# TODO: we do not yet support screenshots due to high qps/cost
and image_type is not ImageType.SCREENSHOT
):
moderation_labels = await rekognition.detect_moderation_labels(image_content)
if moderation_labels is None:
return Error("Service Unavailable", ErrorCode.SERVICE_UNAVAILABLE)

if should_disallow_upload(moderation_labels):
# TODO: store/audit log these occurrences persistently
logging.warning(
"Rejected image due to moderation labels",
extra={
"image_type": image_type,
"file_name": no_ext_file_name,
"moderation_labels": moderation_labels,
},
if settings.SHOULD_FILTER_INAPPROPRIATE_CONTENT:
if (
# TODO: we do not yet support video types from a technical lens
# we'll likely want to add this (at least) for gif profile backgrounds
image_mime_type not in VIDEO_MIME_TYPES
# TODO: we do not yet support screenshots due to high qps/cost
and image_type is not ImageType.SCREENSHOT
):
moderation_labels = await rekognition.detect_moderation_labels(
image_content,
)
return Error("Inappropriate Content", ErrorCode.INAPPROPRIATE_CONTENT)
if moderation_labels is None:
return Error("Service Unavailable", ErrorCode.SERVICE_UNAVAILABLE)

if should_disallow_upload(moderation_labels):
# TODO: store/audit log these occurrences persistently
logging.warning(
"Rejected image due to moderation labels",
extra={
"image_type": image_type,
"file_name": no_ext_file_name,
"moderation_labels": moderation_labels,
},
)
return Error("Inappropriate Content", ErrorCode.INAPPROPRIATE_CONTENT)

await s3.upload(
body=image_content,
Expand Down
4 changes: 4 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ def read_bool(value: str) -> bool:
AWS_REKOGNITION_ENDPOINT_URL = os.environ["AWS_REKOGNITION_ENDPOINT_URL"]

DEFAULT_AVATAR_FILENAME = os.environ["DEFAULT_AVATAR_FILENAME"]

SHOULD_FILTER_INAPPROPRIATE_CONTENT = read_bool(
os.environ["SHOULD_FILTER_INAPPROPRIATE_CONTENT"],
)

0 comments on commit a93767a

Please sign in to comment.