Examples of using the asynchronous prediction API
This document provides examples of how to use the asynchronous prediction API to make asynchronous calls for model predictions. This API supports long text.
Create an asynchronous call
To create an asynchronous call, pass your long text to the content field. The NLP Self-learning Platform saves the text and performs an asynchronous model prediction.
content field supports a maximum of 10,000 characters.package com.alibaba.nlp;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.nlp_automl.model.v20191111.CreateAsyncPredictRequest;
import com.aliyuncs.nlp_automl.model.v20191111.CreateAsyncPredictResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
public class NlpAutomlAsync {
public static void main(String[] args) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(
"cn-hangzhou",
"<your-access-key-id>",
"<your-access-key-secret>");
IAcsClient client = new DefaultAcsClient(profile);
CreateAsyncPredictRequest request = new CreateAsyncPredictRequest();
request.setModelId(1818);
request.setContent(longContent);
CreateAsyncPredictResponse response = client.getAcsResponse(request);
System.out.println("" + new Gson().toJson(response));
}
}
You can also upload a local file. To do this, first Base64-encode the file content into a string. Then, upload the string to the NLP Self-learning Platform. The platform parses the Base64-encoded content, extracts the text, and makes a model prediction call.
package com.alibaba.nlp;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.nlp_automl.model.v20191111.CreateAsyncPredictRequest;
import com.aliyuncs.nlp_automl.model.v20191111.CreateAsyncPredictResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
public class NlpAutomlAsyncDaily {
public static void main(String[] args) throws ClientException, IOException {
DefaultProfile profile = DefaultProfile.getProfile(
"cn-hangzhou",
"<your-access-key-id>",
"<your-access-key-secret>");
IAcsClient client = new DefaultAcsClient(profile);
InputStream in = new FileInputStream("<local-file-dir>");
byte[] data = new byte[in.available()];
int readSize = in.read(data);
System.out.println("read data size=" + readSize);
in.close();
String base64String = Base64.getEncoder().encodeToString(data);
System.out.println("base64 string length=" + base64String.length());
CreateAsyncPredictRequest request = new CreateAsyncPredictRequest();
request.setModelId(2269);
request.setFileType("<file-type>");
request.setFileContent(base64String);
CreateAsyncPredictResponse response = client.getAcsResponse(request);
System.out.println("" + new Gson().toJson(response));
}
}After a successful call, the NLP Self-learning Platform returns an asyncPredictId. Use this ID to query the result of the asynchronous prediction.
{
"requestId": "65917545-CDBF-4246-8708-CD03ED4AFDED",
"asyncPredictId": 1669
}Use the asyncPredictId to retrieve the prediction result as described in the next section.
Get the asynchronous call result
Create a GetAsyncPredictRequest to query the asynchronous prediction result.
package com.alibaba.nlp;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.nlp_automl.model.v20191111.GetAsyncPredictRequest;
import com.aliyuncs.nlp_automl.model.v20191111.GetAsyncPredictResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
public class NlpAutomlAsync2 {
public static void main(String[] args) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(
"cn-hangzhou",
"<your-access-key-id>",
"<your-access-key-secret>");
IAcsClient client = new DefaultAcsClient(profile);
GetAsyncPredictRequest request = new GetAsyncPredictRequest();
request.setAsyncPredictId(1669);
GetAsyncPredictResponse response = client.getAcsResponse(request);
System.out.println("" + new Gson().toJson(response));
}
}
status field in the GetAsyncPredictResponse. For a description of the status values, see the following table.status is not 2 or 3, you must continue to poll the GetAsyncPredict API to retrieve the result.| status | Description |
| 0 | Initialization |
| 1 | Processing |
| 2 | Call successful |
| 3 | Call failed |