• AIPressRoom
  • Posts
  • Construct a generative AI-based content material moderation answer on Amazon SageMaker JumpStart

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

Content material moderation performs a pivotal position 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 gives 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 defend manufacturers from unfavorable associations, thereby contributing to model elevation and income progress. Advertisers prioritize their model’s alignment with acceptable content material to uphold their status and avert destructive 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 vital position in figuring out and safeguarding delicate private identifiable and well being info (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 information on public platforms, making certain the preservation of person privateness and information safety.

On this publish, we introduce a novel methodology to carry out content material moderation on picture information with multi-modal pre-training and a big language mannequin (LLM). With multi-modal pre-training, we will instantly question the picture content material based mostly on a set of questions of curiosity and the mannequin will have the ability to reply these questions. This allows customers to speak 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 protected/unsafe labels and class sort. As well as, by designing a immediate, we will make an LLM generate the outlined output format, resembling JSON format. The designed immediate template permits the LLM to find out if the picture violates the moderation coverage, establish 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 among the state-of-the-art fashions in multi-modal pre-training and outperforms a lot of the present strategies in visible query answering, picture captioning, and picture textual content retrieval. For our LLM, we use Llama 2, the subsequent era 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 parts.

Challenges in content material moderation

Conventional content material moderation strategies, resembling 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 reasonable content material successfully. This ends in a poor person expertise, excessive moderation prices, and model danger. Human-based moderation can be liable 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 massive volumes of UGC and establish content material that violates the group’s insurance policies. ML fashions might be skilled to acknowledge patterns and establish problematic content material, resembling hate speech, spam, and inappropriate materials. In response to the examine Protect your users, brand, and budget with AI-powered content moderation, ML-powered content material moderation may help organizations reclaim as much as 95% of the time their groups spend moderating content material manually. This permits organizations to focus their assets on extra strategic duties, resembling group constructing and content material creation. ML-powered content material moderation may also scale 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 information, they will make inaccurate 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 information – This could be a pricey course of, particularly for complicated content material moderation duties that require coaching labelers. This value could make it difficult to collect massive sufficient datasets to coach a supervised ML mannequin with ease. Moreover, the accuracy of the mannequin closely depends on the standard of the coaching information, and biased or incomplete information 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 properly to a different dataset, notably if the datasets have completely different distributions. Subsequently, it’s important to make sure that the mannequin is skilled on a various and consultant dataset to make sure it generalizes properly to new information.

  • Operational effectivity – That is one other problem when utilizing typical 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 expensive. Moreover, it’s important to make sure that the mannequin is commonly 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 person expertise. Equally, the absence of clear explanations can render the content material moderation course of inefficient, time-consuming, and expensive for moderators.

  • Adversarial nature – The adversarial nature of image-based content material moderation presents a novel problem to standard ML-based approaches. Dangerous actors can try and evade content material moderation mechanisms by altering the content material in numerous methods, resembling 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 seek advice from fashions that may deal with and combine information from a number of sources or modalities, resembling pictures, textual content, audio, video, and different types of structured or unstructured information. One of many standard multi-modality fashions is the visual-language fashions resembling BLIP-2, which mixes laptop imaginative and prescient and pure language processing (NLP) to know and generate each visible and textual info. These fashions allow computer systems to interpret the which means of pictures and textual content in a means that mimics human understanding. Imaginative and prescient-language fashions can sort out 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 based mostly 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 should use this functionality to question towards a listing of questions.

BLIP-2 incorporates three components. The primary part is a frozen picture encoder, ViT-L/14 from CLIP, which takes picture information as enter. The second part is a frozen LLM, FlanT5, which outputs textual content. The third part 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 phases. 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 numerous vision-language duties regardless of having considerably fewer trainable parameters than present strategies. The mannequin additionally demonstrates rising capabilities of zero-shot image-to-text era that may comply with 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 display how you can deploy BLIP-2 to an Amazon SageMaker endpoint, and use BLIP-2 and an LLM for content material moderation.

Conditions

You want an AWS account with an AWS Identity and Access Management (IAM) position with permissions to handle assets created as a part of the answer. For particulars, seek advice from 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, you might have to request a service quota improve 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 may host an LLM on SageMaker utilizing the Large Model Inference (LMI) container that’s optimized for internet hosting massive fashions utilizing DJLServing. DJLServing is a high-performance common mannequin serving answer powered by the Deep Java Library (DJL) that’s programming language agnostic. To be taught extra about DJL and DJLServing, seek advice from 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 may run blip2-sagemaker.ipynb for this step.

To arrange the Docker picture and mannequin file, it’s worthwhile to retrieve the Docker picture of DJLServing, package deal the inference script and configuration information as a mannequin.tar.gz file, and add it to an Amazon Simple Storage Service (Amazon S3) bucket. You may seek advice from 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 information 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(
    image_uri=inference_image_uri,
    model_data=s3_code_artifact,
    position=position,
    title=model_name,
)
mannequin.deploy(
    initial_instance_count=1,
    instance_type="ml.g5.2xlarge",
    endpoint_name=model_name
)

When the endpoint standing turns into in service, you may invoke the endpoint for picture captioning and the instructed zero-shot vision-to-language era activity. For the picture captioning activity, you solely have to cross 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)
    )
    print(response["Body"].learn())

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 era activity, along with the enter picture, it’s worthwhile to outline the query as a immediate:

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

