-
Notifications
You must be signed in to change notification settings - Fork 2
/
julia_web_server.jl
60 lines (47 loc) · 1.62 KB
/
julia_web_server.jl
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
# Fashioned into two main parts
###############################################################################
# Train the model
###############################################################################
using RDatasets
using DataFrames
using ScikitLearn
using ScikitLearn: fit!, predict
@sk_import ensemble: GradientBoostingClassifier
iris = dataset("datasets", "iris")
X = convert(Array, iris[[:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]])
y = convert(Array, iris[:Species])
model = GradientBoostingClassifier()
fit!(model, X, y)
###############################################################################
# Build the API Call
###############################################################################
using HttpServer
import JSON
function dicttodataframe(dict)
new_df = DataFrame()
for (k,v) in dict
new_df[Symbol(k)] = v
end
return new_df
end
http = HttpHandler() do req::Request, res::Response
if length(req.data) > 0
requestData = JSON.parse(String(req.data))
a = dicttodataframe(requestData)
request_X = convert(Array, a[[:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]])
println(request_X)
end
if req.method == "GET"
responseData = "Submit as a POST"
end
if req.method == "POST"
y_hat = predict(model, request_X)
responseData = JSON.json(y_hat)
end
Response(responseData)
end
###############################################################################
# Kick off the http server
###############################################################################
server = Server(http)
run(server, 8000)