Repository navigation
Expand file tree
/
Copy pathpresent.py
More file actions
262 lines (172 loc) · 6.3 KB
/
Copy pathpresent.py
File metadata and controls
262 lines (172 loc) · 6.3 KB
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
# coding: utf-8
# In[1]:
import tensorflow as tf
import numpy as np
import sys
tf.reset_default_graph()
#import data_processing
#import data_utils
from os import path
import pickle as pkl
import time
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.WARN)
import pickle
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
import os
from tensorflow.python.client import device_lib
from tensorflow.python.ops.rnn_cell import LSTMStateTuple
from IPython.display import clear_output, Image, display, HTML
import json
import numpy as np
import pickle as pkl
import pandas as pd
# In[2]:
f = open('vocab_new2', 'rb')
vocab = pkl.load(f)
f.close()
# In[3]:
len(vocab)
# In[4]:
batch_size = 100
max_para_len = 100
max_ttl_len = 15
hidden_num = 100
vocab_size = len(vocab)
emb_dim = 200
lr = 0.005
prob = 0.5
epochs = 20
ckpt_path = 'model'
attention_states = [batch_size, max_para_len, hidden_num]
# In[5]:
WHITELIST = '0123456789abcdefghijklmnopqrstuvwxyz '
VOCAB_SIZE = 20000
UNK = "UNKNOWN_TOKEN"
limit = {
'max_descriptions' : 1000,
'min_descriptions' : 0,
'max_headings' :70 ,
'min_headings' : 0,
}
# In[6]:
def filter(line, whitelist):
##############################################################
# Filters out all characters which are not in whitelist #
##############################################################
return ''.join([ch for ch in line if ch in whitelist])
# In[7]:
def preprocess(string, vocab):
string = filter(string.lower(), WHITELIST)
token_string = string.split(' ')
new_vocab = vocab[:len(vocab)-2]
word2id = dict((w,i) for i,w in enumerate(new_vocab))
temp = []
for j in range(max_para_len):
if(j>=len(token_string)):
temp.append(len(new_vocab)+1)
else:
if(token_string[j] in new_vocab):
temp.append(word2id[token_string[j]])
else:
temp.append(len(new_vocab))
return temp
# In[44]:
# f = open('save/summary_id_new2', 'rb')
# summary = pkl.load(f)
# f.close()
# f = open('save/vocab_new2', 'rb')
# vocab = pkl.load(f)
# f.close()
# In[8]:
model_name= 'Text Summarizer4'
model_dir = 'model/' + model_name
save_dir = os.path.join(model_dir, "save/")
log_dir = os.path.join(model_dir, "log")
# In[9]:
word2id = dict((w,i) for i, w in enumerate(vocab))
id2word = dict((i,w) for i, w in enumerate(vocab))
# In[33]:
# x= np.array(articles)
# y = np.array(summary)
# a = np.zeros([len(y),1])
# for i in range(len(y)):
# a[i] = len(vocab)-2
# y = np.concatenate((a,y), axis=1)
# In[10]:
inputs = tf.placeholder(tf.int32, (None, max_para_len), 'inputs')
targets = tf.placeholder(tf.int32, (None, None), 'targets')
outputs = tf.placeholder(tf.int32, (None, None), 'outputs')
keep_prob = tf.placeholder(tf.float32)
word_embedding = tf.Variable(tf.random_uniform((vocab_size, emb_dim), -1.0, 1.0), name='enc_embedding')
input_embed = tf.nn.embedding_lookup(word_embedding, inputs)
output_embed = tf.nn.embedding_lookup(word_embedding, outputs)
with tf.variable_scope('encoding') as encoding_scope:
lstm_enc = tf.contrib.rnn.BasicLSTMCell(hidden_num)
final_enc = tf.contrib.rnn.DropoutWrapper(lstm_enc, input_keep_prob=prob)
((enc_fw, enc_bw),(enc_fw_final_state, enc_bw_final_state)) = (
tf.nn.bidirectional_dynamic_rnn(cell_fw=final_enc,
cell_bw = final_enc, inputs=input_embed,dtype=tf.float32)
)
enc_final_state_c = tf.concat((enc_fw_final_state.c, enc_bw_final_state.c), 1)
enc_final_state_h = tf.concat((enc_fw_final_state.h, enc_bw_final_state.h), 1)
encoder_final_state = LSTMStateTuple(c=enc_final_state_c, h= enc_final_state_h)
with tf.variable_scope('attention') as attention_scope:
attention_states = tf.transpose(encoder_final_state, [1, 0, 2])
attention_mechanism = tf.contrib.seq2seq.LuongAttention(hidden_num, attention_states)
with tf.variable_scope('decoding') as decoding_scope:
lstm_dec = tf.contrib.rnn.BasicLSTMCell(hidden_num*2)
final_dec = tf.contrib.rnn.DropoutWrapper(lstm_dec, input_keep_prob=keep_prob)
dec_cell = tf.contrib.seq2seq.AttentionWrapper(final_dec, attention_mechanism)
dec_outputs,_ = tf.nn.dynamic_rnn(final_dec, inputs= output_embed, initial_state=encoder_final_state)
logits = tf.contrib.layers.fully_connected(dec_outputs,num_outputs=vocab_size, activation_fn = None)
with tf.name_scope('optimization'):
loss = tf.contrib.seq2seq.sequence_loss(logits, targets, tf.ones([batch_size, max_ttl_len]))
optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(loss)
# In[11]:
# os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
# os.environ["CUDA_VISIBLE_DEVICES"]="0,1"
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
sess.run(tf.global_variables_initializer()) # For initializing all the variables
saver = tf.train.Saver() # For Saving the model
summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
# In[12]:
saver.restore(sess, save_dir)
# In[13]:
def predict_summary(file):
source_batch = file
dec_input = np.zeros((len(source_batch), 1)) + word2id['UNKNOWN_TOKEN']
for i in range(max_ttl_len):
batch_logits = sess.run(logits,
feed_dict = {inputs: source_batch,
outputs: dec_input,keep_prob:prob})
prediction = batch_logits[:,-1].argmax(axis=-1)
dec_input = np.hstack([dec_input, prediction[:,None]])
output = []
for i in range(len(dec_input)):
temp = []
for j in range(1, len(dec_input[i])):
if(id2word[dec_input[i][j]]=='PAD'):
continue
if(id2word[dec_input[i][j]]=="UNKNOWN_TOKEN"):
temp.append('UNK')
else:
temp.append(id2word[dec_input[i][j]])
temp = ' '.join(temp)
output.append(temp)
return output
# In[14]:
f = open('input_file.txt','r+')
x_input = f.read()
x_input = preprocess(x_input, vocab)
x_input = np.array(x_input).reshape((1,100))
output = predict_summary(x_input)
output = str(output)
# In[15]:
f = open('output_file.txt','w+')
f.write(output)
f.close()