The product title category prediction service predicts categories for product titles in E-commerce scenarios. The service uses the same category system as Taobao.
This service is provided by the NLP Self-learning Platform. You can call the API directly to use this service.
Service activation and resource plan purchase
Before you use the service, ensure that it is activated. After activation, you can purchase a resource plan.
Activate the service: Activation page
Purchase a resource plan: Purchase page
Service invocation and debugging
For information about how to invoke the model, see Model invocation.
For information about software development kit (SDK) examples, see SDK examples.
Debug
Configure access credentials using environment variables
Notes:
The AccessKey pair of an Alibaba Cloud account has permissions to access all APIs. This poses a high security threat. We strongly recommend that you create and use a Resource Access Management (RAM) user to make API calls or perform routine O&M. To create a RAM user, log on to the RAM console.
To prevent security threats, do not hard-code your AccessKey ID and AccessKey secret in your code. We recommend that you store and access your AccessKey pair by configuring 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 and set their values to your AccessKey ID and AccessKey secret.Restart Windows.
Java code example
/**
* An AccessKey pair of an Alibaba Cloud account has all API access permissions. This poses a high security threat.
* We strongly recommend that you create and use a RAM user to make API calls or perform routine O&M. To create a RAM user, log on to the RAM console.
* This example shows how to store an AccessKey ID and an AccessKey secret in environment variables. You can also store them in a configuration file as needed.
* To prevent security threats, 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);
Map<String, Object> map = new HashMap<>();
map.put("input", "wallpaper");
map.put("topk", 1);
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("NLP-E-Commerce-Category");
request.setPredictContent(JSON.toJSONString(map));
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: utf-8 -*-
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
# An AccessKey pair of an Alibaba Cloud account has all API access permissions. This poses a high security threat.
# We strongly recommend that you create and use a RAM user to make API calls or perform routine O&M. To create a RAM user, log on to the RAM console.
# This example shows how to store an AccessKey ID and an AccessKey secret in environment variables. You can also store them in a configuration file as needed.
# To prevent security threats, 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 an AcsClient instance
client = AcsClient(
access_key_id,
access_key_secret,
"cn-hangzhou"
);
content ={"input": "wallpaper","topk":1}
# Initialize a request and set its parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('NLP-E-Commerce-Category')
request.set_PredictContent(json.dumps(content))
# Print the 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
{
"input": "wallpaper",
"topk":1
}PredictResult example
{
"label": [
{
"score": 0.8,
"key": "Home Improvement Main Materials-Wallpaper-Paper Wallpaper",
"id":"27-50013322-50024689"
}
]
}Request parameters
Parameter | Description |
input | The product title. |
topk | Required. The number of predicted categories to return. The categories are sorted by probability in descending order. |
Response parameters
Parameter | Description |
key | The detected category. This is a hierarchical structure: Level-1 category[-Level-2 category-Level-3 category-Level-4 category-Leaf category]. It contains at least one level and at most five levels. |
id | The ID that corresponds to the key. |
score | The probability of the predicted category. The value is a number in the range of (0, 1]. |