Text manual review
This topic describes how to use the Java software development kit (SDK) to call the text manual review API.
Feature description
If the results of machine-assisted moderation do not meet your expectations, call the text manual review API. For more information about the parameters, see the Text manual review API documentation.
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.
NoteYou 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 text manual review task
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.TextAsyncManualScanRequest;
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 TextAsyncManualScanSample {
public static void main(String[] args) throws Exception {
/**
* An Alibaba Cloud account AccessKey has full permissions on all APIs. For better security, use a Resource Access Management (RAM) user to call APIs or perform O&M.
* Common methods 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 of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
// Note: To improve moderation performance, reuse the instantiated client. This avoids repeated connections.
IAcsClient client = new DefaultAcsClient(profile);
TextAsyncManualScanRequest textAsyncManualScanRequest = new TextAsyncManualScanRequest();
// Specify the API response format.
textAsyncManualScanRequest.setAcceptFormat(FormatType.JSON);
// Specify the request method.
textAsyncManualScanRequest.setMethod(MethodType.POST);
textAsyncManualScanRequest.setEncoding("utf-8");
// HTTP and HTTPS are supported.
textAsyncManualScanRequest.setProtocol(ProtocolType.HTTP);
JSONObject task = new JSONObject();
task.put("dataId", "Data ID");
task.put("content", "Text content");
JSONObject httpBody = new JSONObject();
httpBody.put("tasks", Arrays.asList(task));
// The callback and seed parameters are optional and used for callback notifications.
httpBody.put("callback", "Webhook address");
httpBody.put("seed", "Random string");
textAsyncManualScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
"UTF-8", FormatType.JSON);
/**
* Set a timeout period. The server-side timeout period for the entire link is 10 seconds. Set the timeout period accordingly.
* If the ReadTimeout value is less than the server-side processing time, a ReadTimeout exception is returned.
*/
textAsyncManualScanRequest.setConnectTimeout(3000);
textAsyncManualScanRequest.setReadTimeout(10000);
try {
HttpResponse httpResponse = client.doAction(textAsyncManualScanRequest);
// The response 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 moderation 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 task failed. Analyze the cause of the failure based on the specific situation.
System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
}
}
} else {
/**
* This indicates that the entire request failed. Analyze the cause of the failure 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 text manual review task
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.TextAsyncManualScanResultsRequest;
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 TextAsyncManualScanResults {
public static void main(String[] args) throws Exception {
/**
* An Alibaba Cloud account AccessKey has full permissions on all APIs. For better security, use a Resource Access Management (RAM) user to call APIs or perform O&M.
* Common methods 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 of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
// Note: To improve moderation performance, reuse the instantiated client. This avoids repeated connections.
IAcsClient client = new DefaultAcsClient(profile);
TextAsyncManualScanResultsRequest textAsyncManualScanResultsRequest =
new TextAsyncManualScanResultsRequest();
// Specify the API response format.
textAsyncManualScanResultsRequest.setAcceptFormat(FormatType.JSON);
// Specify the request method.
textAsyncManualScanResultsRequest.setMethod(MethodType.POST);
textAsyncManualScanResultsRequest.setEncoding("utf-8");
// HTTP and HTTPS are supported.
textAsyncManualScanResultsRequest.setProtocol(ProtocolType.HTTP);
List<String> taskIds = new ArrayList<String>();
// Add the taskId that you want to query. Save the taskId when you submit the task.
taskIds.add("Text moderation task ID");
textAsyncManualScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* Set a timeout period.
*/
textAsyncManualScanResultsRequest.setConnectTimeout(3000);
textAsyncManualScanResultsRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(textAsyncManualScanResultsRequest);
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 taskId = ((JSONObject) taskResult).getString("taskId");
String suggestion = ((JSONObject) taskResult).getString("suggestion");
// Process the task based on the suggestion. For example, delete data that contains violations.
} 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();
}
}
}