How to publish an exported Google AutoML machine as an API?

319 views Asked by At

I have built a machine learning model using Google's AutoML Tables interface. Once the model was trained, I exported it in a docker container to my local machine by following the steps detailed on Google's official documentation page: https://cloud.google.com/automl-tables/docs/model-export. Now, on my machine, it exists inside a Docker container, and I am able to run it successfully using the following command:

docker run -v exported_model:/models/default/0000001 -p 8080:8080 -it gcr.io/cloud-automl-tables-public/model_server

Once running as a local host via Docker, I am able to make predictions using the following python code:

import requests
import json

vector = [1, 1, 1, 1, 1, 2, 1]
input = {"instances": [{"column_1": vector[0],
                             "column_2": vector[1],
                             "column_3": vector[2],
                             "column_4": vector[3],
                             "column_5": vector[4],
                             "column_6": vector[5],
                             "column_7": vector[6]}]}
jsonData = json.dumps(input)
response = requests.post("http://localhost:8080/predict", jsonData)
print(response.json())

I need to publish this model as an API to be used by my client. I have considered AWS EC2 and Azure functions, however, I have not had any success so far. Ideally, I plan to use the FastAPI interface, but do not know how to do this in a dockerized context.

1

There are 1 answers

0
munkaboo On

I have since solved the problem, however, it comes at a cost. It is possible to deploy an AutoML model from GCP, but it will incur a small charge over time. I don't think there is a way around this, otherwise Google would loose revenue.

Once the model is up and running, the following Python code can be used to make predictions:

from google.cloud import automl_v1beta1 as automl

project_id = 'chatbot-286a1'
compute_region = 'us-central1'
model_display_name = 'DNALC_4p_global_20201112093636'
inputs = {'sessionDuration': 60.0, 'createdStartDifference': 1206116.042162, 'confirmedStartDifference': 1206116.20255, 'createdConfirmedDifference': -0.160388}

client = automl.TablesClient(project=project_id, region=compute_region)
feature_importance = False
if feature_importance:
    response = client.predict(
        model_display_name=model_display_name,
        inputs=inputs,
        feature_importance=True,
    )
else:
    response = client.predict(
        model_display_name=model_display_name, inputs=inputs
    )

print("Prediction results:")
for result in response.payload:
    print(
        "Predicted class name: {}".format(result.tables.value)
    )
    print("Predicted class score: {}".format(result.tables.score))
    break

In order for the code to work, the following resources may be helpful:

Installing AutoML in python:

How to install google.cloud automl_v1beta1 for python using anaconda?

Authenticating AutoML in python:

https://cloud.google.com/docs/authentication/production

(Remember to put the json file path to the authentication token as an environment variable - this is for security purposes.)