pythongoogle-cloud-platformgoogle-cloud-ml-engine

gcloud ml-engine API


Are gcloud ml-engine calls included in the google-cloud client library for python? I currently cannot find any documentation for it (though I see the natural language API). I am trying to replicate the following command in a jupyter notebook via the API and have not had any success:

gcloud ml-engine local predict --json-instances=XXX --model-dir=YYY

UPDATE w/ solution

with open('test.json') as data_file:    
    json_request = json.load(data_file)

response = predict_json(project = PROJECT_ID,
                        model= 'test_model',
                        instances = [json_request],
                        version = 'v1')

Solution

  • I believe what you are looking for can be found in the official documentation under the section "Requesting predictions" (be sure to click on the Python tab).

    For your convenience:

    def predict_json(project, model, instances, version=None):
        """Send json data to a deployed model for prediction.
    
        Args:
            project (str): project where the Cloud ML Engine Model is deployed.
            model (str): model name.
            instances ([Mapping[str: Any]]): Keys should be the names of Tensors
                your deployed model expects as inputs. Values should be datatypes
                convertible to Tensors, or (potentially nested) lists of datatypes
                convertible to tensors.
            version: str, version of the model to target.
        Returns:
            Mapping[str: any]: dictionary of prediction results defined by the
                model.
        """
        # Create the ML Engine service object.
        # To authenticate set the environment variable
        # GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
        service = googleapiclient.discovery.build('ml', 'v1')
        name = 'projects/{}/models/{}'.format(project, model)
    
        if version is not None:
            name += '/versions/{}'.format(version)
    
        response = service.projects().predict(
            name=name,
            body={'instances': instances}
        ).execute()
    
        if 'error' in response:
            raise RuntimeError(response['error'])
    
        return response['predictions']