R/predict_webapi.R

#' Predict using a Web API
#'
#' Performs a prediction using a Web API providing a SavedModel.
#'
#' @inheritParams predict_savedmodel
#'
#' @export
predict_savedmodel.webapi_prediction <- function(
  instances,
  model,
  ...) {

  text_response <- httr::POST(
    url = model,
    body = list(
      instances = instances
    ),
    encode = "json"
  ) %>% httr::content(as = "text")

  tryCatch({
    response <- text_response %>%
      jsonlite::fromJSON(simplifyDataFrame = FALSE)

    if (!identical(response$error, NULL))
      stop(response$error)

    append_predictions_class(response)
  }, error = function(e) {
    stop(text_response)
  })
}

Try the tfdeploy package in your browser

Any scripts or data that you put into this service are public.

tfdeploy documentation built on June 14, 2019, 5:04 p.m.