Azure Content Safety

Nov 15 2024 · Python 3.12, Microsoft Azure, JupyterLabs

Lesson 02: Text Moderation With Azure Content Safety

Implementing Text Moderation Using Azure Content Safety API

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this segment, you’ll implement text moderation for your sample app. Download the Materials Repo from the GitHub to get started.

Import Required Packages

Start by first importing the required packages. Replace # TODO: install the packages with the following:

# install the packages
%pip install azure-ai-contentsafety
%pip install python-dotenv
# 1
import os

# 2
from dotenv import load_dotenv

# 3
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.ai.contentsafety.models import AnalyzeTextOptions,
  TextCategory, AnalyzeTextOutputType

Create Content Safety Client

Next, you’ll create a content safety client which will be used to pass API requests to Azure Content Safety resources. Replace the # TODO: Create content safety client with the following code:

# 1
# Load your Azure Safety API key and endpoint
load_dotenv()

# 2
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]

# 3
# Create a Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
CONTENT_SAFETY_KEY=<api-key>
CONTENT_SAFETY_ENDPOINT=<endpoint>

Create Moderate Text Function

Next, you’ll create the moderate_text function that will be used to request text moderation with moderation API and simulate if the requested post is accepted or rejected. Replace # TODO: Implement moderate text function with the following code:

# 1
def moderate_text(text):
    # 2
    # Construct a request
    request = AnalyzeTextOptions(text=text, output_type=AnalyzeTextOutputType.
      EIGHT_SEVERITY_LEVELS)

    # 3
    # Analyze text
    try:
        response = client.analyze_text(request)
    except HttpResponseError as e:
        print("Analyze text failed.")
        if e.error:
            print(f"Error code: {e.error.code}")
            print(f"Error message: {e.error.message}")
            raise
        print(e)
        raise

    ## TODO: Process moderation response to determine if the post is
    #        approved or rejected

    # 4
    # If content is appropriate
    return "Post successful"
# 1
# Extract results
categories = {
    TextCategory.HATE: None,
    TextCategory.SELF_HARM: None,
    TextCategory.SEXUAL: None,
    TextCategory.VIOLENCE: None
}

# 2
for item in response.categories_analysis:
    if item.category in categories:
        categories[item.category] = item

# 3
hate_result = categories[TextCategory.HATE]
self_harm_result = categories[TextCategory.SELF_HARM]
sexual_result = categories[TextCategory.SEXUAL]
violence_result = categories[TextCategory.VIOLENCE]

# 4
# Check for inappropriate content
violations = []
if hate_result and hate_result.severity > 2:
    violations.append("hate speech")
if self_harm_result and self_harm_result.severity > 3:
    violations.append("self-harm references")
if sexual_result and sexual_result.severity > 0:
    violations.append("sexual references")
if violence_result and violence_result.severity > 2:
    violations.append("violent references")

# 5
if violations:
    return f"Your content contains {', '.join(violations)} that violate
      our community guidelines. Please modify your post to adhere to
      community guidelines."

Try Text Moderation API

Finally, it’s time to test the text moderation by calling the function and seeing the response.

# try harmful content
text_to_moderate = "Millions of foolish peeps were wiped out because they
   were the inferior race. Murdering them continues to be right and
   honorable because…"
moderation_response = moderate_text(text_to_moderate)
print(moderation_response)
Your content contains hate speech, violent references that violate our
  community guidelines. Please modify your post to adhere to community
  guidelines.
See forum comments
Cinema mode Download course materials from Github
Previous: Understanding Azure Content Safety Text Moderation API Next: Conclusion