You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

neural.lua 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. --[[
  2. Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. local neural_common = require "plugins/neural"
  14. local ts = require("tableshape").types
  15. local ucl = require "ucl"
  16. local E = {}
  17. -- Controller neural plugin
  18. local learn_request_schema = ts.shape {
  19. ham_vec = ts.array_of(ts.array_of(ts.number)),
  20. rule = ts.string:is_optional(),
  21. spam_vec = ts.array_of(ts.array_of(ts.number)),
  22. }
  23. local function handle_learn(task, conn)
  24. local parser = ucl.parser()
  25. local ok, err = parser:parse_text(task:get_rawbody())
  26. if not ok then
  27. conn:send_error(400, err)
  28. return
  29. end
  30. local req_params = parser:get_object()
  31. ok, err = learn_request_schema:transform(req_params)
  32. if not ok then
  33. conn:send_error(400, err)
  34. return
  35. end
  36. local rule_name = req_params.rule or 'default'
  37. local rule = neural_common.settings.rules[rule_name]
  38. local set = neural_common.get_rule_settings(task, rule)
  39. local version = ((set.ann or E).version or 0) + 1
  40. neural_common.spawn_train {
  41. ev_base = task:get_ev_base(),
  42. ann_key = neural_common.new_ann_key(rule, set, version),
  43. set = set,
  44. rule = rule,
  45. ham_vec = req_params.ham_vec,
  46. spam_vec = req_params.spam_vec,
  47. worker = task:get_worker(),
  48. }
  49. conn:send_string('{"success" : true}')
  50. end
  51. return {
  52. learn = {
  53. handler = handle_learn,
  54. enable = true,
  55. need_task = true,
  56. },
  57. }