Construct a generative AI-based content material moderation resolution on Amazon SageMaker JumpStart

Content material moderation performs a pivotal function in sustaining on-line security and upholding the values and requirements of internet sites and social media platforms. Its significance is underscored by the safety it offers customers from publicity to inappropriate content material, safeguarding their well-being in digital areas. For instance, within the promoting trade, content material moderation serves to protect manufacturers from unfavorable associations, thereby contributing to model elevation and income development. Advertisers prioritize their model’s alignment with applicable content material to uphold their popularity and avert detrimental publicity. Content material moderation additionally assumes vital significance within the finance and healthcare sectors, the place it serves a number of features. It performs an necessary function in figuring out and safeguarding delicate private identifiable and well being data (PII, PHI). By adhering to inside requirements and practices and complying with exterior laws, content material moderation enhances digital safety for customers. This manner, it prevents the inadvertent sharing of confidential knowledge on public platforms, making certain the preservation of consumer privateness and knowledge safety.

On this put up, we introduce a novel methodology to carry out content material moderation on picture knowledge with multi-modal pre-training and a big language mannequin (LLM). With multi-modal pre-training, we are able to instantly question the picture content material primarily based on a set of questions of curiosity and the mannequin will have the ability to reply these questions. This permits customers to talk with the picture to substantiate if it incorporates any inappropriate content material that violates the group’s insurance policies. We use the highly effective producing functionality of LLMs to generate the ultimate choice together with secure/unsafe labels and class sort. As well as, by designing a immediate, we are able to make an LLM generate the outlined output format, corresponding to JSON format. The designed immediate template permits the LLM to find out if the picture violates the moderation coverage, determine the class of violation, clarify why, and supply the output in a structured JSON format.

We use BLIP-2 because the multi-modal pre-training methodology. BLIP-2 is likely one of the state-of-the-art fashions in multi-modal pre-training and outperforms many of the current strategies in visible query answering, picture captioning, and picture textual content retrieval. For our LLM, we use Llama 2, the following technology open-source LLM, which outperforms existing open-source language models on many benchmarks, including reasoning, coding, proficiency, and knowledge tests. The next diagram illustrates the answer elements.

Challenges in content material moderation

Conventional content material moderation strategies, corresponding to human-based moderation, can’t sustain with the rising quantity of user-generated content material (UGC). As the amount of UGC will increase, human moderators can turn into overwhelmed and wrestle to average content material successfully. This ends in a poor consumer expertise, excessive moderation prices, and model danger. Human-based moderation can also be vulnerable to errors, which can lead to inconsistent moderation and biased choices. To handle these challenges, content material moderation powered by machine studying (ML) has emerged as an answer. ML algorithms can analyze giant volumes of UGC and determine content material that violates the group’s insurance policies. ML fashions might be skilled to acknowledge patterns and determine problematic content material, corresponding to hate speech, spam, and inappropriate materials. In line with the examine Protect your users, brand, and budget with AI-powered content moderation, ML-powered content material moderation can assist organizations reclaim as much as 95% of the time their groups spend moderating content material manually. This enables organizations to focus their sources on extra strategic duties, corresponding to group constructing and content material creation. ML-powered content material moderation also can cut back moderation prices as a result of it’s extra environment friendly than human-based moderation.

