0

我想,我似乎遗漏了一些明显的东西。我们已经使用 Google Translation API 有一段时间了,现在我们想“升级”到自定义训练模型而不是默认的 nmt。

我们已经上传了我们的文本,对其进行了训练,现在有了一个模型。在 Google 控制台的预测选项卡中,它运行良好。那么现在怎么办?

这是我们今天使用的代码:

        translate = TranslateOptions
            .newBuilder()
            .setCredentials(ServiceAccountCredentials.fromStream(googleCredentials))
            .build()
            .getService();

                translate.translate(
                    text,
                    TranslateOption.sourceLanguage(fromLng),
                    TranslateOption.targetLanguage(toLng),
                    TranslateOption.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 会产生其他错误,例如:“INFO:无法检测我们是否在 Google Compute Engine 上运行。”

我哪里错了?

4

1 回答 1

0

我们开始吧......对于下一个想要这样做的人:

<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());

}
于 2019-06-27T15:15:03.370 回答