Audio moderation

Updated at:

This topic describes how to use the AI Guardrails software development kit (SDK) for Java to detect spam in real-time audio streams or audio files.

Feature description

Audio stream and audio file moderation are both asynchronous. You can retrieve the moderation results by polling or using callbacks. For more information about the scenes request parameter, the label classification parameter, and the suggestion parameter in the response, see Asynchronous audio moderation.

Audio moderation is billed per minute based on the duration of the moderated audio files or streams. The total duration is calculated daily. If the total duration for a day is less than one minute, it is billed as one minute.

Note
  • This SDK supports only public audio URLs. It does not support local files or binary data.

  • Supported URL types:

    • Public HTTP/HTTPS URLs that do not exceed 2048 characters in length.

    • Alibaba Cloud OSS URL: oss://<bucket-name>.<endpoint>/<object-name>. You must authorize AI Guardrails to access the destination OSS bucket. The bucket and the AI Guardrails service must be in the same region. For more information, see Authorize AI Guardrails to access an OSS bucket.

Prerequisites

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

    Note

    You must use the Java 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 if you submit a local image or a binary image stream for image moderation.

Submit asynchronous audio moderation tasks

Interface

Description

Supported region

VoiceAsyncScanRequest

Asynchronously detects violations in audio streams or audio files.

  • Supported audio stream protocols: HTTP, RTMP, and RTSP

  • Supported audio stream formats: M3U8 and FLV

cn-shanghai: China (Shanghai)

