在 JAVA 代码中,而不是在 Google 环境中的 运行 中,如何使用经过训练的翻译模型?

In JAVA code, not running in the Google environment, how does one use a trained translation model?

我想我似乎遗漏了一些明显的东西。我们已经使用 Google 翻译 API 一段时间了,现在我们想要 "upgrade" 到自定义训练模型而不是默认的 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: Failed to detect whether we are running on Google Compute Engine."

我哪里错了?

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

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

}