這個文檔介紹如何使用異步預測接口,進行模型預測的異步調用,支持更長文本的離線調用。
創建異步調用
參考以下示例代碼,通過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 | 調用失敗 |
文檔內容是否對您有幫助?