knitr::opts_chunk$set(echo = TRUE)
library(withr) # for gcloud sdk: https://stackoverflow.com/a/64381848/1812363 library(glue) sh <- function(cmd, args = c(), intern = FALSE) { with_path(path.expand("~/google-cloud-sdk/bin/"), { if (is.null(args)) { cmd <- glue(cmd) s <- strsplit(cmd, " ")[[1]] cmd <- s[1] args <- s[2:length(s)] } ret <- system2(cmd, args, stdout = TRUE, stderr = TRUE) if ("errmsg" %in% attributes(attributes(ret))$names) cat(attr(ret, "errmsg"), "\n") if (intern) return(ret) else cat(paste(ret, collapse = "\n")) } ) }
library(googleAuthR) library(googleCloudVertexAIR) options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/cloud-platform") # options(googleAuthR.verbose = 0) # set when debugging gar_auth_service(json_file = Sys.getenv("GAR_SERVICE_JSON"))
projectId <- Sys.getenv("GCVA_DEFAULT_PROJECT_ID") gcva_region_set(region = "us-central1") gcva_project_set(projectId = projectId)
Doc: Overview of getting predictions on Vertex AI | Google Cloud API: https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.endpoints/create
endpoint <- gcva_create_endpoint( displayName = "CaliforniaHousingEndpoint" ) endpoint
endpoints <- gcva_list_endpoints() endpoints
# manually set model object (to fasttrack for testing) model <- gcva_model(model = Sys.getenv("GCVA_TEST_MODEL_NAME_CUSTOM")) model
Doc: https://cloud.google.com/vertex-ai/docs/predictions/get-predictions#api API: https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.endpoints/deployModel
model_deployed <- gcva_deploy( model = model, endpoint = endpoint, machineType = "n1-standard-4" ) model_deployed
2023-03-18 20:45:18> Deploying model to endpoint... 2023-03-18 20:45:28> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:45:38> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:45:48> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:45:58> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:46:08> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:46:19> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:46:29> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:46:39> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:46:49> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:46:59> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:47:09> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:47:19> Model deployment in progress. Waiting 10 seconds before checking operation status. 2023-03-18 20:47:20> Model deployment completed successfully. $name [1] "projects/442003009360/locations/us-central1/endpoints/1203107613345054720/operations/4971509788551675904" $metadata $metadata$`@type` [1] "type.googleapis.com/google.cloud.aiplatform.v1.DeployModelOperationMetadata" $metadata$genericMetadata $metadata$genericMetadata$createTime [1] "2023-03-19T00:45:18.085624Z" $metadata$genericMetadata$updateTime [1] "2023-03-19T00:47:13.271833Z" $done [1] TRUE $response $response$`@type` [1] "type.googleapis.com/google.cloud.aiplatform.v1.DeployModelResponse" $response$deployedModel $response$deployedModel$id [1] "9074185901151617024" attr(,"class") [1] "gcva_endpoint"
data_uri <- "gs://cloud-samples-data/ai-platform-unified/datasets/tabular/california-housing-tabular-regression.csv" ## create example data for prediction request data_raw <- read.csv(text=sh("gsutil cat {data_uri}", intern = TRUE)) ## convert all to string values to build correct prediction request data <- as.data.frame(lapply(data_raw, as.character)) ## build list of 5 examples/rows only for testing instances <- list(instances=head(data[, names(data) != "median_house_value"], 5)) ## convert to required format of JSON library(jsonlite) instances_json <- toJSON(instances, auto_unbox = TRUE) instances_json
gcva_predict( endpoint = endpoint, instances = instances )
library(glue) ## set parameters for prediction request here so easier to understand url <- glue( "https://{gcva_region_get()}-aiplatform.googleapis.com/v1/{endpoint$name}:predict" ) access_token <- sh("gcloud auth print-access-token", intern = TRUE) ## make prediction request sh( "curl", c( "--tr-encoding", "-s", "-X POST", glue("-H 'Authorization: Bearer {access_token}'"), "-H 'Content-Type: application/json'", url, glue("-d '{instances_json}'") ) )
TODO
gcva_undeploy(endpoint = endpoint)
TODO
gcva_endpoint_undeploy_all(endpoint = endpoint)
gcva_delete_endpoint(endpoint = endpoint)
to confirm deleted
endpoints <- gcva_list_endpoints() endpoints
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.