generated from posit-conf-2023/workshop-template
-
Notifications
You must be signed in to change notification settings - Fork 11
/
03-deploy-rstats.qmd
120 lines (89 loc) · 2.46 KB
/
03-deploy-rstats.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: Deeper into deployment
subtitle: Deploy and maintain models with vetiver (R)
format: html
---
## Create your vetiver model 🏺
```{r}
library(tidyverse)
library(tidymodels)
library(arrow)
path <- here::here("data", "inspections.parquet")
inspections <- read_parquet(path)
set.seed(123)
inspect_split <- initial_split(inspections, prop = 0.8)
inspect_train <- training(inspect_split)
inspect_test <- testing(inspect_split)
inspection_rec <-
recipe(results ~ facility_type + risk + total_violations + inspection_date,
data = inspect_train) |>
step_date(inspection_date, features = c("month", "year"), keep_original_cols = FALSE)
inspection_fit <-
workflow(inspection_rec, rand_forest(mode = "classification", trees = 1e3)) |>
fit(data = inspect_train)
v <- vetiver_model(___, ___)
v
```
## Docker 🐳
See `docker-rstats.qmd` in this same folder if you want to work along with the Docker demo.
## Model metrics as metadata 🎯
Compute metrics for your model:
```{r}
inspect_metric_set <- metric_set(accuracy, mn_log_loss, f_meas)
inspect_metrics <-
augment(inspection_fit, new_data = ___) |>
inspect_metric_set(truth = results, estimate = .pred_class, .pred_FAIL)
inspect_metrics
```
Store these metrics as vetiver metadata:
```{r}
library(vetiver)
v <- vetiver_model(
___,
___,
metadata = list(metrics = ___)
)
```
Write a new version of your pin:
```{r}
library(pins)
board <- ___
board |> ___
```
Get your metrics metadata:
```{r}
extracted_metrics <-
board |>
pin_meta(___) |>
pluck("user", "metrics") |>
as_tibble()
extracted_metrics
```
If you have time, redeploy your model and then get **your neighbor's** metrics metadata by calling the `/metadata` endpoint of their model:
```{r}
url <- ___
r <- httr::content(httr::GET(url), as = "text", encoding = "UTF-8")
jsonlite::fromJSON(___) |>
pluck("user", "metrics") |>
as_tibble()
```
## Add a new endpoint to your API ✨
Write a `plumber.R` file:
```{r}
vetiver_write_plumber(board, ___)
```
Open the file and run it locally; use the "Run API" button in RStudio.
Add a new endpoint to this file!
One option is to add this function plus `library(lubridate)`:
```r
handler_weekday <- function(req) {
wday(req$body$inspection_date, label = TRUE)
}
```
Change your `#* @plumber` decorated function so its body looks like:
```r
pr |>
vetiver_api(v) |>
pr_post(path = "/weekday", handler = handler_weekday)
```
After you add a new endpoint, run the API locally again.