Sample code

  • Submit an audio URL for detection

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest;
    import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
           /**
             * An AccessKey of an Alibaba Cloud account has permissions for all APIs. We recommend that you use a RAM user for API access or daily O&M. 
             * Common methods for obtaining 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");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "Your RAM user's AccessKey ID",
                    "Your RAM user's AccessKey secret");
            // Note: To improve detection performance, reuse the instantiated client as much as possible and avoid creating repeated connections.     
            final IAcsClient client = new DefaultAcsClient(profile);
    
            VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); 
            asyncScanRequest.setAcceptFormat(FormatType.JSON); // Specify the API response format.
            asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method.
            asyncScanRequest.setRegionId("cn-shanghai");
            asyncScanRequest.setConnectTimeout(3000);
            asyncScanRequest.setReadTimeout(6000);
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task1 = new LinkedHashMap<String, Object>();
            // Replace the following URL with the URL of the audio file that you want to scan.
            task1.put("url", "https://xxxxxxxxxxxxxx");
            tasks.add(task1);
            JSONObject data = new JSONObject();
    
            System.out.println("==========Task count:" + tasks.size());
            data.put("scenes", Arrays.asList("antispam"));
            data.put("tasks", tasks);
            // If you want to scan an audio stream, set this parameter to true.
            data.put("live", false);
            asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
            System.out.println(JSON.toJSONString(data, true));
    
            try {
                HttpResponse httpResponse = client.doAction(asyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    if (200 == scrResponse.getInteger("code")) {
                        JSONArray taskResults = scrResponse.getJSONArray("data");
                        for (Object taskResult : taskResults) {
                            Integer code = ((JSONObject) taskResult).getInteger("code");
                            if (200 == code) {
                                final String taskId = ((JSONObject) taskResult).getString("taskId");
                                System.out.println("submit async task success, taskId = [" + taskId + "]");
                            } else {
                                System.out.println("task process fail: " + code);
                            }
                        }
                    } else {
                        System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
                    }
                } else {
                    System.out.println("response not success. status: " + httpResponse.getStatus());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • Submit a local audio file for detection

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest;
    import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import com.aliyuncs.green.extension.uploader.ClientUploader;
    
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            /**
             * An AccessKey of an Alibaba Cloud account has permissions to access all APIs. We recommend that you use a RAM user to make API calls for daily O&M. 
             * The following code shows 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");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "Obtain the AccessKey ID of a RAM user from an environment variable",
                    "Obtain the AccessKey secret of a RAM user from an environment variable");
            // Note: To improve detection performance, reuse the instantiated client. This avoids the need to repeatedly establish connections.
            final IAcsClient client = new DefaultAcsClient(profile);
    
            VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); 
            asyncScanRequest.setAcceptFormat(FormatType.JSON); // Specify the format of the API response.
            asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method.
            asyncScanRequest.setRegionId("cn-shanghai");
            asyncScanRequest.setConnectTimeout(3000);
            asyncScanRequest.setReadTimeout(6000);
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task1 = new LinkedHashMap<String, Object>();
            task1.put("type", "file");
            String url = null;
            ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false);
            try{
                url = uploader.uploadFile("d:/test.mp4");
            }catch (Exception e){
                e.printStackTrace();
            }
            task1.put("url", url);
            tasks.add(task1);
            JSONObject data = new JSONObject();
    
            System.out.println("==========Task count:" + tasks.size());
            data.put("scenes", Arrays.asList("antispam"));
            data.put("tasks", tasks);
            asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
            System.out.println(JSON.toJSONString(data, true));
    
            try {
                HttpResponse httpResponse = client.doAction(asyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    if (200 == scrResponse.getInteger("code")) {
                        JSONArray taskResults = scrResponse.getJSONArray("data");
                        for (Object taskResult : taskResults) {
                            Integer code = ((JSONObject) taskResult).getInteger("code");
                            if (200 == code) {
                                final String taskId = ((JSONObject) taskResult).getString("taskId");
                                System.out.println("submit async task success, taskId = [" + taskId + "]");
                            } else {
                                System.out.println("task process fail: " + code);
                            }
                        }
                    } else {
                        System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
                    }
                } else {
                    System.out.println("response not success. status: " + httpResponse.getStatus());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Query asynchronous audio moderation results

API

Description

Supported regions

VoiceAsyncScanResultsRequest

Queries asynchronous audio moderation results.

cn-shanghai: China (Shanghai)

Sample code

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        /**
         * An AccessKey of an Alibaba Cloud account has access permissions for all APIs. We recommend that you use a RAM user for API access or 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");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "Obtain the AccessKey ID of a RAM user from an environment variable.",
                "Obtain the AccessKey secret of a RAM user from an environment variable.");
        // Note: To improve detection performance, reuse the client instance. This avoids repeatedly establishing connections.
        final IAcsClient client = new DefaultAcsClient(profile);
        pollingScanResult(client, "The task ID returned after you submit an asynchronous voice scan task");
    }

    public static void pollingScanResult(IAcsClient client, String taskId) throws InterruptedException {
        int failCount = 0;
        boolean stop = false;
        do {
            // Set the query interval to 10 seconds.
            Thread.sleep(10 * 1000);
            JSONObject scanResult = getScanResult(client, taskId);
            if (scanResult == null || 200 != scanResult.getInteger("code")) {
                failCount++;
                System.out.println(taskId + ": get result fail, failCount=" + failCount);
                if (scanResult != null) {
                    System.out.println(taskId + ": errorMsg:" + scanResult.getString("msg"));
                }
                if (failCount > 20) {
                    break;
                }
                continue;
            }

            JSONArray taskResults = scanResult.getJSONArray("data");
            if (taskResults.isEmpty()) {
                System.out.println("failed");
                break;
            }

            for (Object taskResult : taskResults) {
                JSONObject result = (JSONObject) taskResult;
                Integer code = result.getInteger("code");
                if (280 == code) {
                    System.out.println(taskId + ": processing status: " + result.getString("msg"));
                } else if (200 == code) {
                    System.out.println(taskId + ": ========== SUCCESS ===========");
                    System.out.println(JSON.toJSONString(scanResult, true));
                    System.out.println(taskId + ": ========== SUCCESS ===========");
                    stop = true;
                } else {
                    System.out.println(taskId + ": ========== FAILED ===========");
                    System.out.println(JSON.toJSONString(scanResult, true));
                    System.out.println(taskId + ": ========== FAILED ===========");
                    stop = true;
                }
            }
        } while (!stop);
    }

    private static JSONObject getScanResult(IAcsClient client, String taskId) {
        VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
        getResultsRequest.setAcceptFormat(FormatType.JSON); // Specify the API response format.
        getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method.
        getResultsRequest.setEncoding("utf-8");
        getResultsRequest.setRegionId("cn-shanghai");


        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task1 = new LinkedHashMap<String, Object>();
        task1.put("taskId", taskId);
        tasks.add(task1);

        /**
         * Be sure to set the timeout period.
         */
        getResultsRequest.setConnectTimeout(3000);
        getResultsRequest.setReadTimeout(6000);

        try {
            getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

            HttpResponse httpResponse = client.doAction(getResultsRequest);
            if (httpResponse.isSuccess()) {
                return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
            } else {
                System.out.println("response not success. status: " + httpResponse.getStatus());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Short speech synchronous recognition

Sample code

/**

* The AccessKey of an Alibaba Cloud account grants full access permissions to all APIs. For security purposes, we recommend that you use a Resource Access Management (RAM) user for API access and daily O&M.

* The following are common ways to obtain environment variables:

* Method 1:

* Obtain the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");

* Obtain the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

* Method 2:

* Obtain the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");

* Obtain the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

*/

DefaultProfile profile = DefaultProfile.getProfile(

"cn-shanghai",

"We recommend that you obtain the AccessKey ID of the RAM user from an environment variable",

We recommend that you retrieve a RAM user's AccessKey secret from an environment variable.

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.VoiceSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
       /**
         * An AccessKey of an Alibaba Cloud account has permissions to call all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
         * The following are common methods for obtaining environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "Obtain the AccessKey ID of a RAM user from an environment variable",
                "Obtain the AccessKey secret of a RAM user from an environment variable");
        // Note: To improve moderation performance, reuse the instantiated client. This avoids the need to repeatedly establish connections.
        final IAcsClient client = new DefaultAcsClient(profile);

        VoiceSyncScanRequest asyncScanRequest = new VoiceSyncScanRequest();
        asyncScanRequest.setAcceptFormat(FormatType.JSON); // Specify the format of the API response.
        asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method.
        asyncScanRequest.setRegionId("cn-shanghai");
        asyncScanRequest.setConnectTimeout(3000);
        // Synchronous audio moderation is time-consuming. We recommend that you set the read timeout to 15 seconds or longer.
        asyncScanRequest.setReadTimeout(15000);

        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task1 = new LinkedHashMap<String, Object>();
        // Replace the following URL with the URL of the audio file that you want to moderate.
        task1.put("url", "https://xxxxxxxxxxxxxx");
        tasks.add(task1);
        JSONObject data = new JSONObject();

        System.out.println("==========Task count:" + tasks.size());
        data.put("scenes", Arrays.asList("antispam"));
        data.put("tasks", tasks);
        asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        System.out.println(JSON.toJSONString(data, true));

        try {
            HttpResponse httpResponse = client.doAction(asyncScanRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    for (Object taskResult : taskResults) {
                        Integer code = ((JSONObject) taskResult).getInteger("code");
                        if (200 == code) {
                            System.out.println("task process success, result:" + JSON.toJSONString(taskResult));
                        } else {
                            System.out.println("task process fail: " + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response fail:" + new String(httpResponse.getHttpContent(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}