Regardless of the benefits of ML-powered content material moderation, it nonetheless has additional enchancment house. The effectiveness of ML algorithms closely depends on the standard of the information they’re skilled on. When fashions are skilled utilizing biased or incomplete knowledge, they’ll make misguided moderation choices, exposing organizations to model dangers and potential authorized liabilities. The adoption of ML-based approaches for content material moderation brings a number of challenges that necessitate cautious consideration. These challenges embrace:

  • Buying labeled knowledge – This is usually a pricey course of, particularly for advanced content material moderation duties that require coaching labelers. This price could make it difficult to assemble giant sufficient datasets to coach a supervised ML mannequin with ease. Moreover, the accuracy of the mannequin closely depends on the standard of the coaching knowledge, and biased or incomplete knowledge may end up in inaccurate moderation choices, resulting in model danger and authorized liabilities.
  • Mannequin generalization – That is vital to adopting ML-based approaches. A mannequin skilled on one dataset could not generalize nicely to a different dataset, significantly if the datasets have completely different distributions. Due to this fact, it’s important to make sure that the mannequin is skilled on a various and consultant dataset to make sure it generalizes nicely to new knowledge.
  • Operational effectivity – That is one other problem when utilizing standard ML-based approaches for content material moderation. Always including new labels and retraining the mannequin when new lessons are added might be time-consuming and dear. Moreover, it’s important to make sure that the mannequin is often up to date to maintain up with modifications within the content material being moderated.
  • Explainability – Finish customers could understand the platform as biased or unjust if content material will get flagged or eliminated with out justification, leading to a poor consumer expertise. Equally, the absence of clear explanations can render the content material moderation course of inefficient, time-consuming, and dear for moderators.
  • Adversarial nature – The adversarial nature of image-based content material moderation presents a novel problem to traditional ML-based approaches. Unhealthy actors can try to evade content material moderation mechanisms by altering the content material in varied methods, corresponding to utilizing synonyms of pictures or embedding their precise content material inside a bigger physique of non-offending content material. This requires fixed monitoring and updating of the mannequin to detect and reply to such adversarial ways.

Multi-modal reasoning with BLIP-2

Multi-modality ML fashions consult with fashions that may deal with and combine knowledge from a number of sources or modalities, corresponding to pictures, textual content, audio, video, and different types of structured or unstructured knowledge. One of many common multi-modality fashions is the visual-language fashions corresponding to BLIP-2, which mixes laptop imaginative and prescient and pure language processing (NLP) to grasp and generate each visible and textual data. These fashions allow computer systems to interpret the that means of pictures and textual content in a manner that mimics human understanding. Imaginative and prescient-language fashions can deal with quite a lot of duties, together with picture captioning, picture textual content retrieval, visible query answering, and extra. For instance, a picture captioning mannequin can generate a pure language description of a picture, and a picture textual content retrieval mannequin can seek for pictures primarily based on a textual content question. Visible query answering fashions can reply to pure language questions on pictures, and multi-modal chatbots can use visible and textual inputs to generate responses. When it comes to content material moderation, you need to use this functionality to question in opposition to an inventory of questions.

BLIP-2 incorporates three elements. The primary element is a frozen picture encoder, ViT-L/14 from CLIP, which takes picture knowledge as enter. The second element is a frozen LLM, FlanT5, which outputs textual content. The third element is a trainable module known as Q-Former, a light-weight transformer that connects the frozen picture encoder with the frozen LLM. Q-Former employs learnable question vectors to extract visible options from the frozen picture encoder and feeds essentially the most helpful visible function to the LLM to output the specified textual content.

The pre-training course of entails two levels. Within the first stage, vision-language illustration studying is carried out to show Q-Former to be taught essentially the most related visible illustration for the textual content. Within the second stage, vision-to-language generative studying is carried out by connecting the output of Q-Former to a frozen LLM and coaching Q-Former to output visible representations that may be interpreted by the LLM.

BLIP-2 achieves state-of-the-art efficiency on varied vision-language duties regardless of having considerably fewer trainable parameters than current strategies. The mannequin additionally demonstrates rising capabilities of zero-shot image-to-text technology that may observe pure language directions. The next illustration is modified from the original research paper.

Resolution overview

The next diagram illustrates the answer structure.

Within the following sections, we exhibit methods to deploy BLIP-2 to an Amazon SageMaker endpoint, and use BLIP-2 and an LLM for content material moderation.


You want an AWS account with an AWS Identity and Access Management (IAM) function with permissions to handle sources created as a part of the answer. For particulars, consult with Create a standalone AWS account.

