This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DataProvider.scala
171 lines (149 loc) · 5.45 KB
/
DataProvider.scala
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (C) 2017-2019 Lightbend
*
* This file is part of the Lightbend model-serving-tutorial (https://github.com/lightbend/model-serving-tutorial)
*
* The model-serving-tutorial is free software: you can redistribute it and/or modify
* it under the terms of the Apache License Version 2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lightbend.modelserving.client.client
import java.io.{ByteArrayOutputStream, File}
import java.nio.file.{Files, Paths}
import com.google.protobuf.ByteString
import com.lightbend.model.modeldescriptor.ModelDescriptor
import com.lightbend.model.winerecord.WineRecord
import com.lightbend.modelserving.client.{KafkaLocalServer, MessageSender}
import com.lightbend.modelserving.configuration.ModelServingConfiguration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.io.Source
/**
* Application publishing models from /data directory to Kafka.
*/
object DataProvider {
import ModelServingConfiguration._
val file = "data/winequality_red.csv"
val directory = "data/"
val tensorfile = "data/optimized_WineQuality.pb"
val tensorfilebundle = "data/saved/1/"
var modelTimeInterval = 1000 * 60 * 1 // 1 mins
var dataTimeInterval = 1000 * 1 // 1 sec
def main(args: Array[String]) {
println(s"Using kafka brokers at $KAFKA_BROKER")
println(s"Data Message delay $dataTimeInterval")
println(s"Model Message delay $modelTimeInterval")
val kafka = KafkaLocalServer(true)
kafka.start()
kafka.createTopic(DATA_TOPIC)
kafka.createTopic(MODELS_TOPIC)
println(s"Cluster created")
publishData()
publishModels()
while(true)
pause(600000)
}
def publishData() : Future[Unit] = Future {
val sender = MessageSender(KAFKA_BROKER)
val bos = new ByteArrayOutputStream()
val records = getListOfDataRecords(file)
var nrec = 0
while (true) {
records.foreach(record => {
val r = record.withTs(System.currentTimeMillis())
bos.reset()
r.writeTo(bos)
sender.writeValue(DATA_TOPIC, bos.toByteArray)
nrec = nrec + 1
if (nrec % 10 == 0)
println(s"wrote $nrec records")
pause(dataTimeInterval)
})
}
}
def publishModels() : Future[Unit] = Future {
val sender = MessageSender(KAFKA_BROKER)
val files = getListOfModelFiles(directory)
val bos = new ByteArrayOutputStream()
while (true) {
// TF model bundled
val tbRecord = ModelDescriptor(name = "tensorflow saved model",
description = "generated from TensorFlow saved bundle", modeltype =
ModelDescriptor.ModelType.TENSORFLOWSAVED, dataType = "wine").
withLocation(tensorfilebundle)
bos.reset()
tbRecord.writeTo(bos)
sender.writeValue(MODELS_TOPIC, bos.toByteArray)
println(s"Published Model ${tbRecord.description}")
pause(modelTimeInterval)
files.foreach(f => {
// PMML
val pByteArray = Files.readAllBytes(Paths.get(directory + f))
val pRecord = ModelDescriptor(
name = f.dropRight(".pmml".length),
description = "generated from SparkML", modeltype = ModelDescriptor.ModelType.PMML,
dataType = "wine"
).withData(ByteString.copyFrom(pByteArray))
bos.reset()
pRecord.writeTo(bos)
sender.writeValue(MODELS_TOPIC, bos.toByteArray)
println(s"Published Model ${pRecord.description}")
pause(modelTimeInterval)
})
// TF
val tByteArray = Files.readAllBytes(Paths.get(tensorfile))
val tRecord = ModelDescriptor(name = tensorfile.dropRight(".pb".length),
description = "generated from TensorFlow", modeltype = ModelDescriptor.ModelType.TENSORFLOW,
dataType = "wine").withData(ByteString.copyFrom(tByteArray))
bos.reset()
tRecord.writeTo(bos)
sender.writeValue(MODELS_TOPIC, bos.toByteArray)
println(s"Published Model ${tRecord.description}")
pause(modelTimeInterval)
}
}
private def pause(timeInterval : Long): Unit = {
try {
Thread.sleep(timeInterval)
} catch {
case _: Throwable => // Ignore
}
}
def getListOfDataRecords(file: String): Seq[WineRecord] = {
var result = Seq.empty[WineRecord]
val bufferedSource = Source.fromFile(file)
for (line <- bufferedSource.getLines) {
val cols = line.split(";").map(_.trim)
val record = new WineRecord(
fixedAcidity = cols(0).toDouble,
volatileAcidity = cols(1).toDouble,
citricAcid = cols(2).toDouble,
residualSugar = cols(3).toDouble,
chlorides = cols(4).toDouble,
freeSulfurDioxide = cols(5).toDouble,
totalSulfurDioxide = cols(6).toDouble,
density = cols(7).toDouble,
pH = cols(8).toDouble,
sulphates = cols(9).toDouble,
alcohol = cols(10).toDouble,
dataType = "wine"
)
result = record +: result
}
bufferedSource.close
result
}
private def getListOfModelFiles(dir: String): Seq[String] = {
val d = new File(dir)
if (d.exists && d.isDirectory) {
d.listFiles.filter(f => (f.isFile) && (f.getName.endsWith(".pmml"))).map(_.getName)
} else {
Seq.empty[String]
}
}
}