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.
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("http://h2o-release.s3.amazonaws.com/h2o/rel-turnbull/2/R"))) library(h2o) 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, Month, DayofMonth, DayOfWeek, CRSDepTime, CRSArrTime, UniqueCarrier, FlightNum, CRSElapsedTime, Origin, Dest) { new_data<- data.frame(Year=Year, Month=Month, DayofMonth=DayofMonth, DayOfWeek=DayOfWeek, CRSDepTime=CRSDepTime, CRSArrTime=CRSArrTime, UniqueCarrier=UniqueCarrier, FlightNum=FlightNum, CRSElapsedTime=CRSElapsedTime, Origin=Origin, Dest=Dest, ststringsAsFactors = F) predictions <- predict(model_object, as.h2o(new_data)) return(as.character(as.data.frame(predictions$pred)$predict))
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, 'Month':Month, 'DayofMonth':DayofMonth, 'DayOfWeek':DayOfWeek, 'CRSDepTime':CRSDepTime, 'CRSArrTime':CRSArrTime, 'UniqueCarrier':UniqueCarrier, 'FlightNum':FlightNum, 'CRSElapsedTime':CRSElapsedTime, 'Origin':Origin, 'Dest':Dest} 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 3.10.1.2. Our requirements.txt file has one line in it to install the correct version of the H2O Python package:
http://h2o-release.s3.amazonaws.com/h2o/rel-turnbull/2/Python/h2o-3.10.1.2-py2.py3-none-any.whl
Publish the API endpoint
- Navigate to the Publish page in Domino.
- Enter the file containing the code to invoke.
- Enter the function to invoke.
- Click Publish.
- Wait a 2-5 minutes for the API endpoint to start serving.
For more detailed instructions on how to publish an API endpoint, please see our documentation on Model APIs.
Comments
0 comments
Please sign in to leave a comment.