If that is your first time working with Amazon SageMaker Studio, you first have to create a SageMaker domain. Moreover, chances are you’ll have to request a service quota enhance for the corresponding SageMaker internet hosting situations. For the BLIP-2 mannequin, we use an ml.g5.2xlarge SageMaker internet hosting occasion. For the Llama 2 13B mannequin, we use an ml.g5.12xlarge SageMaker internet hosting occasion.

Deploy BLIP-2 to a SageMaker endpoint

You’ll be able to host an LLM on SageMaker utilizing the Large Model Inference (LMI) container that’s optimized for internet hosting giant fashions utilizing DJLServing. DJLServing is a high-performance common mannequin serving resolution powered by the Deep Java Library (DJL) that’s programming language agnostic. To be taught extra about DJL and DJLServing, consult with Deploy large models on Amazon SageMaker using DJLServing and DeepSpeed model parallel inference. With the assistance of the SageMaker LMI container, the BLIP-2 mannequin might be simply applied with the Hugging Face library and hosted on SageMaker. You’ll be able to run blip2-sagemaker.ipynb for this step.

To arrange the Docker picture and mannequin file, it’s essential to retrieve the Docker picture of DJLServing, package deal the inference script and configuration recordsdata as a mannequin.tar.gz file, and add it to an Amazon Simple Storage Service (Amazon S3) bucket. You’ll be able to consult with the inference script and configuration file for extra particulars.

