File moderation

更新时间:
复制 MD 格式

This topic describes how to use the Python software development kit (SDK) to detect text and images in files.

Feature description

File moderation is an asynchronous process and does not return moderation results in real time. For more information about the parameters, see Asynchronous file moderation.

Feature

File detection

Detects the image file format.

DOC, DOCX, PPT, PPTX, PDF, XLS, XLSX, and TXT.

Text detection in images

Support

File size limit

200 MB

Number of concurrent file detection tasks

10

Offline moderation mode (for batch file moderation)

Supported

Other

  • Enable or disable image moderation.

  • You can specify the maximum number of pages to detect in a file.

Prerequisites

  • Python dependencies are installed. For more information, see Installation.

    Note

    You 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 an asynchronous file moderation task

API

Description

Supported regions

FileAsyncScanV2Request

Submits an asynchronous file moderation task to detect text and images in a file.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

Sample code

# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import FileAsyncScanV2Request
import json

# Note: To improve moderation performance, reuse the instantiated client. This avoids repeatedly establishing connections.
# Common methods to obtain environment variables:
# Obtain the AccessKey ID of a Resource Access Management (RAM) user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of a RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("The AccessKey ID of your RAM user", "The AccessKey secret of your RAM user", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = FileAsyncScanV2Request.FileAsyncScanV2Request()
request.set_accept_format('JSON')
request.set_content(
    json.dumps({"modType": "All", "maxPages": 30, "offline": False, "bizType": "aligreen_default_bizType",
                "tasks": [
                    {"dataId": "The data ID",
                     "url": "The URL of the file to moderate"}
                ]}))

try:
    response = clt.do_action_with_exception(request)
    print(response)
except Exception as err:
    print(err)

Get the results of an asynchronous file moderation task

Interface

Description

Supported regions

FileAsyncScanResultsRequest

Gets the results of an asynchronous file moderation task.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

Sample code

# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import FileAsyncScanResultsRequest
import json


# Note: To improve moderation performance, reuse the instantiated client. This avoids repeatedly establishing connections.
# Common methods to obtain environment variables:
# Obtain the AccessKey ID of a RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of a RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("The AccessKey ID of your RAM user", "The AccessKey secret of your RAM user", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = FileAsyncScanResultsRequest.FileAsyncScanResultsRequest()
request.set_accept_format('JSON')

request.set_content(
    json.dumps({"taskIds": ["The ID of the file moderation task"]}))

try:
    response = clt.do_action_with_exception(request)
    print(response)
except Exception as err:
    print(err)