代码之家  ›  专栏  ›  技术社区  ›  Kai Wähner

Google ML Engine Predict=>gcloud命令可以工作,但是Java API范围问题

  •  2
  • Kai Wähner  · 技术社区  · 8 年前

    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 两者似乎都有效。我可以连接,它列出了我的bucket和部署的模型。

    但我仍然错过了一些设置或配置,这似乎是一个范围问题:

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

    1 回复  |  直到 8 年前
        1
  •  0
  •   VitalyT    7 年前

    试试这个,应该可以:

    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;
        }
    
    推荐文章