-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathiris-server.R
49 lines (37 loc) · 1.32 KB
/
iris-server.R
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
## Example gRPC service to score features sent in a Proto message
library(grpc)
library(futile.logger)
library(jsonlite)
library(RProtoBuf)
## reading the service definitions
spec <- system.file('examples/iris_classifier.proto', package = 'grpc')
## build model for scoring (saved to global environment)
library(rpart)
names(iris) <- tolower(sub('.', '_', names(iris), fixed = TRUE))
fit <- rpart(species ~ ., iris)
## define service definitions
impl <- read_services(spec)
impl$Classify$f <- function(request) {
request <- as.list(request)
flog.info('Data received for scoring: %s', toJSON(request, auto_unbox = TRUE))
## try to score
response <- tryCatch({
for (v in attr(terms(fit), "term.labels")) {
if (request[[v]] > 0) next else {
stop('Negative ', v, ' provided')
}
}
scores <- predict(fit, newdata = request)
i <- which.max(scores)
cls <- attr(fit, "ylevels")[i]
p <- scores[, i]
flog.info('Predicted class: %s (p=%5.4f)', cls, p)
new(iris.Class, species = cls, probability = p)
},
error = function(e) {
new(iris.Class, status = new(iris.Status, code = 1, message = e$message))
})
response
}
## run the service handlers
start_server(impl, '0.0.0.0:50051')