forked from aceimnorstuvwxz/chatbot-zh-torch7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval-server.lua
executable file
·155 lines (118 loc) · 3.36 KB
/
eval-server.lua
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
require 'neuralconvo'
local tokenizer = require "tokenizer"
local list = require "pl.List"
local options = {}
if dataset == nil then
cmd = torch.CmdLine()
cmd:text('Options:')
cmd:option('--cuda', false, 'use CUDA. Training must be done on CUDA')
cmd:option('--opencl', false, 'use OpenCL. Training must be done on OpenCL')
cmd:option('--debug', false, 'show debug info')
cmd:text()
options = cmd:parse(arg)
-- Data
dataset = neuralconvo.DataSet()
-- Enabled CUDA
if options.cuda then
require 'cutorch'
require 'cunn'
elseif options.opencl then
require 'cltorch'
require 'clnn'
end
end
if model == nil then
print("-- Loading model")
model = torch.load("data/model.t7")
end
-- Word IDs to sentence
function pred2sent(wordIds, i)
local words = {}
i = i or 1
for _, wordId in ipairs(wordIds) do
local word = dataset.id2word[wordId[i]]
--print(wordId[i]..word)
table.insert(words, word)
end
return tokenizer.join(words)
end
function printProbabilityTable(wordIds, probabilities, num)
print(string.rep("-", num * 22))
for p, wordId in ipairs(wordIds) do
local line = "| "
for i = 1, num do
local word = dataset.id2word[wordId[i]]
line = line .. string.format("%-10s(%4d%%)", word, probabilities[p][i] * 100) .. " | "
end
print(line)
end
print(string.rep("-", num * 22))
end
function say(text)
local wordIds = {}
--print(text)
local values = {}
for w in text:gmatch("[\33-\127\192-\255]+[\128-\191]*") do
table.insert(values, w)
end
for i, word in ipairs(values) do
local id = dataset.word2id[word] or dataset.unknownToken
--print(i.." "..word.." "..id)
table.insert(wordIds, id)
end
--[[
for t, word in tokenizer.tokenize(text) do
local id = dataset.word2id[word:lower()] or dataset.unknownToken
table.insert(wordIds, id)
end
]]--
local input = torch.Tensor(list.reverse(wordIds))
local wordIds, probabilities = model:eval(input)
local ret = pred2sent(wordIds)
print(">> " .. ret)
if options.debug then
printProbabilityTable(wordIds, probabilities, 4)
end
return ret
end
--[[ http server using ASyNC]]--
function unescape (s)
s = string.gsub(s, "+", " ")
s = string.gsub(s, "%%(%x%x)", function (h)
return string.char(tonumber(h, 16))
end)
return s
end
local async = require 'async'
require('pl.text').format_operator()
async.http.listen('http://0.0.0.0:8082/', function(req,res)
print('request:',req)
local resp
if req.url.path == '/' and req.url.query ~= nil and #req.url.query > 0 then
local text_in = unescape(req.url.query)
print(text_in)
local ret = say(text_in)
resp = [[${data}]] % {data = ret}
else
resp = 'Oops~ This is a wrong place, please goto <a href="/?你好啊"> here!</a>'
end
-- if req.url.path == '/test' then
-- resp = [[
-- <p>You requested route /test</p>
-- ]]
-- else
-- -- Produce a random story:
-- resp = [[
-- <h1>From my server</h1>
-- <p>It's working!<p>
-- <p>Randomly generated number: ${number}</p>
-- <p>A variable in the global scope: ${ret}</p>
-- ]] % {
-- number = math.random(),
-- ret = ret
-- }
-- end
res(resp, {['Content-Type']='text/html; charset=UTF-8'})
end)
print('server listening to port 8082')
async.go()