Microsoft Azure AI Content Safety Client Library for Python
Project description
Azure AI Content Safety client library for Python
Azure AI Content Safety detects harmful user-generated and AI-generated content in applications and services. Content Safety includes text and image APIs that allow you to detect material that is harmful.
Getting started
Prequisites
- Python 3.7 or later is required to use this package.
- You need an Azure subscription to use this package.
- An existing Azure AI Content Safety instance.
Installating the package
pip install azure-ai-contentsafety
Authenticate the client
Get the endpoint
You can find the endpoint for your Azure AI Content Safety service resource using the Azure Portal or Azure CLI:
# Get the endpoint for the Azure AI Content Safety service resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "properties.endpoint"
Get the API key
The API key can be found in the Azure Portal or by running the following Azure CLI command:
az cognitiveservices account keys list --name "<resource-name>" --resource-group "<resource-group-name>"
Create a ContentSafetyClient with AzureKeyCredential
To use an API key as the credential
parameter, pass the key as a string into an instance of AzureKeyCredential
.
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety import ContentSafetyClient
endpoint = "https://<my-custom-subdomain>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
client = ContentSafetyClient(endpoint, credential)
Key concepts
Available features
There are different types of analysis available from this service. The following table describes the currently available APIs.
Feature | Description |
---|---|
Text Analysis API | Scans text for sexual content, violence, hate, and self harm with multi-severity levels. |
Image Analysis API | Scans images for sexual content, violence, hate, and self harm with multi-severity levels. |
Text Blocklist Management APIs | The default AI classifiers are sufficient for most content safety needs. However, you might need to screen for terms that are specific to your use case. You can create blocklists of terms to use with the Text API. |
Harm categories
Content Safety recognizes four distinct categories of objectionable content.
Category | Description |
---|---|
Hate | Hate refers to any content that attacks or uses pejorative or discriminatory language in reference to a person or identity group based on certain differentiating attributes of that group. This includes but is not limited to race, ethnicity, nationality, gender identity and expression, sexual orientation, religion, immigration status, ability status, personal appearance, and body size. |
Sexual | Sexual describes content related to anatomical organs and genitals, romantic relationships, acts portrayed in erotic or affectionate terms, pregnancy, physical sexual acts—including those acts portrayed as an assault or a forced sexual violent act against one’s will—, prostitution, pornography, and abuse. |
Violence | Violence describes content related to physical actions intended to hurt, injure, damage, or kill someone or something. It also includes weapons, guns and related entities, such as manufacturers, associations, legislation, and similar. |
Self-harm | Self-harm describes content related to physical actions intended to purposely hurt, injure, or damage one’s body or kill oneself. |
Classification can be multi-labeled. For example, when a text sample goes through the text moderation model, it could be classified as both Sexual content and Violence. |
Severity levels
Every harm category the service applies also comes with a severity level rating. The severity level is meant to indicate the severity of the consequences of showing the flagged content.
Severity | Label |
---|---|
0 | Safe |
2 | Low |
4 | Medium |
6 | High |
Text blocklist management
Following operations are supported to manage your text blocklist:
- Create or modify a blocklist
- List all blocklists
- Get a blocklist by blocklistName
- Add blockItems to a blocklist
- Remove blockItems from a blocklist
- List all blockItems in a blocklist by blocklistName
- Get a blockItem in a blocklist by blockItemId and blocklistName
- Delete a blocklist and all of its blockItems
You can set the blocklists you want to use when analyze text, then you can get blocklist match result from returned response.
Examples
The following section provides several code snippets covering some of the most common Content Safety service tasks, including:
Please refer to sample data for the data used here. For more samples, please refer to samples.
Analyze text
Analyze text without blocklists
import os
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
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
text_path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_data/text.txt"))
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
# Read sample data
with open(text_path) as f:
# Build request
request = AnalyzeTextOptions(text=f.readline(), categories=[TextCategory.HATE, TextCategory.SELF_HARM])
# 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
if response.hate_result:
print(f"Hate severity: {response.hate_result.severity}")
if response.self_harm_result:
print(f"SelfHarm severity: {response.self_harm_result.severity}")
Analyze text with blocklists
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety.models import AnalyzeTextOptions
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
input_text = "I h*te you and I want to k*ll you."
try:
# After you edit your blocklist, it usually takes effect in 5 minutes, please wait some time before analyzing with blocklist after editing.
analysis_result = client.analyze_text(AnalyzeTextOptions(text=input_text, blocklist_names=[blocklist_name], break_by_blocklists=False))
if analysis_result and analysis_result.blocklists_match_results:
print("\nBlocklist match results: ")
for match_result in analysis_result.blocklists_match_results:
print(f"Block item was hit in text, Offset={match_result.offset}, Length={match_result.length}.")
print(f"BlocklistName: {match_result.blocklist_name}, BlockItemId: {match_result.block_item_id}, BlockItemText: {match_result.block_item_text}")
except HttpResponseError as e:
print("\nAnalyze text failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Analyze image
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
image_path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_data/image.jpg"))
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
# Build request
with open(image_path, "rb") as file:
request = AnalyzeImageOptions(image=ImageData(content=file.read()))
# Analyze image
try:
response = client.analyze_image(request)
except HttpResponseError as e:
print("Analyze image failed.")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
if response.hate_result:
print(f"Hate severity: {response.hate_result.severity}")
if response.self_harm_result:
print(f"SelfHarm severity: {response.self_harm_result.severity}")
if response.sexual_result:
print(f"Sexual severity: {response.sexual_result.severity}")
if response.violence_result:
print(f"Violence severity: {response.violence_result.severity}")
Manage text blocklist
Create or update text blocklist
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
blocklist_description = "Test blocklist management."
try:
blocklist = client.create_or_update_text_blocklist(blocklist_name=blocklist_name, resource={"description": blocklist_description})
if blocklist:
print("\nBlocklist created or updated: ")
print(f"Name: {blocklist.blocklist_name}, Description: {blocklist.description}")
except HttpResponseError as e:
print("\nCreate or update text blocklist failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
List text blocklists
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
try:
blocklists = client.list_text_blocklists()
if blocklists:
print("\nList blocklists: ")
for blocklist in blocklists:
print(f"Name: {blocklist.blocklist_name}, Description: {blocklist.description}")
except HttpResponseError as e:
print("\nList text blocklists failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Get text blocklist
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
try:
blocklist = client.get_text_blocklist(blocklist_name=blocklist_name)
if blocklist:
print("\nGet blocklist: ")
print(f"Name: {blocklist.blocklist_name}, Description: {blocklist.description}")
except HttpResponseError as e:
print("\nGet text blocklist failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Delete text blocklist
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
try:
client.delete_text_blocklist(blocklist_name=blocklist_name)
print(f"\nDeleted blocklist: {blocklist_name}")
except HttpResponseError as e:
print("\nDelete blocklist failed:")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Add blockItems
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety.models import (
TextBlockItemInfo,
AddBlockItemsOptions
)
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
block_item_text_1 = "k*ll"
block_item_text_2 = "h*te"
block_items = [TextBlockItemInfo(text=block_item_text_1), TextBlockItemInfo(text=block_item_text_2)]
try:
result = client.add_block_items(
blocklist_name=blocklist_name,
body=AddBlockItemsOptions(block_items=block_items),
)
if result and result.value:
print("\nBlock items added: ")
for block_item in result.value:
print(f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}")
except HttpResponseError as e:
print("\nAdd block items failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
List blockItems
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
try:
block_items = client.list_text_blocklist_items(blocklist_name=blocklist_name)
if block_items:
print("\nList block items: ")
for block_item in block_items:
print(f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}")
except HttpResponseError as e:
print("\nList block items failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Get blockItem
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety.models import TextBlockItemInfo, AddBlockItemsOptions
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
block_item_text_1 = "k*ll"
try:
# Add a blockItem
add_result = client.add_block_items(
blocklist_name=blocklist_name,
body=AddBlockItemsOptions(block_items=[TextBlockItemInfo(text=block_item_text_1)]),
)
if not add_result or not add_result.value or len(add_result.value) <= 0:
raise RuntimeError("BlockItem not created.")
block_item_id = add_result.value[0].block_item_id
# Get this blockItem by blockItemId
block_item = client.get_text_blocklist_item(
blocklist_name=blocklist_name,
block_item_id= block_item_id
)
print("\nGet blockitem: ")
print(f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}")
except HttpResponseError as e:
print("\nGet block item failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Remove blockItems
import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety.models import (
TextBlockItemInfo,
AddBlockItemsOptions,
RemoveBlockItemsOptions
)
from azure.core.exceptions import HttpResponseError
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# Create an Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
blocklist_name = "TestBlocklist"
block_item_text_1 = "k*ll"
try:
# Add a blockItem
add_result = client.add_block_items(
blocklist_name=blocklist_name,
body=AddBlockItemsOptions(block_items=[TextBlockItemInfo(text=block_item_text_1)]),
)
if not add_result or not add_result.value or len(add_result.value) <= 0:
raise RuntimeError("BlockItem not created.")
block_item_id = add_result.value[0].block_item_id
# Remove this blockItem by blockItemId
client.remove_block_items(
blocklist_name=blocklist_name,
body=RemoveBlockItemsOptions(block_item_ids=[block_item_id])
)
print(f"\nRemoved blockItem: {add_result.value[0].block_item_id}")
except HttpResponseError as e:
print("\nRemove block item failed: ")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
Troubleshooting
General
Azure AI Content Safety client library will raise exceptions defined in Azure Core. Error codes are defined as below:
Error Code | Possible reasons | Suggestions |
---|---|---|
InvalidRequestBody | One or more fields in the request body do not match the API definition. | 1. Check the API version you specified in the API call. 2. Check the corresponding API definition for the API version you selected. |
InvalidResourceName | The resource name you specified in the URL does not meet the requirements, like the blocklist name, blocklist term ID, etc. | 1. Check the API version you specified in the API call. 2. Check whether the given name has invalid characters according to the API definition. |
ResourceNotFound | The resource you specified in the URL may not exist, like the blocklist name. | 1. Check the API version you specified in the API call. 2. Double check the existence of the resource specified in the URL. |
InternalError | Some unexpected situations on the server side have been triggered. | 1. You may want to retry a few times after a small period and see it the issue happens again. 2. Contact Azure Support if this issue persists. |
ServerBusy | The server side cannot process the request temporarily. | 1. You may want to retry a few times after a small period and see it the issue happens again. 2.Contact Azure Support if this issue persists. |
TooManyRequests | The current RPS has exceeded the quota for your current SKU. | 1. Check the pricing table to understand the RPS quota. 2.Contact Azure Support if you need more QPS. |
Logging
This library uses the standard logging library for logging.
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO
level.
Detailed DEBUG
level logging, including request/response bodies and unredacted headers, can be enabled on the client or per-operation with the logging_enable
keyword argument.
See full SDK logging documentation with examples here.
Optional Configuration
Optional keyword arguments can be passed in at the client and per-operation level. The azure-core reference documentation describes available configurations for retries, logging, transport protocols, and more.
Next steps
Additional documentation
For more extensive documentation on Azure Content Safety, see the Azure AI Content Safety on docs.microsoft.com.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for azure-ai-contentsafety-1.0.0b1.zip
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2036840750aab944f55d36d6825acc5944f18dadfa97c6feac5b47629adc77f9 |
|
MD5 | 19fe21b932818757e090349144a55891 |
|
BLAKE2b-256 | 1f3aaa4ae03797cd06adb33248d62e905b976135168fac1ea5a06d0709db07bf |
Hashes for azure_ai_contentsafety-1.0.0b1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec037d26b1b3c6e8fe0e1f1c97afcffcbdc939d89bfedbc4b0aab18d8b246316 |
|
MD5 | 8f6bcf3e825d3c9f858d4963d0096c34 |
|
BLAKE2b-256 | 92db2791fedf10fd2148f64deba8add4e2c558669bdf04c6eb10e4ab2697be96 |