-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainAPI.R
More file actions
112 lines (86 loc) · 3.7 KB
/
mainAPI.R
File metadata and controls
112 lines (86 loc) · 3.7 KB
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
# Intro Comments ----------------------------------------------------------
# This script loads the model and sets up the API. It assumes that
# `createModel.R` has already been run and that the model and dataframe are
# present in the `model` folder, along with the parameter validation script
# `valParams.R`. Note that there is no restriction as to where the model was
# run, the files can be cloned later if desired into the docker container.
# Endpoints:
# "/" : used for a simple html welcome screen.
# "/titanic" : used to serve predictions.
# It is assumed that the script is called from `runAPI.R`, and that the
# relevant packages (mlr and plumbr) are loaded.
# Created by George Kampolis.
# Load Model, Data Set and Validation Function ----------------------------
library(mlr)
titanicModel <- readRDS("model/titanicModel.rds")
titanicNewData <- readRDS("model/titanicNewData.rds")
source("model/valParams.R")
# Prediction API ----------------------------------------------------------
#' Predict survival on the titanic
#' @param pClass:int Class of passenger. Possible values: 1, 2 or 3.
#' @param pSex:character Sex of passenger. Possible values: male or female.
#' @param pAge:numeric Age of passenger in years. Numeric.
#' @param pFare:numeric Fare paid by passenger in pounds £. Numeric
#' @param pFamily:int No. of family members on board. Integer.
#' @get /titanic
function(pClass, pSex, pAge, pFare, pFamily){
# validate parameters passed
valResult <- valParams(pClass, pSex, pAge, pFare, pFamily)
# make predictions if valid parameters are supplied ----
if (isTRUE(valResult)) {
titanicNewData$class[1] <<- factor(pClass, levels=levels(titanicNewData$class))
titanicNewData$sex[1] <<- factor(pSex, levels=levels(titanicNewData$sex))
titanicNewData$age[1] <<- as.numeric(pAge)
titanicNewData$fare[1] <<- as.numeric(pFare)
titanicNewData$familyOnBoard <<- as.integer(pFamily)
pred <- predict(object = titanicModel, newdata = as.data.frame(titanicNewData))
return(as.data.frame(pred))
} else {
return(valResult)
}
}
# Intro Page --------------------------------------------------------------
# Simple into page just to give an idea of what's going.
#' Welcome to the Titanic API
#' @get /
#' @html
function() {
title <- "Titanic API"
bodyIntro <- "Welcome to the Titanic API!"
bodyMsg <- paste("To receive a prediction on survival probability,",
"submit the following variables to the <b>/titanic</b> endpoint:",
sep = "\n")
varList <- list(
pClass = "pClass (passenger's class): 1, 2, 3 (Ticket Class: 1st, 2nd, 3rd).",
pSex = "pSex, either female or male",
pAge = "pAge, in years.",
pFare = "pFare, in pounds.",
pFamily = "pFamily, other family members on board - must be integer.",
gap = "",
reponse = paste("Successful submission will result in a json return of",
" survival (response TRUE) or not (response FALSE) and",
" overall prediction (with a simple 50% threshold).",
sep = "\n"
)
)
bodyReqs <- paste(varList, collapse = "<br>")
exampleQuery <- paste("<b>Example query:</b>",
".../titanic?pClass=2&pSex=male&pAge=70&pFare=125&pFamily=0",
"<b> Expected response:</b>",
'[{"prob.FALSE":0.0479,"prob.TRUE":0.9521,"response":"TRUE"}]',
sep = "\n"
)
result <- paste(
"<html>",
"<h1>", title, "</h1>", "<br>",
"<body>",
"<p>", bodyIntro, "</p>",
"<p>", bodyMsg, "</p>",
"<p>", bodyReqs, "</p>",
"<p>", exampleQuery, "</p>",
"</body>",
"</html>",
collapse = "\n"
)
return(result)
}