Use BLIP-2 and LLM for content material moderation

On this stage, you can also make queries on the given picture and retrieve hidden info. With the LLM, you manage the queries and retrieve info to generate the JSON format end result. You may roughly cut up this activity into the next two sub-tasks:

  1. Extract info from the picture with the BLIP-2 mannequin.

  2. Generate the ultimate end result and rationalization with the LLM.

Extract info from the picture with the BLIP-2 mannequin

To retrieve sufficient helpful hidden info from the given picture, it’s worthwhile to outline queries. As a result of every question will invoke the endpoint as soon as, many queries will result in longer processing time. Subsequently, we recommend making queries top 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 data with the next code:

test_image = "./surf_swimwear.png"
raw_image = Picture.open(test_image).convert('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 data retrieved by queries, you may get info with the picture captioning activity by invoking the endpoint with out the immediate area 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 may mix the contents of queries and solutions with the picture caption and use this retrieved info for the downstream activity, described within the subsequent part under.

Generate the ultimate end result and rationalization with the LLM

Giant language fashions (LLMs) resembling Llama 2 can generate high-quality outcomes with the proper immediate template. Utilizing Amazon SageMaker JumpStart, ML practitioners can select from a broad collection of publicly obtainable basis fashions. With only a few 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 based mostly on classes of inappropriate or offensive moderation categories; picture info extracted from BLIP-2; the query template to the LLM asking if the photograph 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, establish 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, based mostly on the moderation coverage, we collect picture info from the person uploaded picture. Please reply the query with json format. 
        
###### moderation coverage ######
{moderation_policy}
        
###### Picture info ######
{conversations}
        
###### Query ######
Primarily based on the next Moderation coverage and QA, inform me if the photograph 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 area:  
"""

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

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

You may customise the immediate based mostly by yourself use case. Discuss with 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(
        EndpointName=endpoint_name,
        ContentType="utility/json",
        Physique=json.dumps(payload),
        CustomAttributes="accept_eula=true",
    )
    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}
}
end result = query_endpoint(payload)[0]

A part of the generated output is as follows:

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

Rationalization:
The photograph 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." Subsequently, the photograph is taken into account unsafe and falls below the class of Suggestive.

Often, Llama 2 attaches extra rationalization in addition to the reply from the assistant. You would use the parsing code to extract JSON information from the unique generated outcomes:

reply = end result['generation']['content'].cut up('}')[0]+'}'
json.hundreds(reply)

Benefits of generative approaches

The previous sections confirmed how you can implement the core a part of mannequin inference. On this part, we cowl numerous points of generative approaches, together with comparisons with typical approaches and views.

The next desk compares every strategy.

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 will have “a person is standing on the seaside with a surfboard” because the picture description.

  • Visible query answering –  As the instance picture within the center exhibits, we will ask “Is it industrial associated content material” and we’ve got “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 will extract the picture textual content “it’s monday however hold smiling” as demonstrated within the picture on the proper.

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

As we will see from numerous examples above, multi-modality fashions open up new alternatives for fixing complicated issues that conventional single-modality fashions would wrestle to handle.

Clear up

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

Conclusion

On this publish, 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 information and carry out query answering towards the pictures to routinely extract helpful info. We additionally supplied additional dialogue on the benefits of utilizing a generative AI-based strategy in comparison with the normal classification-based strategy. 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 answer supplied on this publish and a dataset related to your small business.

Concerning the Authors

Gordon Wang is a Senior AI/ML Specialist TAM at AWS. He helps strategic prospects with AI/ML finest practices cross many industries. He’s obsessed with laptop imaginative and prescient, NLP, generative AI, and MLOps. In his spare time, he loves working 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 Laptop Science and Random Techniques), and has a number of years of expertise constructing AI-powered industrial functions in laptop imaginative and prescient, pure language processing, and on-line person conduct prediction. At AWS, he shares his area experience and helps prospects unlock enterprise potentials and drive actionable outcomes with machine studying at scale. Exterior of labor, he enjoys studying and touring.

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