In this segment, you’ll implement text moderation for your sample app. Download the Materials Repo from the GitHub to get started.
This demo uses Jupyter Labs to write and run Python code. So, if you don’t have it in your system, please install it before following up with the rest of the video by visiting Jupyter Install Page and following the installation instructions.
Once installed, open the Jupyter Lab in your browser. Then, navigate to 02-text-moderation-with-azure-content-safety/Starter and open the starter.ipynb file. You will see a nice-looking Python notebook containing some instructions and cells with TODO in it. You will implement those TODOS!
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
The above code will install the following packages:
- azure-ai-contentsafety: This package provides tools for interacting with the Azure AI Content Safety API, which is used to create moderation requests and manage content safety in your Python applications.
- python-dotenv: You’ll use this library to load your sensitive endpoint and api-key variables from the .env file.
Run the cell by pressing the Enter + Shift keys and wait for the installation to finish. This successfully installs both of the additional libraries that are required for this demo.
Next, you’ll import the packages that you’ll use in this notebook. Replace # TODO import packages with the following:
# 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
Here’s the explanation of the above code:
- You imported the os module. This module is part of Python’s standard library and provides functions to interact with the operating system. You’ll use this module to read the environment variables - content safety key and endpoint.
-
You then imported the
load_dotenvfunction from dotenv module. This function will load all the key-value pairs from the .env file to environment variables you’ll create later. Overall, this provides a clean and convenient method to avoid exposing sensitive endpoints and key values to the main code base. - Finally, you imported various classes from the Azure AI content safety module; you’ll learn more about them soon enough!
Run the cell to successfully import everything provided and wait for the cell execution to finish.
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))
Here’s the explanation for the above code:
-
You are using
load_dotenvfunction to load theCONTENT_SAFETY_KEYandCONTENT_SAFETY_ENDPOINTto environment variables from the .env file that you’ll soon create. -
Next, you’re loading the value of both
CONTENT_SAFETY_KEYandCONTENT_SAFETY_ENDPOINTfrom environment variable tokeyandendpointvariables respectively usingos.environ. -
Finally, you’re creating
ContentSafetyClientusing endpoint and key and assigning it to client variable.
Before running this cell, you’ll first need to create the .env file and store the key-value pair for CONTENT_SAFETY_KEY and CONTENT_SAFETY_ENDPOINT. Open the 02-text-moderation-with-azure-content-safety/Starter in VSCode or any other preferred code editor. Create .env file inside the root directory of project. Next, copy the following in the file:
CONTENT_SAFETY_KEY=<api-key>
CONTENT_SAFETY_ENDPOINT=<endpoint>
You simply created two key-value pairs for holding content safety key and endpoint. Now, head to your resource in Azure to fetch endpoint and API key value. You’ll find both your endpoint API and key inside Resource Management/Keys and Endpoint page. Copy and paste them to CONTENT_SAFETY_KEY and CONTENT_SAFETY_ENDPOINT respectively.
Finally, head back to Jupyter Labs and run the cell to create the safety client.
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"
Here’s what the following code does:
-
You create a function
moderate_text, which takes a text as an argument and returns"Post successful"if the content is appropriate. -
Inside it. You construct the request using
AnalyzeTextOptions. You’ve additionally providedAnalyzeTextOutputType.EIGHT_SEVERITY_LEVELSvalue for the optional parameteroutput_type. This will request to moderation API to share severity level at an scale of 0-7 instead of defaultFOUR_SEVERITY_LEVELSwhere you only get 0,2,4,6 as the severity level as output. -
Next, you are calling the
client.analyze_textfunction inside thetry-exceptblock and storing its response inresponsevariable. Also, if any error occurs during the request, you handle it by catching it using the except block and letting the user know about it. -
Finally, if the content is appropriate, you return the status as
"Post successful".
After understanding how to create text moderation requests, let’s implement the logic to process the received moderation response to determine if the post is approved or rejected and take action accordingly. Replace ## TODO: Process moderation response to determine if the post is approved or rejected with the following code:
# 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."
Make sure to fix the indentation by pressing the tab key.
Here’s the explanation of the above code:
-
You create a dictionary of
TextCategoryvalues, which will be utilized to extract the moderation results of respective category. -
You’re then iterating through
response.categories_analysislist to extract the output category responses and store them in the appropriate category dictionary key defined above. - Then, you extract the results of each category in separate variables from the category dictionary.
- This is the main part of the processing logic. Here, you’re determining if the text content that is requested to moderation is found inappropriate for different categories - hate, self-harm, violence, and sexual based on the severity threshold defined. If the output severity level exceeds each category’s threshold value, then you append the category name to the violation list.
- Finally, if any violation exists; i.e. if the violation list is not empty; you inform the user by returning details regarding the categories that are found to be violated from the text, and ask them to update the post content to adhere to the community guidelines. If no violations are found, then you notify the user that the post is successfully posted.
Now, run the cell to ensure that the function defined is error-free.
Try Text Moderation API
Finally, it’s time to test the text moderation by calling the function and seeing the response.
In the newly created cell, add the following code:
# 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)
This code passes the text_to_moderate variable to the moderate_text function and finally, print the moderation_response received from the function.
Run the cell to see it in action! You should receive the following output:
Your content contains hate speech, violent references that violate our
community guidelines. Please modify your post to adhere to community
guidelines.
Congratulations! You’ve successfully implemented the Text Moderation workflow, which can handle your large volume of moderation requests in real-time. Feel free to try a few more post examples and see how the moderation API responds.
That’s it for this segment. Continue to the next segment to conclude the lesson!