Manual review for voice

Updated at:

This topic describes how to use the Java software development kit (SDK) to call the API for voice manual review.

Description

You can use manual review if the results of machine-assisted moderation for a voice file do not meet your expectations. For more information about the parameters, see VoiceAsyncManualScan.

You must use the Content Moderation endpoint to call the service using the SDK. For more information about API endpoints, see Endpoints.

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 a manual review task for voice

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.VoiceAsyncManualScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Arrays;

public class VoiceAsyncManualScanSample {

    public static void main(String[] args) throws Exception {
       /**
         * The AccessKey of an Alibaba Cloud account has permissions to access all APIs. For security, use a Resource Access Management (RAM) user to call APIs or perform routine O&M. 
         * 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",
                "Obtain the AccessKey ID from an environment variable.",
                "Obtain the AccessKey secret from an environment variable.");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
        IAcsClient client = new DefaultAcsClient(profile);

        VoiceAsyncManualScanRequest voiceAsyncManualScanRequest = new VoiceAsyncManualScanRequest();
        // Specify the API response format.
        voiceAsyncManualScanRequest.setAcceptFormat(FormatType.JSON);
        // Specify the request method.
        voiceAsyncManualScanRequest.setMethod(MethodType.POST);
        voiceAsyncManualScanRequest.setEncoding("utf-8");
        // HTTP and HTTPS are supported.
        voiceAsyncManualScanRequest.setProtocol(ProtocolType.HTTP);

        JSONObject task = new JSONObject();
        task.put("dataId", "Data ID");
        // Set the URL.
        task.put("url", "URL of the audio file to detect");

        JSONObject httpBody = new JSONObject();
        httpBody.put("tasks", Arrays.asList(task));
        // The callback and seed parameters are used for callback notifications. They are optional.
        httpBody.put("callback", "Webhook address");
        httpBody.put("seed", "Random string");

        voiceAsyncManualScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
                "UTF-8", FormatType.JSON);

        /**
         * Set the timeout period. The server-side timeout period for the entire link is 10 seconds. Configure the timeout period accordingly.
         * If the ReadTimeout value that you set is less than the server-side processing time, a ReadTimeout exception is returned.
         */
        voiceAsyncManualScanRequest.setConnectTimeout(3000);
        voiceAsyncManualScanRequest.setReadTimeout(10000);

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

            // The result returned by the server after the server receives and processes the request.
            if (httpResponse != null && httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
                System.out.println(JSON.toJSONString(scrResponse, true));
                int requestCode = scrResponse.getIntValue("code");
                // The detection result of each task.
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // The processing result of a single task.
                        int taskCode = ((JSONObject) taskResult).getIntValue("code");
                        if (200 == taskCode) {
                            // Save the taskId to poll the result.
                            System.out.println(((JSONObject)taskResult).getString("taskId"));
                        } else {
                            // The processing of a single task failed. Analyze the cause based on the specific situation.
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    /**
                     * The entire scan request failed. Analyze the cause based on the specific situation.
                     */
                    System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

Query the result of a manual review task for voice

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageAsyncManualScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.ArrayList;
import java.util.List;

public class VoiceAsyncManualScanResults {

    public static void main(String[] args) throws Exception {
       /**
         * The AccessKey of an Alibaba Cloud account has permissions to access all APIs. For security, use a Resource Access Management (RAM) user to call APIs or perform routine O&M. 
         * 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",
                "Obtain the AccessKey ID from an environment variable.",
                "Obtain the AccessKey secret from an environment variable.");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
        IAcsClient client = new DefaultAcsClient(profile);

        ImageAsyncManualScanResultsRequest imageAsyncManualScanResultsRequest =
                new ImageAsyncManualScanResultsRequest();
        // Specify the API response format.
        imageAsyncManualScanResultsRequest.setAcceptFormat(FormatType.JSON);
        // Specify the request method.
        imageAsyncManualScanResultsRequest.setMethod(MethodType.POST);
        imageAsyncManualScanResultsRequest.setEncoding("utf-8");
        // HTTP and HTTPS are supported.
        imageAsyncManualScanResultsRequest.setProtocol(ProtocolType.HTTP);

        List<String> taskIds = new ArrayList<String>();
        // Add the task IDs to query. You must save the task IDs when you submit the tasks.
        taskIds.add("Manual voice review task ID");
        imageAsyncManualScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * You must set a timeout period.
         */
        imageAsyncManualScanResultsRequest.setConnectTimeout(3000);
        imageAsyncManualScanResultsRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(imageAsyncManualScanResultsRequest);
            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) {
                        if (200 == ((JSONObject) taskResult).getInteger("code")) {
                            String url = ((JSONObject) taskResult).getString("url");
                            String taskId = ((JSONObject) taskResult).getString("taskId");
                            String suggestion = ((JSONObject) taskResult).getString("suggestion");
                            // Process the data based on the suggestion. For example, delete non-compliant data.
                        } else {
                            System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
                        }
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}