-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_server.py
299 lines (262 loc) · 10.7 KB
/
generate_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
###############################################################################
# Language Modeling on Wikitext-2
#
# This file generates new sentences sampled from the language model
#
###############################################################################
import argparse
import torch
import data
from random import random
from flask import Flask, render_template,request,url_for,redirect
app = Flask(__name__, static_url_path='/static')
@app.route('/Form1',methods=['get'])
def Form1():
return render_template('form1_index.html')
@app.route('/Form2')
def Form2():
select=request.args["selected_value"]
# return select
return render_template("form2_index.html",value=select)
@app.route('/form2_image',methods=['get'])
def form2_image():
return redirect(url_for('/static/images/new.jpg'))
parser = argparse.ArgumentParser(description='PyTorch Wikitext-2 Language Model')
# Model parameters.
parser.add_argument('--data', type=str, default='./data/wikitext-2',
help='location of the data corpus')
parser.add_argument('--checkpoint', type=str, default='./LSTM_new2.pt',
help='model checkpoint to use')
parser.add_argument('--outf', type=str, default='generated.txt',
help='output file for generated text')
parser.add_argument('--words', type=int, default='100',
help='number of words to generate')
parser.add_argument('--seed', type=int, default=2222,
help='random seed')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--temperature', type=float, default=1.0,
help='temperature - higher will increase diversity')
parser.add_argument('--log-interval', type=int, default=10,
help='reporting interval')
args = parser.parse_args()
import string
import re
import numpy as np
import torch
from torch.autograd import Variable
# from gensim.models import Word2Vec
###############################################################################
# Load data
###############################################################################
#load file and read text
def load_doc(file):
words=[]
with open(file,'r')as f:
for line in f:
line=re.sub('=.*=','',line)
words+=line.split()
words=[word for word in words if word!='<unk>']
return words
#(convert into words, remove puntuations,create punctuation dict and translate,allow only alphabeticals,transform into lower case)
#(accept text corpus and return formatted words)
def clean_doc(corpus):
tokens=corpus.split()
table=str.maketrans('','',string.punctuation)
tokens=[word.translate(table)for word in tokens]
tokens=[word.lower() for word in tokens if word.isalpha()]
return tokens
def find_voc(file_name):
token=load_doc(file_name)
#token=clean_doc(re.sub('=.*=','',token))
#token=[i for i in token if i!='unk']
return token
tokens=[]
file_name="./data/wikitext-2/train.txt"
tokens+=find_voc(file_name)
train_tokens=find_voc(file_name)
file_name="./data/wikitext-2/valid.txt"
tokens+=find_voc(file_name)
valid_tokens=find_voc(file_name)
file_name="./data/wikitext-2/test.txt"
tokens+=find_voc(file_name)
test_tokens=find_voc(file_name)
data_size=len(tokens)
voc=set(tokens)
voc_size=len(voc)
print("Data_size",data_size)
print("Voc_size",voc_size)
#(converting each word into one_hot representation using voc_size)
# def one_hot(i):
# one_hot_value=np.zeros((voc_size))
# one_hot_value[i]=1
# return one_hot_value
# print(" ".join(tokens))
#(create dictionary for word_to_index ,index_to_word,index_to_one_hot_rep)
w_i = { ch:i for i,ch in enumerate(voc)}
i_w = { i:ch for i,ch in enumerate(voc)}
# word2vec_rep={ch:w2v[i_w[ch]] for i,ch in enumerate(voc)}
###############################################################################
# Convert into Numeric_Data
###############################################################################
def numeric_tokens(tokens):
numeric_tokens=torch.LongTensor([w_i[i] for i in tokens])
return numeric_tokens
numeric_train_tokens=numeric_tokens(train_tokens)
numeric_valid_tokens=numeric_tokens(valid_tokens)
numeric_test_tokens=numeric_tokens(test_tokens)
###############################################################################
# Batchify dataset
###############################################################################
batch_size=20
def batchify(data, bsz):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
return data
eval_batch_size = 10
train_data = batchify(numeric_train_tokens,batch_size)
print(train_data.shape)
val_data = batchify(numeric_valid_tokens, eval_batch_size)
print(val_data.shape)
test_data = batchify(numeric_test_tokens, eval_batch_size)
print(test_data.shape)
###############################################################################
# Create Input And Target Sequences
###############################################################################
def get_batch(source, i,seq_len):
seq_len = min(seq_len, len(source) - 1 - i)
data = source[i:i+seq_len]
target = source[i+1:i+1+seq_len].view(-1)
return data, target
#Build model
#model consists of a Lstm layer and a Linear fully connected layer
###############################################################################
# Creation of model
###############################################################################
import torch.nn as nn
class Rnn(nn.Module):
def __init__(self,num_classes,hidden_size,num_layers,dim_size,batch_size,drop_prob=0.5):
super(Rnn,self).__init__()
self.no_classes=num_classes
# self.input_size=input_size
self.hidden_size=hidden_size
self.num_layers=num_layers
self.batch_size=batch_size
# self.seq_len=seq_len
self.dim_size=dim_size
self.dropout_prob=drop_prob
self.embed=nn.Embedding(voc_size,dim_size)
self.drop= nn.Dropout(self.dropout_prob)
self.lstm=nn.LSTM(dim_size,hidden_size,num_layers)
self.fc=nn.Linear(self.hidden_size,self.no_classes)
def init_weights(self):
initrange = 0.1
self.embed.weight.data.uniform_(-initrange, initrange)
self.fc.bias.data.zero_()
self.fc.weight.data.uniform_(-initrange, initrange)
#Forward propagation of our model
def forward(self,x,hidden):
embeds=self.embed(x)
embeds=self.drop(embeds)
out,hidden=self.lstm(embeds,hidden)
out=self.drop(out)
out=out.view(-1,self.hidden_size)
decoded=self.fc(out)
return decoded,hidden
def init_hidden(self, bsz):
weight = next(self.parameters())
return (weight.new_zeros(self.num_layers, bsz, self.hidden_size),
weight.new_zeros(self.num_layers, bsz, self.hidden_size))
if torch.cuda.is_available():
if not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
device = torch.device("cuda" if args.cuda else "cpu")
if args.temperature < 1e-3:
parser.error("--temperature has to be greater or equal 1e-3")
with open(args.checkpoint, 'rb') as f:
model = torch.load(f,map_location='cpu').cpu()
##############################n-gram model with Maximum Liklihood Estimation####################################
def generate_word(lm, history, order):
history = history[-order:]
his=" ".join(history)
dist = lm[his]
x = random()
for w,v in dist:
x = x - v
if x <= 0:
return w
def generate_text(lm,history,order,nwords):
# history = ["~"] * order
history=history.split(" ")
out = []
for i in range(nwords):
w = generate_word(lm, history, order)
w=w.replace(" ","")
w1=w.split(" ")
history = history[-order:] + w1
out.append(w)
return " ".join(out)
#########################################################################################################
import json
@app.route('/Form3')
def Form3():
name=request.args["model"]
text_area1=request.args["text_area1"]
text1=request.args["text1"]
if(name=="LSTM"):
text_area1=text_area1.split()
ntokens=voc_size
# ntokens = len(corpus.dictionary)
hidden = model.init_hidden(1)
# input = torch.randint(ntokens, (1, 1), dtype=torch.long).to(device)
# print(corpus.dictionary.word2idx['Plot'])
input=torch.tensor(w_i[text_area1[-1]]).view(1,1)
pred_seq=""
print(i_w[input.item()])
with open(args.outf, 'w') as outf:
with torch.no_grad(): # no tracking history
for i in range(int(text1)):
output, hidden = model(input, hidden)
word_weights = output.squeeze().div(args.temperature).exp().cpu()
word_idx = torch.multinomial(word_weights, 1)[0]
input.fill_(word_idx)
word = i_w[word_idx.item()]
pred_seq+=word+" "
outf.write(word + ('\n' if i % 20 == 19 else ' '))
if i % args.log_interval == 0:
print('| Generated {}/{} words'.format(i,int(text1)))
print(pred_seq)
return pred_seq
else:
yourdict = json.load(open("filename.csv"))
history=text_area1
print (generate_text(yourdict,history,35,int(text1)))
return generate_text(yourdict,history,35,int(text1))
if __name__ == '__main__':
app.run(debug=True)
'''
corpus = data.Corpus(args.data)
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(1)
# input = torch.randint(ntokens, (1, 1), dtype=torch.long).to(device)
# print(corpus.dictionary.word2idx['Plot'])
input=torch.tensor(corpus.dictionary.word2idx['Plot']).view(1,1)
pred_seq=""
with open(args.outf, 'w') as outf:
with torch.no_grad(): # no tracking history
for i in range(args.words):
output, hidden = model(input, hidden)
word_weights = output.squeeze().div(args.temperature).exp().cpu()
word_idx = torch.multinomial(word_weights, 1)[0]
input.fill_(word_idx)
word = corpus.dictionary.idx2word[word_idx]
pred_seq+=word+" "
outf.write(word + ('\n' if i % 20 == 19 else ' '))
if i % args.log_interval == 0:
print('| Generated {}/{} words'.format(i, args.words))
print(pred_seq)
'''