This topic describes how to use Content Moderation SDK for Python to moderate text for spam such as pornographic and terrorist content.
Description
Only synchronous moderation is supported for text anti-spam. For more information about the related parameters, see /green/text/scan.
You can send a request to moderate one or more text entries. You are charged based on the number of text entries that are moderated. For more information, see Billing overview.
Prerequisites
Python dependencies are installed. For more information, see Installation.
NoteYou must use the required Python version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
The Extension.Uploader utility class is downloaded and imported into your project.
Submit text moderation tasks
To moderate text for spam, you must submit a text moderation task. If a text moderation result does not meet your expectations, you can use TextFeedbackRequest to provide feedback on the machine-assisted moderation result. Text anti-spam allows you to add custom terms, such as the brand terms of competitors. If the moderated text contains the terms that you add, the value of the suggestion parameter is block in the returned machine-assisted moderation result.
You can add terms in the Content Moderation console or by calling an API operation.
Operation | Description | Supported region |
TextScanRequest | Submits text moderation tasks with the scenes parameter set to antispam. |
|
Sample code
# coding=utf-8
# The following code provides an example on how to call the TextScanRequest operation.
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import TextScanRequest
import json
import uuid
import datetime
# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request.
request = TextScanRequest.TextScanRequest()
request.set_accept_format('JSON')
task1 = {"dataId": str(uuid.uuid1()),
"content":"textContentToBeModerated",
"time":datetime.datetime.now().microsecond
}
# Set the scenes parameter to antispam.
request.set_content(bytearray(json.dumps({"tasks": [task1], "scenes": ["antispam"]}), "utf-8"))
response = clt.do_action_with_exception(request)
print(response)
result = json.loads(response)
if 200 == result["code"]:
taskResults = result["data"]
for taskResult in taskResults:
if (200 == taskResult["code"]):
sceneResults = taskResult["results"]
for sceneResult in sceneResults:
scene = sceneResult["scene"]
suggestion = sceneResult["suggestion"]
# Take further action based on the values of the scene and suggestion parameters.
Provide feedback on text anti-spam results
If a text anti-spam result does not meet your expectations, you can call the TextFeedbackRequest operation to provide feedback on the machine-assisted moderation result.
The server corrects the text anti-spam result based on your feedback and adds the feedback to a text library. When you submit text that matches the text pattern next time, the system returns the text anti-spam result that is corrected based on your feedback.
Operation | Description | Supported region |
TextFeedbackRequest | Provides feedback on a text anti-spam result to correct the machine-assisted moderation result that does not meet your expectations. |
|
Sample code
# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import TextFeedbackRequest
import json
# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = TextFeedbackRequest.TextFeedbackRequest()
request.set_accept_format('JSON')
request.set_content(
json.dumps({"dataId": "ID of the moderated text", "taskId": "ID of the text moderation task",
"content": "Text content", "label": "spam", "note": "Remarks"}))
try:
response = clt.do_action_with_exception(request)
print(response)
result = json.loads(response)
if 200 == result["code"]:
print("response success.")
except Exception as err:
print(err)