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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
--[[
Copyright (c) 2016, Vsevolod Stakhov <vsevolod@highsecure.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-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.
]]--
-- This plugin is a concept of FANN scores adjustment
-- NOT FOR PRODUCTION USE so far
local rspamd_logger = require "rspamd_logger"
local rspamd_fann = require "rspamd_fann"
local rspamd_util = require "rspamd_util"
local fun = require "fun"
local redis_params
local classifier_config = {
key = 'neural_net',
neurons = 200,
layers = 3,
}
local current_classify_ann = {
loaded = false,
version = 0,
spam_learned = 0,
ham_learned = 0
}
redis_params = rspamd_parse_redis_server('fann_classifier')
local function maybe_load_fann(task, continue_cb, call_if_fail)
local function load_fann()
local function redis_fann_load_cb(err, data)
-- XXX: upstreams
if not err and type(data) == 'table' and type(data[2]) == 'string' then
local version = tonumber(data[1])
local _err,ann_data = rspamd_util.zstd_decompress(data[2])
local ann
if _err or not ann_data then
rspamd_logger.errx(task, 'cannot decompress ann: %s', _err)
else
ann = rspamd_fann.load_data(ann_data)
end
if ann then
current_classify_ann.loaded = true
current_classify_ann.version = version
current_classify_ann.ann = ann
if type(data[3]) == 'string' then
current_classify_ann.spam_learned = tonumber(data[3])
else
current_classify_ann.spam_learned = 0
end
if type(data[4]) == 'string' then
current_classify_ann.ham_learned = tonumber(data[4])
else
current_classify_ann.ham_learned = 0
end
rspamd_logger.infox(task, "loaded fann classifier version %s (%s spam, %s ham), %s MSE",
version, current_classify_ann.spam_learned,
current_classify_ann.ham_learned,
ann:get_mse())
continue_cb(task, true)
elseif call_if_fail then
continue_cb(task, false)
end
elseif call_if_fail then
continue_cb(task, false)
end
end
local key = classifier_config.key
local ret = rspamd_redis_make_request(task,
redis_params, -- connect params
key, -- hash key
false, -- is write
redis_fann_load_cb, --callback
'HMGET', -- command
{key, 'version', 'data', 'spam', 'ham'} -- arguments
)
if not ret then
rspamd_logger.err(task, 'got error connecting to redis')
end
end
local function check_fann()
local _, ret, upstream
local function redis_fann_check_cb(err, data)
if err then
rspamd_logger.errx(task, 'redis error on host %s: %s', upstream:get_addr(), err)
upstream:fail()
else
upstream:ok()
end
if not err and type(data) == 'string' then
local version = tonumber(data)
if version <= current_classify_ann.version then
continue_cb(task, true)
else
load_fann()
end
end
end
local key = classifier_config.key
ret,_,upstream = rspamd_redis_make_request(task,
redis_params, -- connect params
key, -- hash key
false, -- is write
redis_fann_check_cb, --callback
'HGET', -- command
{key, 'version'} -- arguments
)
if not ret then
rspamd_logger.err(task, 'got error connecting to redis')
end
end
if not current_classify_ann.loaded then
load_fann()
else
check_fann()
end
end
local function tokens_to_vector(tokens)
local vec = fun.totable(fun.map(function(tok) return tok[1] end, tokens))
local ret = {}
local neurons = classifier_config.neurons
for i = 1,neurons do
ret[i] = 0
end
fun.each(function(e)
local n = (e % neurons) + 1
ret[n] = ret[n] + 1
end, vec)
local norm = 0
for i = 1,neurons do
if ret[i] > norm then
norm = ret[i]
end
end
for i = 1,neurons do
if ret[i] ~= 0 and norm > 0 then
ret[i] = ret[i] / norm
end
end
return ret
end
local function add_metatokens(task, vec)
local mt = rspamd_gen_metatokens(task)
for _,tok in ipairs(mt) do
table.insert(vec, tok)
end
end
local function create_fann()
local layers = {}
local mt_size = rspamd_count_metatokens()
local neurons = classifier_config.neurons + mt_size
for i = 1,classifier_config.layers - 1 do
layers[i] = math.floor(neurons / i)
end
table.insert(layers, 1)
local ann = rspamd_fann.create(classifier_config.layers, layers)
current_classify_ann.loaded = true
current_classify_ann.version = 0
current_classify_ann.ann = ann
current_classify_ann.spam_learned = 0
current_classify_ann.ham_learned = 0
end
local function save_fann(task, is_spam)
local ret, conn, upstream
local function redis_fann_save_cb(err)
if err then
rspamd_logger.errx(task, "cannot save neural net to redis: %s", err)
upstream:fail()
else
upstream:ok()
end
end
local data = current_classify_ann.ann:data()
local key = classifier_config.key
current_classify_ann.version = current_classify_ann.version + 1
if is_spam then
current_classify_ann.spam_learned = current_classify_ann.spam_learned + 1
else
current_classify_ann.ham_learned = current_classify_ann.ham_learned + 1
end
ret,conn,upstream = rspamd_redis_make_request(task,
redis_params, -- connect params
key, -- hash key
true, -- is write
redis_fann_save_cb, --callback
'HMSET', -- command
{
key,
'data', rspamd_util.zstd_compress(data),
}) -- arguments
if ret then
conn:add_cmd('HINCRBY', {key, 'version', 1})
if is_spam then
conn:add_cmd('HINCRBY', {key, 'spam', 1})
else
conn:add_cmd('HINCRBY', {key, 'ham', 1})
end
else
rspamd_logger.err(task, 'got error connecting to redis')
end
end
if redis_params then
rspamd_classifiers['neural'] = {
classify = function(task, classifier, tokens)
local function classify_cb()
local min_learns = classifier:get_param('min_learns')
if min_learns then
min_learns = tonumber(min_learns)
end
if min_learns and min_learns > 0 then
if current_classify_ann.ham_learned < min_learns or
current_classify_ann.spam_learned < min_learns then
rspamd_logger.infox(task, 'fann classifier has not enough learns: (%s spam, %s ham), %s required',
current_classify_ann.spam_learned, current_classify_ann.ham_learned,
min_learns)
return
end
end
-- Perform classification
local vec = tokens_to_vector(tokens)
add_metatokens(task, vec)
local out = current_classify_ann.ann:test(vec)
local result = rspamd_util.tanh(2 * (out[1]))
local symscore = string.format('%.3f', out[1])
rspamd_logger.infox(task, 'fann classifier score: %s', symscore)
if result > 0 then
fun.each(function(st)
task:insert_result(st:get_symbol(), result, symscore)
end,
fun.filter(function(st)
return st:is_spam()
end, classifier:get_statfiles())
)
else
fun.each(function(st)
task:insert_result(st:get_symbol(), -result, symscore)
end,
fun.filter(function(st)
return not st:is_spam()
end, classifier:get_statfiles())
)
end
end
maybe_load_fann(task, classify_cb, false)
end,
learn = function(task, _, tokens, is_spam)
local function learn_cb(_, is_loaded)
if not is_loaded then
create_fann()
end
local vec = tokens_to_vector(tokens)
add_metatokens(task, vec)
if is_spam then
current_classify_ann.ann:train(vec, {1.0})
rspamd_logger.infox(task, "learned ANN spam, MSE: %s",
current_classify_ann.ann:get_mse())
else
current_classify_ann.ann:train(vec, {-1.0})
rspamd_logger.infox(task, "learned ANN ham, MSE: %s",
current_classify_ann.ann:get_mse())
end
save_fann(task, is_spam)
end
maybe_load_fann(task, learn_cb, true)
end,
}
end
|