inference_image_uri = image_uris.retrieve(
    framework="djl-deepspeed", area=sess.boto_session.region_name, model="0.22.1"
! tar czvf mannequin.tar.gz blip2/
s3_code_artifact = sess.upload_data("mannequin.tar.gz", bucket, s3_code_prefix)

When the Docker picture and inference associated recordsdata are prepared, you create the mannequin, the configuration for the endpoint, and the endpoint:

from sagemaker.utils import name_from_base
blip_model_version = "blip2-flan-t5-xl"
model_name = name_from_base(blip_model_version)
mannequin = Mannequin(

When the endpoint standing turns into in service, you may invoke the endpoint for picture captioning and the instructed zero-shot vision-to-language technology process. For the picture captioning process, you solely have to go a picture to the endpoint:

import base64
import json
from PIL import Picture

smr_client = boto3.consumer("sagemaker-runtime")

def encode_image(img_file):
    with open(img_file, "rb") as image_file:
        img_str = base64.b64encode(image_file.learn())
        base64_string = img_str.decode("latin1")
    return base64_string

def run_inference(endpoint_name, inputs):
    response = smr_client.invoke_endpoint(
        EndpointName=endpoint_name, Physique=json.dumps(inputs)

test_image = "carcrash-ai.jpeg"
base64_string = encode_image(test_image)
inputs = {"picture": base64_string}
run_inference(endpoint_name, inputs)

For the instructed zero-shot vision-to-language technology process, along with the enter picture, it’s essential to outline the query as a immediate:

base64_string = encode_image(test_image)
inputs = {"immediate": "Query: what occurred on this picture? Reply:", "picture": base64_string}
run_inference(endpoint_name, inputs)

Use BLIP-2 and LLM for content material moderation

On this stage, you may make queries on the given picture and retrieve hidden data. With the LLM, you arrange the queries and retrieve data to generate the JSON format consequence. You’ll be able to roughly break up this process into the next two sub-tasks:

  1. Extract data from the picture with the BLIP-2 mannequin.
  2. Generate the ultimate consequence and rationalization with the LLM.

Extract data from the picture with the BLIP-2 mannequin

To retrieve sufficient helpful hidden data from the given picture, it’s essential to outline queries. As a result of every question will invoke the endpoint as soon as, many queries will result in longer processing time. Due to this fact, we advise making queries prime quality and canopy all insurance policies but additionally with out duplicated. In our pattern code, we outline the queries as follows:

check_list = [
"Does this photo contain complete naked person?",
"Does this photo contain topless person?",
"Does this photo contain weapon?",
"Does this photo contain contact information?",
"Does this photo contain a smoker?",
"Does this photo contain blood?",
"Are there persons fighting in this photo?",
"Does this photo contain harassment words?"

With the previous queries, invoke the endpoint of BLIP-2 to retrieve the knowledge with the next code:

test_image = "./surf_swimwear.png"
raw_image ='RGB')

base64_string = encode_image(test_image)
conversations = """"""
for query in check_list:
    inputs = {"immediate": f"Query: {query}? Reply:", "picture": base64_string}
    response = run_inference(endpoint_name, inputs)
    conversations += f"""
Query: {query}
Reply: {response}.

Along with the knowledge retrieved by queries, you may get data with the picture captioning process by invoking the endpoint with out the immediate discipline within the payload:

inputs = {"picture": base64_string}
response = smr_client.invoke_endpoint(
EndpointName=endpoint_name, Physique=json.dumps(inputs)
image_caption = response["Body"].learn().decode('utf-8')

You’ll be able to mix the contents of queries and solutions with the picture caption and use this retrieved data for the downstream process, described within the subsequent part beneath.

Generate the ultimate consequence and rationalization with the LLM

Giant language fashions (LLMs) corresponding to Llama 2 can generate high-quality outcomes with the proper immediate template. Utilizing Amazon SageMaker JumpStart, ML practitioners can select from a broad number of publicly obtainable basis fashions. With just some clicks in SageMaker Studio, now you can discover and deploy Llama 2.

The ultimate outcomes depend on a LLM with a particular immediate template. Such immediate consists of: the moderation coverage primarily based on classes of inappropriate or offensive moderation categories; picture data extracted from BLIP-2; the query template to the LLM asking if the picture incorporates unsafe content material and requesting its class and cause if unsafe; and directions to output the ends in JSON format. The designed immediate template permits the LLM to find out if the picture violates the moderation coverage, determine the class of violation, clarify why, and supply the output in a structured JSON format.

The core supply code is as follows:

prompt_template = f"""
The next is our firm's content material moderation coverage, primarily based on the moderation coverage, we collect picture data from the consumer uploaded picture. Please reply the query with json format. 
###### moderation coverage ######
###### Picture data ######
###### Query ######
Primarily based on the next Moderation coverage and QA, inform me if the picture containes unsafe content material, additionally give its class and cause if it is unsafe. Please anwser the query with the next format and solely put rationalization into the explanation discipline:  

prompt_template += """
    "flag": "xxx",
    "class": "xxx",
    "cause": "the reason being ..."

dialog = [
    {"role": "user", "content": prompt_template}

You’ll be able to customise the immediate primarily based by yourself use case. Seek advice from the notebook for extra particulars. When the immediate is prepared, you may invoke the LLM endpoint to generate outcomes:

endpoint_name = "jumpstart-dft-meta-textgeneration-llama-2-70b-f"

def query_endpoint(payload):
    consumer = boto3.consumer("sagemaker-runtime")
    response = consumer.invoke_endpoint(
    response = response["Body"].learn().decode("utf8")
    response = json.hundreds(response)
    return response
payload = {
    "inputs": [dialog], 
    "parameters": {"max_new_tokens": 256, "top_p": 0.9, "temperature": 0.5}
consequence = query_endpoint(payload)[0]

A part of the generated output is as follows:

> Assistant:  {
    "flag": "unsafe",
    "class": "Suggestive",
    "cause": "The picture incorporates a topless particular person, which is taken into account suggestive content material."

The picture incorporates a topless particular person, which violates the moderation coverage's rule quantity 2, which states that suggestive content material consists of "Feminine Swimwear Or Underwear, Male Swimwear Or Underwear, Partial Nudity, Barechested Male, Revealing Garments and Sexual Conditions." Due to this fact, the picture is taken into account unsafe and falls underneath the class of Suggestive.

Often, Llama 2 attaches extra rationalization moreover the reply from the assistant. You might use the parsing code to extract JSON knowledge from the unique generated outcomes:

reply = consequence['generation']['content'].break up('}')[0]+'}'

Benefits of generative approaches

The previous sections confirmed methods to implement the core a part of mannequin inference. On this part, we cowl varied facets of generative approaches, together with comparisons with standard approaches and views.

The next desk compares every method.

. Generative Method Classification Method
Buying labeled knowledge Pre-trained mannequin on a lot of pictures, zero-shot inference Requires knowledge from all sorts of classes
Mannequin generalization Pre-trained mannequin with varied sorts of pictures Requires a big quantity of content material moderation associated knowledge to enhance mannequin generalization
Operational effectivity Zero-shot capabilities Requires coaching the mannequin for recognizing completely different patterns, and retraining when labels are added
Explainability Reasoning because the textual content output, nice consumer expertise Laborious to realize reasoning, onerous to clarify and interpret
Adversarial nature Strong Excessive frequency retraining

Potential use instances of multi-modal reasoning past content material moderation

The BLIP-2 fashions might be utilized to suit a number of functions with or with out fine-tuning, which incorporates the next:

  • Picture captioning – This asks the mannequin to generate a textual content description for the picture’s visible content material. As illustrated within the following instance picture (left), we are able to have “a person is standing on the seashore with a surfboard” because the picture description.
  • Visible query answering –  As the instance picture within the center reveals, we are able to ask “Is it business associated content material” and now we have “sure” as the reply. As well as, BLIP-2 helps the multi-round dialog and outputs the next query: “Why do you assume so?” Primarily based on the visible cue and LLM capabilities, BLIP-2 outputs “it’s an indication for amazon.”
  • Picture textual content retrieval – Given the query as “Textual content on the picture”, we are able to extract the picture textual content “it’s monday however preserve smiling” as demonstrated within the picture on the proper.

The next pictures present examples to exhibit the zero-shot image-to-text functionality of visible data reasoning.

As we are able to see from varied examples above, multi-modality fashions open up new alternatives for fixing advanced issues that conventional single-modality fashions would wrestle to handle.

Clear up

To keep away from incurring future expenses, delete the sources created as a part of this put up. You are able to do this by following the directions within the pocket book cleanup part, or delete the created endpoints by way of the SageMaker console and sources saved within the S3 bucket.


On this put up, we mentioned the significance of content material moderation within the digital world and highlighted its challenges. We proposed a brand new methodology to assist enhance content material moderation with picture knowledge and carry out query answering in opposition to the photographs to robotically extract helpful data. We additionally offered additional dialogue on the benefits of utilizing a generative AI-based method in comparison with the normal classification-based method. Lastly, we illustrated the potential use instances of visual-language fashions past content material moderation.

We encourage you to be taught extra by exploring SageMaker and constructing an answer utilizing the multi-modality resolution offered on this put up and a dataset related to your corporation.

Concerning the Authors

Gordon Wang is a Senior AI/ML Specialist TAM at AWS. He helps strategic prospects with AI/ML greatest practices cross many industries. He’s captivated with laptop imaginative and prescient, NLP, generative AI, and MLOps. In his spare time, he loves operating and mountaineering.

Yanwei Cui, PhD, is a Senior Machine Studying Specialist Options Architect at AWS. He began machine studying analysis at IRISA (Analysis Institute of Pc Science and Random Techniques), and has a number of years of expertise constructing AI-powered industrial purposes in laptop imaginative and prescient, pure language processing, and on-line consumer conduct prediction. At AWS, he shares his area experience and helps prospects unlock enterprise potentials and drive actionable outcomes with machine studying at scale. Outdoors of labor, he enjoys studying and touring.

Melanie Li, PhD, is a Senior AI/ML Specialist TAM at AWS primarily based in Sydney, Australia. She helps enterprise prospects construct options utilizing state-of-the-art AI/ML instruments on AWS and offers steering on architecting and implementing ML options with greatest practices. In her spare time, she likes to discover nature and spend time with household and mates.

Leave a Reply

Your email address will not be published. Required fields are marked *