Multimodal asynchronous review SDK reference

Updated at:

This topic describes how to integrate the AI Content Safety Guardrail SDK to submit asynchronous content review tasks and retrieve moderation results.

Step 1: Activate the service

Go to the AI Content Safety Guardrail page and activate the service. The default billing method is pay-as-you-go. Daily fees are calculated based on actual usage, and no fees are incurred if the service is not called. After you start using the API, the system automatically generates bills based on your usage.

Step 2: Grant RAM user permissions

Before you integrate the SDK or API, you must grant permissions to a RAM user. You can create an AccessKey for your Alibaba Cloud account or a RAM user. The AccessKey is required for identity verification when you call API operations. For instructions, see Create a RAM user and grant permissions.

Step 3: Install the SDK

The following table lists the supported regions and endpoints for AI Content Safety Guardrail.

Region

Public endpoint

VPC endpoint

China (Shanghai)

green-cip.cn-shanghai.aliyuncs.com

green-cip-vpc.cn-shanghai.aliyuncs.com

China (Beijing)

green-cip.cn-beijing.aliyuncs.com

green-cip-vpc.cn-beijing.aliyuncs.com

China (Hangzhou)

green-cip.cn-hangzhou.aliyuncs.com

green-cip-vpc.cn-hangzhou.aliyuncs.com

China (Shenzhen)

green-cip.cn-shenzhen.aliyuncs.com

green-cip-vpc.cn-shenzhen.aliyuncs.com

China (Chengdu)

green-cip.cn-chengdu.aliyuncs.com

N/A

Singapore

green-cip.ap-southeast-1.aliyuncs.com

green-cip-vpc.ap-southeast-1.aliyuncs.com

Note

If you need SDK sample code in other programming languages, use the OpenAPI Developer Portal to debug API operations online. The portal automatically generates SDK sample code.

Code examples

Java SDK

Java 1.8 or later is supported.

For source code, see Java SDK source code or Java SDK source code (OSS path).

To use the SDK in a Maven project, add the following dependency to the pom.xml file.

1. Add the following dependency to the dependencies section.

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>green20220302</artifactId>
  <version>3.6.1</version>
</dependency>

2. Java SDK code examples.

  • Submit a video review task

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyun.green20220302.models.MultiModalGuardAsyncRequest;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResponse;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResponseBody;
import com.aliyun.teaopenapi.models.Config;

