Updated on 2022-12-05 GMT+08:00

Moderation (Text)

This section provides an example of how to use a Java SDK to call Moderation (Text). You can directly call APIs to use SDK functions. The procedure is as follows:

Step 1: Subscribe to a Service. In the left navigation pane, choose Service List or Service Management, and subscribe to Moderation (Text).

Step 2: Configure a Custom Dictionary. Configure allowlist and blocklist dictionaries.

Step 3: Configure the Environment. Obtain the SDK and sample project and import them to the development environment.

Step 4: Perform Authentication. Use AK/SK for authentication.

Step 5: Call a Service API. Call the service API. You can view the status code and error code at any time during the usage.

Step 1: Subscribe to a Service

Subscribe to Moderation (Text) by following the instructions in Subscribing to the Service.

Step 2: Configure a Custom Dictionary

You can configure allowlist and blocklist dictionaries for text moderation.

  1. If an allowlist dictionary is customized, the words in the allowlist dictionary will not be detected when you use text moderation.

The procedure is as follows:

  1. Log in to the HUAWEI CLOUD and click Console in the upper right corner. The Console page is displayed.
  2. Click Service List and choose AI > Content Moderation. The Content Moderation management console is displayed.
  3. In the left navigation pane, choose Service List > Text Moderation. Click Configuration next to Custom Allowlist Dictionary. The Create Dictionary dialog box is displayed.

    Figure 1 Create Dictionary

  4. Enter a dictionary name and the keywords in the allowlist dictionary. Click OK.

    • Dictionary Name: A dictionary name contains a maximum of 40 characters. Only letters, digits, hyphens (-), and underscores (_) are allowed.
    • Add Keyword: A dictionary contains a maximum of 5000 keywords. Each keyword consists of a maximum of 40 characters and ends with a carriage return.

2. If a blocklist dictionary is customized, the words in the blocklist dictionary will be detected when you use Text Moderation.

The procedure is as follows:

  1. Log in to the HUAWEI CLOUD and click Console in the upper right corner. The Console page is displayed.
  2. Click Service List and choose AI > Content Moderation. The Content Moderation management console is displayed.
  3. In the left navigation pane, choose Service List > Text Moderation. Click Configuration next to Custom Blocklist Dictionary. The Custom Dictionary page is displayed.
  4. Click Create Dictionary in the upper left corner. The Create Dictionary dialog box is displayed. Enter a dictionary name and the keywords in the blocklist dictionary.

    • Dictionary Name: A dictionary name contains a maximum of 40 characters. Only letters, digits, hyphens (-), and underscores (_) are allowed.
    • Add Keyword: A dictionary contains a maximum of 5000 keywords. Each keyword consists of a maximum of 40 characters and ends with a carriage return.

  5. Click OK.

    • The dictionary name cannot be modified.

Step 3: Configure the Environment

You can compile code to call the API of Moderation (Text) based on the SDK. You need to configure the environment when using the SDK and calling APIs. The procedure is as follows:

  1. To obtain the SDK and related document of Content Moderation, click here.
  2. For details about how to configure the environment, see Preparing a Java Development Environment.
  3. Import an SDK project on Eclipse.

    1. Open Eclipse and configure the correct JRE path in Windows > Preferences > Java > Installed JREs.
    2. In the navigation pane on the left, choose General > Workspace. In the Text file encoding area, select Other, set the parameter value to UTF-8, click Apply, and click OK.
    3. On the Eclipse menu bar, choose File > Import. In the dialog box that is displayed, choose Maven > Existing Maven Projects, click Next, click Browse, and select the local path where ais-moderation-java-sdk resides.
    4. Click Finish to import the SDK. After the SDK is imported, open the project. Figure 2 shows the project directory.
      Figure 2 Project directory

Step 4: Perform Authentication

Content Moderation supports token-based and AK/SK-based authentication. This section uses AK/SK-based authentication as an example.

  1. Obtain an AK/SK.

    The AK/SK is the access key. To obtain the AK/SK, log in to the My Credentials page, choose Access Keys in the left navigation pane, and click Create Access Key in the right pane.

    Figure 3 Creating an access key

  2. Configure the AK/SK in the Java SDK for AK/SK authentication.

    Change the values of AK and SK of the function in the ModerationTextContentDemo.java file of the project to the obtained AK/SK. The sample code is as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 1. Configure the basic information for accessing Text Moderation and generate a client connection object.
    AisAccess service = ServiceAccessBuilder.builder()		
            .ak("######")                       // your ak
            .sk("######")                       // your sk
           .region("ap-southeast-1")              // Content Moderation in CN-Hong Kong (ap-southeast-1) and AP-Singapore (ap-southeast-3)
            .connectionTimeout(5000)            // Timeout limit for connecting to the target URL
            .connectionRequestTimeout(1000)     // Timeout limit for obtaining available connections from the connection pool
            .socketTimeout(20000)               // Timeout limit for obtaining server response data
            .build();
    

Step 5: Call a Service API

  1. Enter the text that you want to check in the ModerationTextContentDemo.java file, for example, 6666666666.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    //
    // 2. Construct the parameters required for accessing Text Moderation.
    //
    String uri = "/v1.0/moderation/text";
    			
    JSONObject json = new JSONObject();
    json.put("categories", new String[] {"porn"}) //Text for moderation
    
    JSONObject text = new JSONObject();
    text.put("text", "6666666666");
    text.put("type", "content");
    			
    JSONArray items = new JSONArray();
    items.add(text);			
    json.put("items", items);			
    
    StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");
    
    // 3. Input the URI and required parameters of Text Moderation.
    // Input the parameters in JSON objects and call the service using POST.
    HttpResponse response = service.post(uri, stringEntity);
    
    // 4. Check whether the API call is successful. If 200 is returned, the API call succeeds. Otherwise, it fails.
    ResponseProcessUtils.processResponseStatus(response);
    

  2. Execute the ModerationTextContentDemo.java file. If 200 is displayed on the console, the program is successfully executed. The text moderation result is displayed on the console. See Figure 4.

    Figure 4 Execution result

  3. View the number of API calls. Choose Service List > Text Moderation, and view call statistics and call details. See Figure 5.

    Figure 5 Call statistics
    • The Call Statistics section provides three statistical charts, displaying daily calls, daily blocked text, and daily reviewed text. You can set a time range to view the change of the number of API calls within the time range.
    • The Call Details section lists the total number of calls, blocks, reviews, and passes every day. This helps you better understand service calls and their statuses.
      • block indicates that sensitive information is contained and the information is blocked.
      • pass indicates that sensitive information is not contained and the information is approved.
      • review indicates that manual review is required.

  4. View monitoring metrics. You can click View Metric to view historical data such as successful and failed calls on the Cloud Eye console. See Figure 6.

    Figure 6 Viewing metrics