Google ML Engine Predict = ›команда gcloud работает, но проблема с областью действия Java API

Я использую Google ML Engine и уже развернул модель, используя этот учебник: https://cloud.google.com/ml-engine/docs/tensorflow/getting-started-training-prediction

Прогноз с помощью gcloud CLI работает.

В качестве следующего шага мне нужно сделать прогноз из Java API (локальное развертывание, а не в GCP). Я использую этот пример: https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/mlengine/online-prediction.

Кроме того, я обнаружил, что сначала мне нужно выполнить авторизацию, поэтому я попробовал неявное и явное подключение, как описано здесь: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/auth/src/main/java/com/google/cloud/auth/samples/AuthExample.java Оба, похоже, работают. Я могу подключиться, и в нем перечислены мои корзины и развернутая модель.

Но я все еще пропускаю некоторые настройки или настройки, похоже, проблема с областью действия:

Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_scope",
  "error_description" : "Empty or missing scope not allowed."
}
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:394)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
    at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
    at com.github.megachucky.kafka.streams.machinelearning.Kafka_Streams_TensorFlow_Serving_Google_ML_Engine_Example.main(Kafka_Streams_TensorFlow_Serving_Google_ML_Engine_Example.java:97)

Вот мой код:

//  static void authExplicit(String jsonPath) throws IOException {
//        // You can specify a credential file by providing a path to GoogleCredentials.
//        // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
//        credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
//              .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
//        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
//
//        System.out.println("Buckets:");
//        Page<Bucket> buckets = storage.list();
//        for (Bucket bucket : buckets.iterateAll()) {
//          System.out.println(bucket.toString());
//        }
//      }


    static void authImplicit() {
          // If you don't specify credentials when constructing the client, the client library will
          // look for credentials via the environment variable GOOGLE_APPLICATION_CREDENTIALS.
          Storage storage = StorageOptions.getDefaultInstance().getService();

          System.out.println("Buckets:");
          Page<Bucket> buckets = storage.list();
          for (Bucket bucket : buckets.iterateAll()) {
            System.out.println(bucket.toString());
          }
        }

    public static void main(String[] args) throws Exception {
//      authExplicit("/Users/kai.waehner/Google Drive/Confluent_Kai/kai-waehner-project-8aad9356ffa2.json");
        authImplicit();


        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();

        RestDescription api = discovery.apis().getRest("ml", "v1").execute();
        RestMethod method = api.getResources().get("projects").getMethods().get("predict");

        JsonSchema param = new JsonSchema();
        String projectId = "kai-waehner-project-mlengine";
        // You should have already deployed a model and a version.
        // For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
        String modelId = "census";
        String versionId = "v1";
        param.set(
            "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

        GenericUrl url =
            new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
        System.out.println(url);

        String contentType = "application/json";
        File requestBodyFile = new File("src/main/resources/generatedModels/TensorFlow_Census/test.json");
        HttpContent content = new FileContent(contentType, requestBodyFile);
        System.out.println(content.getLength());

        GoogleCredential credential = GoogleCredential.getApplicationDefault();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
        HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);

        String response = request.execute().parseAsString();
        System.out.println(response);
      }

Любая помощь приветствуется!


person Kai Wähner    schedule 27.06.2018    source источник
comment
Вы выполнили команду gcloud auth application-default login и имели ли роли / ml.admin для своей учетной записи службы?   -  person Héctor Neri    schedule 27.10.2018


Ответы (1)


Попробуйте это, должно сработать:

public String predict() throws Exception {

        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();
        RestDescription api = discovery.apis().getRest("ml", "v1").execute();
        RestMethod method = api.getResources().get("projects").getMethods().get("predict");

        JsonSchema param = new JsonSchema();
        String projectId = "projectId";
        // You should have already deployed a model and a version.
        // For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
        String modelId = "modelId";
        String versionId = "versionId";
        param.set(
                "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

        GenericUrl url =
                new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
        System.out.println(url);
        try {
            String contentType = "application/json";
            File requestBodyFile = new File(OnlinePredictionSample.class.getClassLoader().getResource("input.txt").getFile());
            HttpContent content = new FileContent(contentType, requestBodyFile);
            System.out.println(content.getLength());



            GoogleCredential credential = GoogleCredential.fromStream(OnlinePredictionSample.class.getClassLoader().
                    getResourceAsStream("client_secret.json"))
                    .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));


            HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
            HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);

            String response = new Gson().toJson(request.execute().parseAsString());
            log.info(response);
            return response;
        } catch (Exception e) {
            log.log(Level.SEVERE, "Cannot Make Rest to ML Google Platform ", e);
        }
        return null;
    }
person VitalyT    schedule 30.06.2019