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")
## 2024-07-08 12:35:26.614851> Region set to 'us-central1'
gcva_project_set(projectId = projectId)
## 2024-07-08 12:35:26.615399> ProjectId set to 'gc-vertex-ai-r'
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
# manually set model object (to fasttrack for testing)
model <- gcva_model(model = Sys.getenv("GCVA_TEST_MODEL_NAME_CUSTOM"))
model
## ==Google Cloud Vertex AI Model==
## name: projects/442003009360/locations/us-central1/models/6818469627048230912
## displayName: vertex-r-model
## createTime: 2023-01-20 22:38:00
## versionId: 1
## versionAlias: default
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
## {"instances":[{"longitude":"-122.23","latitude":"37.88","housing_median_age":"41","total_rooms":"880","total_bedrooms":"129","population":"322","households":"126","median_income":"8.3252"},{"longitude":"-122.22","latitude":"37.86","housing_median_age":"21","total_rooms":"7099","total_bedrooms":"1106","population":"2401","households":"1138","median_income":"8.3014"},{"longitude":"-122.24","latitude":"37.85","housing_median_age":"52","total_rooms":"1467","total_bedrooms":"190","population":"496","households":"177","median_income":"7.2574"},{"longitude":"-122.25","latitude":"37.85","housing_median_age":"52","total_rooms":"1274","total_bedrooms":"235","population":"558","households":"219","median_income":"5.6431"},{"longitude":"-122.25","latitude":"37.85","housing_median_age":"52","total_rooms":"1627","total_bedrooms":"280","population":"565","households":"259","median_income":"3.8462"}]}
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}'")
)
)
gcva_delete_endpoint(endpoint = endpoint)
to confirm deleted
endpoints <- gcva_list_endpoints()
endpoints
## $createTime
## POSIXct of length 0