This document describes how to use the hierarchical news classification service.
This service is provided by the NLP Self-Learning Platform. You can use the service by calling its API directly.
Activate the service and purchase a resource plan
Before you use the service, ensure that it is activated. After activation, you must purchase a resource plan.
Activate the service: Activation URL
Purchase a resource plan: Purchase URL
Service invocation and debugging
For more information about how to call the model, see Model invocation.
For software development kit (SDK) examples, see SDK examples.
Debug
Configure access credentials using environment variables
Notes:
The AccessKey of an Alibaba Cloud account has permissions to access all APIs, which poses a high security threat. For improved security, create and use a Resource Access Management (RAM) user for API calls or routine O&M. To create a RAM user, log on to the RAM console.
To prevent security threats from leaked keys, do not hard-code your AccessKey ID and AccessKey secret into your code. This document explains how to store and access them using environment variables.
Configure on Linux and macOS
export NLP_AK_ENV=<access_key_id> export NLP_SK_ENV=<access_key_secret>Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey ID and an AccessKey secret, see Step 2: Obtain an AccessKey pair.
Configure on Windows
Create an environment variable file. Add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables. Then, set their values to your AccessKey ID and AccessKey secret.Restart your Windows operating system.
To call the hierarchical news classification service, set ServiceName to NewsClassification.
Java code example
/**
* The AccessKey of an Alibaba Cloud account has permissions to access all APIs. This poses a high security threat.
* Create and use a RAM user for API calls or routine O&M. To create a RAM user, log on to the RAM console.
* This example shows how to store the AccessKey ID and AccessKey secret in environment variables.
* You can also store them in a configuration file.
* To prevent security threats from leaked keys, do not hard-code your AccessKey ID and AccessKey secret into your code.
*/
String accessKeyId = System.getenv("NLP_AK_ENV");
String accessKeySecret = System.getenv("NLP_SK_ENV");
DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
IAcsClient client = new DefaultAcsClient(defaultProfile);
String content = "[\"On April 18, UTC+8, the Celtics defeated the Pacers 99-91 at home, leading the series 2-0\",\"The Advisors Alliance recently aired in Japan. Local viewers praised the show for its sophisticated production, outstanding characters such as Sima Yi, Zhuge Liang, and Cao Cao, and the acclaimed performances of actors like Wu Xiubo and Yu Hewei\"]";
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("NewsClassification");
request.setPredictContent(content);
RunPreTrainServiceResponse response = client.getAcsResponse(request);
System.out.println(response.getPredictResult());Python code example
# Install dependencies
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-nlp-automl# -*- coding: utf8 -*-
import json
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdknlp_automl.request.v20191111 import RunPreTrainServiceRequest
"""
The AccessKey of an Alibaba Cloud account has permissions to access all APIs. This poses a high security threat.
Create and use a RAM user for API calls or routine O&M. To create a RAM user, log on to the RAM console.
This example shows how to store the AccessKey ID and AccessKey secret in environment variables.
You can also store them in a configuration file.
To prevent security threats from leaked keys, do not hard-code your AccessKey ID and AccessKey secret into your code.
"""
access_key_id = os.environ['NLP_AK_ENV']
access_key_secret = os.environ['NLP_SK_ENV']
# Initialize AcsClient instance
client = AcsClient(
access_key_id,
access_key_secret,
"cn-hangzhou"
);
content = []
content.append('On April 18, UTC+8, the Celtics defeated the Pacers 99-91 at home, leading the series 2-0')
content.append('The Advisors Alliance recently aired in Japan. Local viewers praised the show for its sophisticated production, outstanding characters such as Sima Yi, Zhuge Liang, and Cao Cao, and the acclaimed performances of actors like Wu Xiubo and Yu Hewei')
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('NewsClassification')
request.set_PredictContent(json.dumps(content))
# Print response
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
predict_result = json.loads(resp_obj['PredictResult'])
print(predict_result['result'])PredictContent example
The PredictContent parameter is a JSON string. The following code shows an example. You can pass multiple texts in the request parameter. To do so, the texts must be encapsulated in a JsonArray and then serialized into a JSON string.
[
"On April 18, UTC+8, the Celtics defeated the Pacers 99-91 at home, leading the series 2-0",
"The Advisors Alliance recently aired in Japan. Local viewers praised the show for its sophisticated production, outstanding characters such as Sima Yi, Zhuge Liang, and Cao Cao, and the acclaimed performances of actors like Wu Xiubo and Yu Hewei"
]PredictResult example
The PredictResult is a JSON string. You must deserialize it before parsing. The following example shows its structure. The `result` response parameter is a JSON string that contains a JsonArray.
{
"ret_code": 0,
"message": "",
"time": 100,
"result": [
[
"news_sports",
"news_sports_basketball"
],
[
"news_entertainment",
"news_entertainment_film_tv"
]
]
}