Use the AI Guardrails SDK for Python to create and manage custom image libraries, then reference those libraries in image and video moderation tasks to detect pornographic content, terrorist content, and ad violations.
All examples share the same client initialization pattern. Reuse the client across requests to improve performance and avoid repeated connection overhead.
Prerequisites
Before you begin, ensure that you have:
Python dependencies installed. See Installation.
Use the Python version specified in the Installation guide. An incompatible version causes operation calls to fail.
The Extension.Uploader utility class downloaded and imported into your project
List image libraries
DescribeImageLibRequest returns all image libraries in your account, including custom image libraries and feedback-based image libraries.
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeImageLibRequest
# Reuse this client across requests to improve performance.
clt = client.AcsClient(
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"), # AccessKey ID
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), # AccessKey secret
"cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")
# Create a new request object for each request. Request objects cannot be reused.
request = DescribeImageLibRequest.DescribeImageLibRequest()
request.set_ServiceModule("open_api")
response = clt.do_action_with_exception(request)
print(response)Create a custom image library
CreateImageLibRequest creates a new custom image library. Set Scene and Category based on your moderation policy.
| Parameter | Type | Description |
|---|---|---|
ServiceModule | String | Set to open_api |
Name | String | Display name for the library |
Scene | String | Moderation scenario: PORN, TERRORISM, or AD |
Category | String | Library type: BLACK (blocklist) or WHITE (allowlist). Blocklist images trigger violations; allowlist images bypass moderation. |
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import CreateImageLibRequest
clt = client.AcsClient(
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
"cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")
request = CreateImageLibRequest.CreateImageLibRequest()
request.set_ServiceModule("open_api")
request.set_Name("Pornography detection blocklist")
request.set_Scene("PORN")
request.set_Category("BLACK")
response = clt.do_action_with_exception(request)
print(response)For a full list of parameters, see Create an image library.
Update a custom image library
UpdateImageLibRequest changes the name or scene of an existing custom image library. Identify the library by its Id.
| Parameter | Type | Description |
|---|---|---|
Id | Integer | ID of the library to update |
Name | String | New display name |
Scene | String | Moderation scenario: PORN, TERRORISM, or AD |
Category | String | Library type: BLACK (blocklist) or WHITE (allowlist) |
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import UpdateImageLibRequest
clt = client.AcsClient(
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
"cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")
request = UpdateImageLibRequest.UpdateImageLibRequest()
request.set_Id(12345)
request.set_Name("New name of the image library")
request.set_Scene("PORN")
request.set_Category("WHITE")
response = clt.do_action_with_exception(request)
print(response)Delete a custom image library
Deleting a custom image library also deletes all images in the library.
DeleteImageLibRequest deletes the library identified by Id.
| Parameter | Type | Description |
|---|---|---|
Id | Integer | ID of the library to delete |
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageLibRequest
clt = client.AcsClient(
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
"cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")
request = DeleteImageLibRequest.DeleteImageLibRequest()
request.set_Id(12345)
response = clt.do_action_with_exception(request)
print(response)List images in a custom image library
DescribeImageFromLibRequest returns a paginated list of images in a specific library.
| Parameter | Type | Description |
|---|---|---|
ImageLibId | Integer | ID of the library to query |
PageSize | Integer | Number of images per page |
CurrentPage | Integer | Page number to retrieve, starting from 1 |
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeImageFromLibRequest
clt = client.AcsClient(
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
"cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")
request = DescribeImageFromLibRequest.DescribeImageFromLibRequest()
request.set_ImageLibId(12345)
request.set_PageSize(10)
request.set_CurrentPage(1)
response = clt.do_action_with_exception(request)
print(response)Remove images from a custom image library
DeleteImageFromLibRequest removes one or more images from a library. Pass a list of image IDs to Ids.
| Parameter | Type | Description |
|---|---|---|
Ids | String | List of image IDs to remove, for example "['669310']" |
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageFromLibRequest
clt = client.AcsClient(
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
"cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")
request = DeleteImageFromLibRequest.DeleteImageFromLibRequest()
request.set_Ids("['669310']")
response = clt.do_action_with_exception(request)
print(response)What's next
Create an image library — full API reference for library parameters
Installation — SDK setup and supported Python versions