This is closely following this jupyter notebook. The point is to go trough several of the functionalities of VERTA modeldb.
library(reticulate) library(data.table) library(magrittr) library(stringr) library(yardstick) # use_condaenv("verta_reticulate",required=T) # library(vertaReticulateClient)
reg_random_string <- rpois(1,1000) HOST = "dev.verta.ai" PROJECT_NAME <- str_glue("MTCARS Regression{reg_random_string}") EXPERIMENT_NAME <- "Linear Regressions" WORKSPACE <- "Stefan_Petrov" # Sys.setenv(VERTA_EMAIL= "...", # VERTA_DEV_KEY = "...") Sys.setenv(VERTA_EMAIL= "...", VERTA_DEV_KEY = "...")
library(vertaReticulateClient) vertaReticulateClient::init_verta(HOST) proj <- vertaReticulateClient::set_project(name="Reticulate MTCars") experiment <- vertaReticulateClient::set_experiment(name="Verta R Experiment")
N <- nrow(mtcars) trainSet <- mtcars[1:floor(N/2),] testSet <- mtcars[(floor(N/2)+1):N,]
experiment_run_name <- str_glue("MTCars Run {rpois(1,1000)}") run <- set_experiment_run(name=experiment_run_name,desc = experiment_run_name) log_run_hyperparameters(run,hyperparams = list(hyperparam1=1,hyperparam2=2),overwrite = T) log_run_hyperparameters(run,hyperparams = list(formula="mpg~."),overwrite = T) mod_mtcars <- lm(mpg~.,data = trainSet) summary_lm <- summary(mod_mtcars) rsq <- summary_lm$adj.r.squared # run_log_metric(run,key = "rsquared",value = rsq)
run2 <- set_experiment_run(name=str_glue("MTCars Experiment 3 {rpois(1,10000)}"), desc = "MTCars Experiment 3") log_run_hyperparameters(run2,hyperparams = list( formula= "mpg~cyl+disp+hp+drat+wt+qsec+vs" ),overwrite = T) mod_mtcars_2 <- lm(mpg~cyl+disp+hp+drat+wt+qsec+vs,data = trainSet) summary_lm_2 <- summary(mod_mtcars_2) rsq_2 <- summary_lm$adj.r.squared run_log_metric(run2,key = "rsquared",value = rsq_2,overwrite = T)
So we created some 2 experiment runs and added some metrics for them...
first_model_object <- list( model_file = mod_mtcars, formula_obj = "mpg~.", original_df = trainSet, required_packages="", additionalObjects=list() ) saveRDS(first_model_object,"mpg_experiment_object.RData") run$log_model(model = "mpg_experiment_object.RData",overwrite = T)# TODO- add # run_log
The downloaded model is almost what's needed. From the web UI it works fine.
# there is some issue with this # run$download_model("downloaded_model.RData") run$log_model(model = "mpg_experiment_object.RData",overwrite = T)# TODO- add run$download_model("./dl_model/download_model.RData") # run$download_model("./dl_model/") downloaded_model <- readRDS("./dl_model/download_model.RData")# this downloaded_model # this is almost what it should be, but not quite... downloaded_model_lm <- downloaded_model[["model_file"]] downloaded_model_lm
lm_model_from_download <- downloaded_model$model_file
best_run <- get_best_run_by_metric(experiment, "metrics.rsquared",descending = T) best_run_hyperparameters <- best_run$get_hyperparameters()
registered_model_best_mtcars <- get_or_create_registered_model(name="mtcars_best", workspace = WORKSPACE) registered_model_best_mtcars$create_version_from_run(best_run$id,name=str_glue("best_model_{rpois(1,10000)}"))
Here we simply use the downloaded_model in order to deploy a service at localhost:8000
runner_template_location <- file.path(path.package("vertaReticulateClient"), "to_run.R" ) plumber_template_location <- file.path(path.package("vertaReticulateClient"), "plumber_sample.R" ) # createDockerfile( # model_file_location = "../mpg_model.RData", # # model_file_location = downloaded_model, # runner_template_location = runner_template_location, # plumber_template_location = plumber_template_location, # required_packages = "forecast" # ) # log_model_data(best_run,file_to_save_at = "../mpg_model.RData", # downloaded_model, # required_packages = c() # ) log_model_data(best_run,modelObject = downloaded_model,required_packages = c()) best_run$download_model("mpg_experiment_best_model.RData")
createDockerContextZip("mpg_experiment_best_model.RData")
buildDocker(location = "docker_folder_YCEQR4149N/",tag = "verta-mtcars:latest")
Run the client with: ```{bash,eval=FALSE} docker kill $(docker container ls -q) docker run -t 8000:8000 verta-mtcars:latest
```
now test w/ the following reques request:
curl -X POST --location "http://localhost:8000/score"\ -H "Accept: application/json"\ -d "{"cyl":8,"disp":440,"hp":230,"drat":3.83,"wt":5.345,"qsec":17.42,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Chrysler Imperial"}"
Or
POST http://localhost:8000/score Accept: application/json
{"cyl":8,"disp":440,"hp":230,"drat":3.83,"wt":5.345,"qsec":17.42,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Chrysler Imperial"}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.