0

I have a working uploaded ML-model on Goggle Cloud platform (Tested via python and gcloud ml-engine predict).

I am currently trying to get predictions from Android using this library: Client Library for Java with this javadoc. I use a service account for access and Android code in a AsyncTask that looks like this:

 JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            HttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
            GoogleCredential credential = GoogleCredential.fromStream(is, httpTransport, jsonFactory);
            CloudMachineLearningEngine ml = new CloudMachineLearningEngine.Builder(httpTransport,jsonFactory,credential)
                    .setApplicationName("myCloudApplication")
                    .build();
            Log.i(TAG,"Successfully set up !!");

is is the InputStream to the json file containing my Service Account Key. I have tried many things getting from here to make predictions against my trained ML-model. I can't find any online examples. Is this even possible?

All help is deeply appreciated.

Lars H
  • 11
  • 3
  • Do you mind specifying the errors you are encountering. That would help determine if the problem are credentials or something else. – rhaertel80 Dec 17 '17 at 06:57
  • I have no errors i strict meaning I just don't understand how to use the ml object in the code once I got hold of it. – Lars H Dec 18 '17 at 13:27

1 Answers1

0

This is definitely supported. From this sample:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.UriTemplate;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.discovery.Discovery;
import com.google.api.services.discovery.model.JsonSchema;
import com.google.api.services.discovery.model.RestDescription;
import com.google.api.services.discovery.model.RestMethod;
import java.io.File;

/*
 * Sample code for doing Cloud Machine Learning Engine online prediction in Java.
 */
public class OnlinePredictionSample {

  public static void main(String[] args) 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 = "YOUR_PROJECT_ID";
    // You should have already deployed a model and a version.
    // For reference, see https://cloud.google.com/ml-engine/docs/how-tos/deploying-models.
    String modelId = "YOUR_MODEL_ID";
    String versionId = "YOUR_VERSION_ID";
    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("input.txt");
    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);
  }
}
rhaertel80
  • 8,254
  • 1
  • 31
  • 47
  • Thanks but that solution doesn't use the com.google.api.services.ml.v1.CloudMachineLearningEngine API. – Lars H Dec 16 '17 at 23:52
  • Is that a strict requirement? Does the above code not work in Android? The above is currently the only publicly available Java sample we have. There tend to be problems with other clients because we are using some advanced features in the API. – rhaertel80 Dec 17 '17 at 06:55
  • 1
    No that is not a strict requirement, but I was interested in use the existing API since I thought it would be more convenient than the general approach you suggested. The solution you refer to doesn't use a _Service Account_ either this implies as I understand it that you can't use the GoogleNetHttpTransport.newTrustedTransport(); nor the GoogleNetHttpTransport.newTrustedTransport(); methods. – Lars H Dec 18 '17 at 10:13
  • I mean "nor the GoogleCredential.getApplicationDefault(); methods". – Lars H Dec 18 '17 at 10:27