-
Notifications
You must be signed in to change notification settings - Fork 15
/
textproject_server.py
345 lines (274 loc) · 9.16 KB
/
textproject_server.py
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json, sys, os, pickle, time, math
import numpy
import theano
import theano.tensor as T
from databases.textproject_reconstruction_database import TextProjectReconstructionDatabase
from nn.containers import Sequential
from nn.rnns import LNLSTM
from nn.layers import OneHot
from nn.utils import Vocabulary
import nn.utils
from lm_vae import Sampler
from lm_vae_sample import LNLSTMStep
from textproject_vae_charlevel import make_model
from wordfilter import Wordfilter
wordfilter = Wordfilter()
### specify your session ###
session = "sp15_trial"
### good work ###
# Flask
from flask import Flask
from flask import request
from flask import jsonify
app = Flask(__name__)
def api_sanitize(s):
san = s.replace('"','')
san = san.replace(u'\u201c', '').replace(u'\u201d', '')
return san
def sanitize_mag(m):
return m
@app.route('/gradient', methods=['GET'])
def gradient():
s1 = api_sanitize(request.args.get('s1')[0:max_len])
s2 = api_sanitize(request.args.get('s2')[0:max_len])
print("Interpolating:")
print(s1)
print(s2)
results = serve_interpolate(s1, s2)
results = process_results(results)
results = screen_results(results)
return jsonify({"results": results})
@app.route('/neighborhood', methods=['GET'])
def jitter():
s1 = api_sanitize(request.args.get('s1')[0:max_len])
mag = request.args.get('mag') or 0.1
print("Exploring neighborhood around:")
print(s1)
results = serve_jitter(s1, mag)
results = process_results(results)
results = screen_results(results)
return jsonify({"results": results})
@app.route('/get_z', methods=['GET'])
def get_z():
#json = request.get_json()
s1 = request.args.get('s1')
print(s1)
z, text = serve_get_z(s1)
return jsonify({"z": z, "text": text})
# The actual work
t1 = time.time()
print("It begins")
vocab = Vocabulary()
if os.path.exists("session/%s/vocab.pkl" % session):
with open("session/%s/vocab.pkl" % session) as vocab_file:
vocab = pickle.load(vocab_file)
print("Loaded vocab with %i chars:" % len(vocab))
print(vocab.index_to_word)
else:
print("Using default 256-char vocab")
# Should probably extract this into a little shared module with textproject_reconstruction_database
# Maybe later
vocab.add('<pad>')
vocab.add('<unk>')
vocab.add('<end>')
for i in xrange(32, 128):
ch = chr(i)
vocab.add(ch)
n_classes = len(vocab)
hyper_params = nn.utils.read_json("session/%s/hyper_params.json" % session)
z = hyper_params["z"]
max_len = hyper_params["max_len"]
p = hyper_params["p"]
lstm_size = hyper_params["lstm_size"]
alpha = hyper_params["alpha"]
dataset = hyper_params["dataset"]
sp_model = str(hyper_params["sp_model"]) # I get an error below if i don't cast to string...
using_sp = (sp_model != None) and (len(sp_model) > 0) and (sp_model != "None")
if using_sp:
print("Using sentencepiece")
import sentencepiece as spm # https://github.com/google/sentencepiece
sp = spm.SentencePieceProcessor()
sp.Load(sp_model)
else:
print("Not using sentencepiece")
print("Loading session %s" % session)
print("Trained using dataset %s" % session)
print("z: %s, max_len: %s, p: %s, lstm_size: %s, alpha: %s" % (z, max_len, p, lstm_size, alpha))
model = make_model(z, max_len, p, n_classes, lstm_size, alpha)
model.load("session/%s/model.flt" % session)
model.set_phase(train=False)
start_word = n_classes
n = 7 # an odd number, so there's one in the center!
encoder = model.layers[0].branches[0]
sampler = encoder[-1]
start_words = numpy.ones(n) * start_word
start_words = theano.shared(start_words.astype('int32'))
#sampled = theano.shared(sampled.astype(theano.config.floatX))
decoder_from_z = model.layers[1].branches[0]
x = T.fmatrix('x')
from_z = decoder_from_z(x) #sampled.astype(theano.config.floatX))
layers = model.layers[-3:]
layers[0] = LNLSTMStep(layers[0])
step = Sequential(layers)
embed = model.layers[1].branches[1].layers[-1]
#onehot = OneHot(n_classes + 3) # <unk>, <pad>, <end> I think?
words = start_words
generated = []
#print(from_z)
#print(words)
for i in xrange(max_len):
#print(onehot(words))
ins = T.concatenate([from_z[i], embed(words)], axis=1)
pred = step(ins)
words = T.argmax(pred, axis=1)
generated.append(words[None, :])
generated = T.concatenate(generated, axis=0)
f = theano.function([x], outputs=generated)
t2 = time.time()
print("Startup took %i seconds!" % (t2-t1))
print("Ready for serving.")
def to_inputs(sentence):
sentence = str(sentence) # ???
if using_sp:
#print(self.sp.EncodeAsPieces(sentence))
chars = sp.EncodeAsIds(sentence)
else:
chars = [vocab.by_word(ch, oov_word="<unk>") for ch in sentence]
print(chars)
pad = vocab.by_word("<pad>")
end = vocab.by_word("<end>")
s = []
for char in chars:
if char == end:
break
if char == pad:
break
if using_sp:
s.append(vocab.by_index(char+2)) # OFFSET BUGGGGG omg omg omg
else:
s.append(vocab.by_index(char)) # OFFSET BUGGGGG omg omg omg
readout_str = ""
for c in s:
readout_str += c + " "
end
print(readout_str)
chars.append(vocab.by_word('<end>'))
for i in xrange(max_len - len(chars)):
chars.append(vocab.by_word('<pad>'))
return numpy.asarray(chars)
def render_results(w):
results = []
pad = vocab.by_word("<pad>")
end = vocab.by_word("<end>")
for i in xrange(w.shape[1]):
s = []
for idx in w[:, i]:
if idx == end:
break
if idx == pad:
break
if using_sp:
s.append(vocab.by_index(idx+2)) # THIS IS SO GNARLY
else:
s.append(vocab.by_index(idx)) # THIS IS SO GNARLY
r = ''.join(s)
print r.strip()
results.append(r)
return results
# from: https://github.com/soumith/dcgan.torch/issues/14
def slerp(val, low, high):
omega = numpy.arccos(numpy.clip(numpy.dot(low/numpy.linalg.norm(low), high/numpy.linalg.norm(high)), -1, 1))
so = numpy.sin(omega)
if so == 0:
return (1.0-val) * low + val * high # L'Hopital's rule/LERP
return numpy.sin((1.0-val)*omega) / so * low + numpy.sin(val*omega) / so * high
def lerp(val, low, high):
return (low + (high-low)*val)
def calc_interpolate(s1, s2, num_steps):
s1 = to_inputs(s1)
s2 = to_inputs(s2)
encoder = model.layers[0].branches[0]
sampler = encoder[-1]
assert isinstance(sampler, Sampler)
ins = numpy.zeros((max_len, 2))
ins[:, 0] = s1
ins[:, 1] = s2
x = T.imatrix()
z = encoder(x)
mu = sampler.mu
f = theano.function([x], mu)
z = f(ins.astype('int32'))
s1_z = z[0]
s2_z = z[1]
#n = 15
#s1_z = numpy.repeat(s1_z[None, :], n, axis=0)
#s2_z = numpy.repeat(s2_z[None, :], n, axis=0)
steps = numpy.linspace(0, 1, num_steps)[:, None]
#sampled = s1_z * (1 - steps) + s2_z * steps
sampled = numpy.zeros((num_steps, len(s1_z))) # len(s1_z) gives num of z-dims
# https://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops
for index, step in enumerate(steps):
sampled[index] = lerp(step, s1_z, s2_z)
return sampled
def calc_jitter(s1, mag):
s1 = to_inputs(s1)
encoder = model.layers[0].branches[0]
sampler = encoder[-1]
assert isinstance(sampler, Sampler)
ins = numpy.zeros((max_len, 1))
ins[:, 0] = s1
x = T.imatrix()
z = encoder(x)
mu = sampler.mu
f = theano.function([x], mu)
z = f(ins.astype('int32'))
s1_z = z[0]
s1_z_n = numpy.repeat(s1_z[None, :], n, axis=0)
jitter_scale = float(mag) # 0.2
# MAKE NEGATIVE TOO
jitter_vals = (numpy.random.rand(n, len(s1_z)) - 0.5) * 2.0 * jitter_scale
sampled = s1_z_n + jitter_vals
return sampled
def calc_get_z(s1):
s1 = to_inputs(s1)
encoder = model.layers[0].branches[0]
sampler = encoder[-1]
assert isinstance(sampler, Sampler)
ins = numpy.zeros((max_len, 1))
ins[:, 0] = s1
x = T.imatrix()
z = encoder(x)
mu = sampler.mu
f = theano.function([x], mu)
z = f(ins.astype('int32'))
s1_z = z[0]
return s1_z
def serve_interpolate(s1, s2):
w = f(calc_interpolate(s1, s2, n).astype(theano.config.floatX))
return render_results(w)
def serve_jitter(s1, mag):
w = f(calc_jitter(s1, float(mag)).astype(theano.config.floatX))
return render_results(w)
def serve_get_z(s1):
s1_z = calc_get_z(s1)
multi_z = numpy.repeat(s1_z[None, :], n, axis=0)
w = f(multi_z.astype(theano.config.floatX))
rendered = render_results(w)[0]
return s1_z.tolist(), rendered
def screen_results(results):
screened_results = list(results)
for i in xrange(len(results)):
if wordfilter.blacklisted(results[i]):
screened_results[i] = "***"
return screened_results
def process_results(results):
print("Processing results")
processed_results = list(results)
for i in xrange(len(results)):
if using_sp:
processed_results[i] = results[i].decode('utf-8').replace(u"\u2581"," ")[1:]
else:
processed_results[i] = results[i]
return processed_results