public class MultiModalGuardAsyncDemo {
  public static void main(String[] args) throws Exception {
    Config config = new Config();
    /**
         * Alibaba Cloud accounts have access to all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
         * Common methods to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
    config.setAccessKeyId("We recommend that you obtain the AccessKey ID of a RAM user from environment variables");
    config.setAccessKeySecret("We recommend that you obtain the AccessKey Secret of a RAM user from environment variables");
    // Modify the region and endpoint based on your actual requirements.
    config.setRegionId("cn-shanghai");
    config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
    // Read timeout period. Unit: milliseconds.
    config.setReadTimeout(10000);
    // Connection timeout period. Unit: milliseconds.
    config.setConnectTimeout(3000);
    // Set the HTTP proxy.
    //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
    // Set the HTTPS proxy.
    //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
    com.aliyun.green20220302.Client client = new com.aliyun.green20220302.Client(config);

    JSONObject serviceParameters = new JSONObject();
    serviceParameters.put("url", "https://xxx.mp4");
    // Set the callback.
    //serviceParameters.put("seed", "123");
    //serviceParameters.put("callback", "http://www.aliyunXXX.com/");
    MultiModalGuardAsyncRequest request = new MultiModalGuardAsyncRequest();
    request.setService("video_security_check");
    request.setServiceParameters(JSON.toJSONString(serviceParameters));

    try {
      MultiModalGuardAsyncResponse response = client.multiModalGuardAsync(request);
      if (200 == response.getStatusCode() ) {
        MultiModalGuardAsyncResponseBody result = response.getBody();
        System.out.println(JSON.toJSONString(result, true));
        Integer code = result.getCode();
        if (200 == code) {
          MultiModalGuardAsyncResponseBody.MultiModalGuardAsyncResponseBodyData data = result.getData();
          System.out.println("multi modal guard async task submit success, taskId = " + data.getTaskId());
        } else {
          System.out.println("multi modal guard async task not success. code:" + code + ", msg:" + result.getMessage());
        }
      } else {
        System.out.println("response not success. status:" + response.getStatusCode());
      }
    } catch (Exception e) {
      System.out.println("multi modal guard async task submit exception" + e.getMessage());
    }
  }
}
  • Get a video review task result

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyun.green20220302.models.MultiModalGuardAsyncResultRequest;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResultResponse;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResultResponseBody;
import com.aliyun.teaopenapi.models.Config;

public class MultiModalGuardAsyncResultDemo {
  public static void main(String[] args) throws Exception {
    Config config = new Config();
    /**
         * Alibaba Cloud accounts have access to all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
         * Common methods to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
    config.setAccessKeyId("We recommend that you obtain the AccessKey ID of a RAM user from environment variables");
    config.setAccessKeySecret("We recommend that you obtain the AccessKey Secret of a RAM user from environment variables");
    // Modify the region and endpoint based on your actual requirements.
    config.setRegionId("cn-shanghai");
    config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
    // Read timeout period. Unit: milliseconds.
    config.setReadTimeout(10000);
    // Connection timeout period. Unit: milliseconds.
    config.setConnectTimeout(3000);
    // Set the HTTP proxy.
    //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
    // Set the HTTPS proxy.
    //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
    com.aliyun.green20220302.Client client = new com.aliyun.green20220302.Client(config);

    MultiModalGuardAsyncResultRequest request = new MultiModalGuardAsyncResultRequest();
    JSONObject serviceParameters = new JSONObject();
    // The taskId returned when the task was submitted.
    serviceParameters.put("taskId", "vi_f_XXX");
    request.setService("video_security_check");
    request.setServiceParameters(JSON.toJSONString(serviceParameters));

    try {
      MultiModalGuardAsyncResultResponse response = client.multiModalGuardAsyncResult(request);
      if (200 == response.getStatusCode() ) {
        MultiModalGuardAsyncResultResponseBody result = response.getBody();
        System.out.println(JSON.toJSONString(result, true));
        Integer code = result.getCode();
        if (200 == code) {
          MultiModalGuardAsyncResultResponseBody.MultiModalGuardAsyncResultResponseBodyData data = result.getData();
          System.out.println("get multi modal guard async result success");
          System.out.println("suggestion = " + data.getSuggestion());
          // If audio detection is enabled, the audio result is returned.
          System.out.println("audioResult = " + JSON.toJSONString(data.getAudioResult()));
          // If frame detection is enabled, the video result is returned.
          System.out.println("frameResult = " + JSON.toJSONString(data.getFrameResult()));
        } else {
          System.out.println("multi modal guard async result not success. code:" + code + ", msg:" + result.getMessage());
        }
      } else {
        System.out.println("response not success. status:" + response.getStatusCode());
      }
    } catch (Exception e) {
      System.out.println("multi modal guard async result exception" + e.getMessage());
    }
  }
}
  • Submit a multi-image review task

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.models.MultiModalGuardAsyncRequest;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResponse;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResponseBody;
import com.aliyun.teaopenapi.models.Config;
import java.util.ArrayList;
import java.util.List;
public class MultiImageGuardAsyncDemo {
  public static void main(String[] args) throws Exception {
    Config config = new Config();
    /**
         * Alibaba Cloud accounts have access to all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
         * Common methods to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
    config.setAccessKeyId("We recommend that you obtain the AccessKey ID of a RAM user from environment variables");
    config.setAccessKeySecret("We recommend that you obtain the AccessKey Secret of a RAM user from environment variables");
    // Modify the region and endpoint based on your actual requirements.
    config.setRegionId("cn-shanghai");
    config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
    // Read timeout period. Unit: milliseconds.
    config.setReadTimeout(10000);
    // Connection timeout period. Unit: milliseconds.
    config.setConnectTimeout(3000);
    // Set the HTTP proxy.
    //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
    // Set the HTTPS proxy.
    //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
    com.aliyun.green20220302.Client client = new com.aliyun.green20220302.Client(config);
    JSONObject serviceParameters = new JSONObject();
    // URLs of images to be detected.
    List<String> imageUrls = new ArrayList<>();
    imageUrls.add("https://xxxxx/xxx1.jpg");
    imageUrls.add("https://xxxxx/xxx2.jpg");
    serviceParameters.put("imageUrls", imageUrls);
    // Set the callback.
    //serviceParameters.put("seed", "123");
    //serviceParameters.put("callback", "http://www.aliyunXXX.com/");
    MultiModalGuardAsyncRequest request = new MultiModalGuardAsyncRequest();
    request.setService("img_query_security_check");
    request.setServiceParameters(JSON.toJSONString(serviceParameters));
    try {
      MultiModalGuardAsyncResponse response = client.multiModalGuardAsync(request);
      if (200 == response.getStatusCode()) {
        MultiModalGuardAsyncResponseBody result = response.getBody();
        System.out.println(JSON.toJSONString(result, true));
        Integer code = result.getCode();
        if (200 == code) {
          MultiModalGuardAsyncResponseBody.MultiModalGuardAsyncResponseBodyData data = result.getData();
          System.out.println("multi image guard async task submit success, taskId = " + data.getTaskId());
        } else {
          System.out.println("multi image guard async task not success. code:" + code + ", msg:" + result.getMessage());
        }
      } else {
        System.out.println("response not success. status:" + response.getStatusCode());
      }
    } catch (Exception e) {
      System.out.println("multi image guard async task submit exception" + e.getMessage());
    }
  }
}
  • Get a multi-image review task result

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResultRequest;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResultResponse;
import com.aliyun.green20220302.models.MultiModalGuardAsyncResultResponseBody;
import com.aliyun.teaopenapi.models.Config;
public class MultiImageGuardAsyncResultDemo {
  public static void main(String[] args) throws Exception {
    Config config = new Config();
    /**
         * Alibaba Cloud accounts have access to all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
         * Common methods to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey Secret of a RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
    config.setAccessKeyId("We recommend that you obtain the AccessKey ID of a RAM user from environment variables");
    config.setAccessKeySecret("We recommend that you obtain the AccessKey Secret of a RAM user from environment variables");
    // Modify the region and endpoint based on your actual requirements.
    config.setRegionId("cn-shanghai");
    config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
    // Read timeout period. Unit: milliseconds.
    config.setReadTimeout(10000);
    // Connection timeout period. Unit: milliseconds.
    config.setConnectTimeout(3000);
    // Set the HTTP proxy.
    //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
    // Set the HTTPS proxy.
    //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
    com.aliyun.green20220302.Client client = new com.aliyun.green20220302.Client(config);
    MultiModalGuardAsyncResultRequest request = new MultiModalGuardAsyncResultRequest();
    JSONObject serviceParameters = new JSONObject();
    // The taskId returned when the task was submitted.
    serviceParameters.put("taskId", "vi_f_xxx");
    request.setService("img_query_security_check");
    request.setServiceParameters(JSON.toJSONString(serviceParameters));
    try {
      MultiModalGuardAsyncResultResponse response = client.multiModalGuardAsyncResult(request);
      if (200 == response.getStatusCode() ) {
        MultiModalGuardAsyncResultResponseBody result = response.getBody();
        System.out.println(JSON.toJSONString(result, true));
        Integer code = result.getCode();
        if (200 == code) {
          MultiModalGuardAsyncResultResponseBody.MultiModalGuardAsyncResultResponseBodyData data = result.getData();
          System.out.println("get multi image guard async result success");
          System.out.println("suggestion = " + data.getSuggestion());
          // If audio detection is enabled, the audio result is returned.
          System.out.println("audioResult = " + JSON.toJSONString(data.getAudioResult()));
          // If frame detection is enabled, the video result is returned.
          System.out.println("frameResult = " + JSON.toJSONString(data.getFrameResult()));
        } else {
          System.out.println("multi image guard async result not success. code:" + code + ", msg:" + result.getMessage());
        }
      } else {
        System.out.println("response not success. status:" + response.getStatusCode());
      }
    } catch (Exception e) {
      System.out.println("multi image guard async result exception" + e.getMessage());
    }
  }
}

Python SDK

Python 3.6 or later is supported.

For source code, see Python SDK source code.

1. Run the following command to install the dependencies:

pip install alibabacloud_green20220302==3.4.1

2. Python SDK code examples.

  • Submit a video review task

# coding=utf-8
# python version >= 3.6
from alibabacloud_green20220302.client import Client
from alibabacloud_green20220302 import models
from alibabacloud_tea_openapi.models import Config
import json

config = Config(
    # We recommend that you do not save the AccessKey ID and AccessKey Secret in the project code to prevent AccessKey leakage.
    # 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']
    access_key_id='We recommend that you obtain the AccessKey ID of a RAM user from environment variables',
    access_key_secret='We recommend that you obtain the AccessKey Secret of a RAM user from environment variables',
    # Connection timeout period. Unit: milliseconds.
    connect_timeout=10000,
    # Read timeout period. Unit: milliseconds.
    read_timeout=3000,
    region_id='cn-shanghai',
    endpoint='green-cip.cn-shanghai.aliyuncs.com'
)

clt = Client(config)

serviceParameters = {
    'url': 'https://abc.alicdn.com/tfs/xxxxxxxxxx001.mp4'
}
multiModalGuardRequest = models.MultiModalGuardAsyncRequest(
    service='video_security_check',
    service_parameters=json.dumps(serviceParameters)
)

try:
    response = clt.multi_modal_guard_async(multiModalGuardRequest)
    if response.status_code == 200:
        result = response.body
        print('response success. result:{}'.format(result))
    else:
        print('response not success. status:{} ,result:{}'.format(response.status_code, response))
except Exception as err:
    print(err)
  • Get a video review task result

# coding=utf-8
# python version >= 3.6
from alibabacloud_green20220302.client import Client
from alibabacloud_green20220302 import models
from alibabacloud_tea_openapi.models import Config
import json
config = Config(
    access_key_id='We recommend that you obtain the AccessKey ID of a RAM user from environment variables',
    access_key_secret='We recommend that you obtain the AccessKey Secret of a RAM user from environment variables',
    connect_timeout=10000,
    read_timeout=3000,
    region_id='cn-shanghai',
    endpoint='green-cip.cn-shanghai.aliyuncs.com'
)
clt = Client(config)
serviceParameters = {
    'taskId': 'vi_f_xxx'
}
multiModalGuardResultRequest = models.MultiModalGuardAsyncResultRequest(
    service='video_security_check',
    service_parameters=json.dumps(serviceParameters)
)
try:
    response = clt.multi_modal_guard_async_result(multiModalGuardResultRequest)
    if response.status_code == 200:
        result = response.body
        print('response success. result:{}'.format(result))
    else:
        print('response not success. status:{} ,result:{}'.format(response.status_code, response))
except Exception as err:
    print(err)
  • Submit a multi-image review task

# coding=utf-8
# python version >= 3.6
from alibabacloud_green20220302.client import Client
from alibabacloud_green20220302 import models
from alibabacloud_tea_openapi.models import Config
import json
config = Config(
    access_key_id='We recommend that you obtain the AccessKey ID of a RAM user from environment variables',
    access_key_secret='We recommend that you obtain the AccessKey Secret of a RAM user from environment variables',
    connect_timeout=10000,
    read_timeout=3000,
    region_id='cn-shanghai',
    endpoint='green-cip.cn-shanghai.aliyuncs.com'
)
clt = Client(config)
serviceParameters = {
    'imageUrls': ['https://img.alicdn.com/tfs/xxxxxxxxxx001.jpg']
}
multiModalGuardRequest = models.MultiModalGuardAsyncRequest(
    service='img_query_security_check',
    service_parameters=json.dumps(serviceParameters)
)
try:
    response = clt.multi_modal_guard_async(multiModalGuardRequest)
    if response.status_code == 200:
        result = response.body
        print('response success. result:{}'.format(result))
    else:
        print('response not success. status:{} ,result:{}'.format(response.status_code, response))
except Exception as err:
    print(err)
  • Get a multi-image review task result

# coding=utf-8
# python version >= 3.6
from alibabacloud_green20220302.client import Client
from alibabacloud_green20220302 import models
from alibabacloud_tea_openapi.models import Config
import json
config = Config(
    access_key_id='We recommend that you obtain the AccessKey ID of a RAM user from environment variables',
    access_key_secret='We recommend that you obtain the AccessKey Secret of a RAM user from environment variables',
    connect_timeout=10000,
    read_timeout=3000,
    region_id='cn-shanghai',
    endpoint='green-cip.cn-shanghai.aliyuncs.com'
)
clt = Client(config)
serviceParameters = {
    'taskId': 'vi_f_xxx'
}
multiModalGuardResultRequest = models.MultiModalGuardAsyncResultRequest(
    service='img_query_security_check',
    service_parameters=json.dumps(serviceParameters)
)
try:
    response = clt.multi_modal_guard_async_result(multiModalGuardResultRequest)
    if response.status_code == 200:
        result = response.body
        print('response success. result:{}'.format(result))
    else:
        print('response not success. status:{} ,result:{}'.format(response.status_code, response))
except Exception as err:
    print(err)