Text Moderation

  • Sample code for a Python 3 request
    # encoding:utf-8
    import requests
    import base64
    
    url = "https://{endpoint}/v1.0/moderation/image"
    token = "Actual token value obtained by the user"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    data= {
            "categories": ['ad', 'politics', 'porn', 'abuse', 'contraband', 'flood'],
            "items": [
                {"text": "666666 nude chat+call 999account123456,fuck666666666666666", "type": "content"}
            ]
        }
    
    response = requests.post(url, headers=headers, json=data, verify=False)
    print(response.content.decode('unicode_escape'))
  • Sample code for a Java request
    package com.huawei.ais.demo;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.huawei.ais.sdk.util.HttpClientUtils;
    
    import java.io.IOException;
    import java.net.URISyntaxException;
    
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
    import org.apache.http.entity.StringEntity;
    import com.alibaba.fastjson.JSONObject;
    
    import org.apache.http.entity.ContentType;
    import org.apache.http.message.BasicHeader;
    
    /**
     * This demo is used only for tests. You are advised to use the SDK.
     * Before using this demo, configure the dependent JAR package. Obtain this package by downloading the SDK.
     */
    
    public class TextModerationDemo {
    	public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException{
    		TokenDemo();
    	}
    
    	public static void TokenDemo() throws URISyntaxException, UnsupportedOperationException, IOException {
    
    		String url = "https://{endpoint}/v1.0/moderation/text";
    		String token = "Actual token value obtained by the user";
    
    		JSONObject params = new JSONObject();
    		try {
    			params.put("categories", new String[] {"porn","politics", "ad", "abuse", "contraband", "flood"});
    
    			JSONObject text = new JSONObject();
    			text.put("text", "666666 nude chat+call 999account123456,fuck666666666666666");
    			text.put("type", "content");
    
    			JSONArray items = new JSONArray();
    			items.add(text);
    
    			params.put("items", items);
    			Header[] headers = new Header[]{new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())};
    			StringEntity stringEntity = new StringEntity(params.toJSONString(), "utf-8");
    			HttpResponse response = HttpClientUtils.post(url, headers, stringEntity);
    			JSONObject jsonObject = JSON.parseObject(HttpClientUtils.convertStreamToString(response.getEntity().getContent()));
    			System.out.println(JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
  • Sample code for a PHP request
    <?php
    
    function TokenRequest() {
        $url = "https://{endpoint}/v1.0/moderation/text";
         $token = "Actual token value obtained by the user";
    
    	$items = array(
    		array(
    			"text" => "666666 nude chat+call 999account123456,fuck666666666666666",
    			"type" => "content"
    		)
    	);
    	$categories = array("ad", "abuse", "politics", "porn", "contraband");
        $data = array(
            "categories" => $categories,
            "items" => $items,
    
        );	
        $curl = curl_init();
        $headers = array(
            "Content-Type:application/json",
            "X-Auth-Token:" . $token
        );
    
        /* Setting the request body */
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_NOBODY, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    
        $response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        echo $response;
    }
    TokenRequest();
    Table 1 Parameter description

    Parameter

    Description

    url

    API request URL, for example, https://{endpoint}/v1.0/moderation/image.

    token

    A token is a user's access credential, which includes user identities and permissions. When you call an API to access a cloud service, a token is required for identity authentication.

    For details about how to obtain the token, see Authentication.