来自 IntelliJ Idea 的 Google AutoML 预测返回 NoSuchMethodError

问题描述

我已经成功地在 AutoML 中训练了我的单标签文本分类模型。在 GCP 控制台中,我可以毫无问题地对模型进行预测。但是当我尝试从 IntelliJ 进行预测时,我收到了这个错误

Exception in thread "main" java.lang.NoSuchMethodError: com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom([Ljava/lang/String;[Lcom/google/protobuf/Descriptors$FileDescriptor;)Lcom/google/protobuf/Descriptors$FileDescriptor;
    at com.google.api.HttpProto.<clinit>(HttpProto.java:67)
    at com.google.api.Annotationsproto.<clinit>(Annotationsproto.java:69)
    at com.google.cloud.automl.v1beta1.PredictionServiceProto.<clinit>(PredictionServiceProto.java:132)
    at com.google.cloud.automl.v1beta1.PredictRequest$ParamsDefaultEntryHolder.<clinit>(PredictRequest.java:268)
    at com.google.cloud.automl.v1beta1.PredictRequest$Builder.internalGetParams(PredictRequest.java:1148)
    at com.google.cloud.automl.v1beta1.PredictRequest$Builder.buildPartial(PredictRequest.java:725)
    at com.google.cloud.automl.v1beta1.PredictRequest$Builder.build(PredictRequest.java:707)
    at com.newsProcessor.text.GoogleTextAnalyser.predict(GoogleTextAnalyser.java:37)

我创建了服务帐户,创建了 JSON 密钥,下载了密钥,创建了指向下载密钥位置的 GOOGLE_APPLICATION_CREDENTIALS。在我的 Java 代码中,我设置了正确的 projectId 和 modelId。

这是我的代码

public static void main(String[] args) throws IOException {
        String projectId = "MY-PROJECT-ID";
        String modelId = "MY-MODEL-ID";
        String content = "The Gulf states accuse Iran of supplying arms to the Houthis a charge Tehran and the group deny";
        predict(projectId,modelId,content);
    }

    static void predict(String projectId,String modelId,String content) throws IOException {
        try (PredictionServiceClient client = PredictionServiceClient.create()) {
            ModelName name = ModelName.of(projectId,"us-central1",modelId);
            TextSnippet textSnippet =
                    TextSnippet.newBuilder()
                            .setContent(content)
                            .setMimeType("text/plain")
                            .build();
            ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
            PredictRequest predictRequest =
                    PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();

            PredictResponse response = client.predict(predictRequest);

            for (AnnotationPayload annotationPayload : response.getPayloadList()) {
                System.out.format("Predicted class name: %s\n",annotationPayload.getdisplayName());
                System.out.format(
                        "Predicted sentiment score: %.2f\n\n",annotationPayload.getClassification().getscore());
            }
        }
    } 

请问您知道什么是错误的吗?因为错误本身对我来说没有多大意义。

我的 Maven 依赖项是:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>libraries-bom</artifactId>
                <version>19.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-language</artifactId>
        <version>1.52.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-automl</artifactId>
        <version>0.55.1-beta</version>
    </dependency>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-storage</artifactId>
    </dependency>    

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)