这个文档介绍如何使用异步预测接口,进行模型预测的异步调用,支持更长文本的离线调用。
创建异步调用
参考以下示例代码,通过content
字段,传入长文本内容,NLP自学习平台会保存长文本,并进行异步模型预测。
说明
content
字段支持的文本长度最大不超过10000字。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));
}
}
另外,可以通过将本地文件内容进行base64编码之后,作为字符串上传NLP自学习平台系统,NLP自学习平台支持解析base64编码的文件内容。并做文本抽取,进行模型预测调用。以下是参考示例代码。
说明 目前支持的文件格式包括:txt、html、pdf、doc、docx。
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));
}
}
调用成功之后,NLP自学习平台返回一个asyncPredictId
字段,用于查询异步预测结果信息。
{
"requestId": "65917545-CDBF-4246-8708-CD03ED4AFDED",
"asyncPredictId": 1669
}
通过使用asyncPredictId
参考下一章节获取异步预测结果。
获取异步调用结果
通过创建GetAsyncPredictRequest
请求,查询异步预测结果。
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));
}
}
说明 异步预测完成时间,根据传入文本长度和文档大小会增加。所以,需要通过
GetAsyncPredictResponse
的status
字段值,进行结果轮询。详细status
枚举值,参考下面内容。status
还没有变成2或者3之前,需要轮询调用GetAsyncPredict
接口查询异步预测结果。status | 说明 |
0 | 初始化 |
1 | 处理中 |
2 | 调用成功 |
3 | 调用失败 |
文档内容是否对您有帮助?