How do I deploy my H2O model as an API endpoint?

If you have developed a model in H2O, you can deploy that model as a web service using Domino’s API endpoints. This help article will guide you through the steps of exporting your H2O model and publishing that model as an API endpoint in Domino using Python or R.

For a public sample Domino project, click here.

Export the H2O model

You can export your H2O model in 3 different ways depending on how you're using H2O.

For H2O Flow, you will use the exportModel command. For example, if the name of your model is gbm_airlines_test and you want to save it to a particular folder calling it airlines_model, use the following command:

exportModel "gbm_airlines_test", "/path/to/my_models/airlines_model", overwrite: false

You can also use the user interface to export your model:

For R, use the h2o.saveModelcommand. For example, if your model object in R is airline_model, save the model object with the following command:

h2o.saveModel(object=airline_model,path=getwd(), force=TRUE)

Similarly, for Python, use the h2o.save_model command:

h2o.save_model(model=airline_model, path="/path/to/my_model/airlines_model", force=True)

For all of the above, after exporting, upload the model file to your Domino project.


Create a wrapper

To create an API endpoint in Domino, you need to create an R or Python file that holds the function that you want to expose as an API endpoint. In this file, you will load the model and write a function to designate the input and output for the API endpoint.

Here is an example in R:

# Remove old H2O packages and install the latest H2O R package
if ("package:h2o" %in% search()) { detach("package:h2o", unload=TRUE) }
if ("h2o" %in% rownames(installed.packages())) { remove.packages("h2o") }
install.packages("h2o", type="source", repos=(c("")))

localH2O <-  h2o.init(nthreads = -1)  # Start an H2O cluster with nthreads = num cores on your machine
model_object <- h2o.loadModel("airlines_model")  #Load up your saved model

# Define a function that feeds inputs to your model to return a prediction
score <- function(Year, 
                  Dest) {
    new_data<- data.frame(Year=Year, 
                          Dest=Dest, ststringsAsFactors = F)
  predictions <- predict(model_object, as.h2o(new_data))

Here is an example in Python:

import h2o
h2o.init(nthreads = -1)  # Start an H2O cluster with nthreads = num cores on your machine
airlines_model = h2o.load_model("airlines_model")

def score(Year, Month, DayofMonth, DayOfWeek, CRSDepTime, CRSArrTime, UniqueCarrier, FlightNum, CRSElapsedTime, Origin, Dest):
    new_data = {'Year':Year, 
    h2oFrame = h2o.H2OFrame()
    new_frame = h2oFrame.from_python(new_data)
    prediction = airlines_model.predict(new_frame)
    return prediction.as_data_frame()['predict'][0]

WARNING: Make sure to have the correct version of H2O installed using the requirements.txt file. In our example, the model used version Our requirements.txt file has one line in it to install the correct version of the H2O Python package:


Publish the API endpoint

  1. Navigate to the Publish page in Domino.
  2. Enter the file containing the code to invoke.
  3. Enter the function to invoke.
  4. Click Publish.
  5. Wait a 2-5 minutes for the API endpoint to start serving.

For more detailed instructions on how to publish an API endpoint, go to our support article on API Endpoints and Model Deployment.

Was this article helpful?
0 out of 0 found this helpful