我想,我似乎遗漏了一些明显的东西。我们使用谷歌翻译API已经有一段时间了,现在我们想“升级”到一个自定义的训练模型,而不是默认的nmt。
我们已经上传了文本,对其进行了训练,现在有了一个模型。在Google控制台的predict选项卡中,它工作得很好。那么,现在怎么办?
这是我们今天使用的代码:
translate = TranslateOptions
.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(googleCredentials))
.build()
.getService();
translate.translate(
text,
TranslateOption.sourceLanguage(fromLng),
TranslateOption.targetLanguage(toLng),
TranslateOption.model(model));
其中model是"nmt“(或"base")...我应该只放入训练结束时创建的新训练的模型代码吗?当我尝试时,它返回了一个400错误和一条消息:
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Value",
"reason" : "invalid"
} ],
"message" : "Invalid Value"
尝试此处记录的不同代码:https://cloud.google.com/translate/docs/quickstart-client-libraries-v3会产生其他错误,如:“信息:无法检测我们是否在Google Compute Engine上运行。”
我哪里错了?
发布于 2019-06-27 23:15:03
开始吧..。对于下一个想要这样做的人:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-automl</artifactId>
<version>0.97.0-beta</version>
</dependency>
代码:
private PredictionServiceClient predictionClient;
private ModelName modelName;
public GoogleTranslationServiceTrained(final byte[] googleCredentials) throws IOException {
super();
PredictionServiceSettings settings = PredictionServiceSettings
.newBuilder()
.setCredentialsProvider(new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return ServiceAccountCredentials.fromStream(new ByteArrayInputStream(googleCredentials));
}
}).build();
// Instantiate client for prediction service.
predictionClient = PredictionServiceClient.create(settings);
// Get the full path of the model.
modelName = ModelName.of("xxxx", "us-central1", "yyy");
}
public String getRemoteTranslate(String text) {
TextSnippet textSnippet = TextSnippet.newBuilder().setContent(text).build();
// Set the payload by giving the content of the file.
ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
// Additional parameters that can be provided for prediction
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionClient.predict(modelName, payload, params);
TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent();
return StringEscapeUtils.unescapeHtml4(translatedContent.getContent());
}
https://stackoverflow.com/questions/56588235
复制相似问题