The Emotion Recognition service detects the emotions of customers or customer service agents. It is suitable for scenarios such as telemarketing and online customer support.
This service is provided by the NLP Self-learning Platform. You can use this service by calling the API directly.
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 more information about model invocation, see Model invocation.
For software development kit (SDK) examples, see SDK examples.
Debug
Configure AccessKey credentials using environment variables
Notes:
The AccessKey of an Alibaba Cloud account provides full access permissions to all APIs. This creates a high security risk. We strongly recommend that you create and use a Resource Access Management (RAM) user for API calls or daily operations and maintenance (O&M). To create a RAM user, log on to the RAM console.
Do not hard-code your AccessKey ID and AccessKey secret into your code. This can lead to security risks. Instead, configure environment variables to store and access your credentials.
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 AccessKey secret, see Step 2: Obtain an AccessKey for your account.
Configure on Windows
Create an environment variable file. Add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables, and then set their values to your AccessKey ID and AccessKey secret.Restart your Windows system.
Java code example
/**
* An AccessKey of an Alibaba Cloud account has full access permissions to all APIs, which poses a high security threat.
* We strongly recommend that you create and use a RAM user for API calls or daily 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.
* Do not hard-code your AccessKey ID and AccessKey secret into your code to avoid security threats.
*/
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 = "{\"input\": {\"content\":\"The graphic captcha is always incorrect\" } }";
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("DeepEmotionBert");
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/**
The AccessKey pair of an Alibaba Cloud account has permissions for all API operations, which creates a high security risk. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
In this example, the AccessKey ID and AccessKey secret are stored in an environment variable. You can also store them in a configuration file.
We strongly recommend that you do not store the AccessKey ID and AccessKey secret in your code because this poses a risk of credential leakage.
*//*** The AccessKey of an Alibaba Cloud account has permissions for all API operations, which creates a high security risk. We recommend that you use a RAM user to call API operations or perform routine O&M. You can log on to the RAM console to create a RAM user.
* This topic provides an example of how to store an AccessKey ID and AccessKey secret in an environment variable. You can also store them in a configuration file.
* We strongly recommend that you do not store your AccessKey ID and AccessKey secret in your code because this poses a risk of credential leakage.
# -*- 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 of an Alibaba Cloud account has full access permissions to all APIs, which poses a high security threat.
We strongly recommend that you create and use a RAM user for API calls or daily 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.
Do not hard-code your AccessKey ID and AccessKey secret into your code to avoid security threats.
"""
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 ={"input":{"content":"The graphic captcha is always incorrect"}}
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('DeepEmotionBert')
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
{
"input": {
"content": "The graphic captcha is always incorrect"
}
}PredictResult example
{
"output": {
"sentiment": [
{
"score": 0.7712000012397766,
"key": "Dissatisfaction"
},
{
"score": 0.014100000262260437,
"key": "Anger"
},
{
"score": 0.014000000432133675,
"key": "Disgust"
},
{
"score": 0.005499999970197678,
"key": "Sadness"
},
{
"score": 0.004100000020116568,
"key": "Complaint"
},
{
"score": 0.0008999999845400453,
"key": "Surprise"
},
{
"score": 0.0007999999797903001,
"key": "Fear"
},
{
"score": 0.00039999998989515007,
"key": "Liking"
},
{
"score": 0.00019999999494757503,
"key": "Happiness"
},
{
"score": 0.00009999999747378752,
"key": "Approval"
},
{
"score": 0.0,
"key": "Gratitude"
}
],
"content": "The graphic captcha is always incorrect"
},
"code": 0,
"cost": 2.271,
"message": "SUCCESS"
}Service description
The service name (ServiceName) supports two versions: a high-performance version and a high-precision version. You can select a version based on your requirements.
Service name (ServiceName) | Description |
DeepEmotion | High-performance version. Faster speed, slightly lower precision. |
DeepEmotionBert | High-precision version. Higher precision, slightly slower speed. |
Request parameters
Parameter | Description |
content | The text sample to detect. |
role | Optional. The role of the speaker. Valid values are `agent` and `customer`. |
Response parameters
The service can detect multiple emotions in a single piece of text.
Parameter | Description |
code | Status code. `0` indicates success. `1002` indicates a parameter error. Check whether the input is in valid JSON format. `10016` indicates that the request exceeds the maximum length of 1,000 characters. Reduce the size of the request. `10015` indicates that a required parameter is missing. |
content | From the request parameters. The text sample that was detected. The maximum length is 1,000 characters. |
sentiment | A list of detected emotions. The list includes 8 general emotions (Happiness, Liking, Sadness, Anger, Disgust, Surprise, Fear, and Approval) and 3 common business-related emotions (Gratitude, Dissatisfaction, and Complaint). |
key | The name of the emotion. |
score | The probability of the emotion. The value is between 0 and 1. A score greater than 0.5 indicates a hit for that emotion type. |