def main(_): FLAGS.start_string = FLAGS.start_string #.decode('utf-8') converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start_string = FLAGS.start_string sys.stdout.write("> ") sys.stdout.flush() start_string = sys.stdin.readline() while start_string: start = converter.text_to_arr(start_string) arr = model.sample(FLAGS.max_length, start, converter.vocab_size) print(converter.arr_to_text(arr)) sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline()
def main(_): converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = tf.train.latest_checkpoint( FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, None, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) # start = converter.text_to_arr(FLAGS.seed_for_generating) seeds = [ 'var a = fun', 'function a(', 'this.', 'document.', 'window.', 'var a = document.g', 'var a;', 'jQuery' ] for seed in seeds: start = converter.text_to_arr(seed) for i in range(0, FLAGS.num_to_generate): print('Generating: ' + seed + ' -> ' + str(i)) file_name = str(uuid.uuid1()) file_path = '../../BrowserFuzzingData/generated/' + FLAGS.file_type + '/' + file_name + '.' + FLAGS.file_type arr = model.sample(FLAGS.max_length_of_generated, start, converter.vocab_size, converter.word_to_int) f = open(file_path, "wb") f.write(converter.arr_to_text(arr).encode('utf-8')) f.close()
def composePotery(): converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = [] arr = model.sample(FLAGS.max_length, start, converter.vocab_size) rawText = converter.arr_to_text(arr) return(selectPoetry(rawText))
def main(_): converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_arr(FLAGS.start_string) arr = model.sample(FLAGS.max_length, start, converter.vocab_size) print(converter.arr_to_text(arr))
def main(_): tc = TextConverter("", -1, byte_file=FLAGS.vocab_path) output_size = tc.vocab_size if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(output_size=output_size, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, sampling=True) model.load(FLAGS.checkpoint_path) start = tc.text_to_arr(FLAGS.start_string) generate_arr = model.sample(FLAGS.length, start, output_size) generate_text = tc.arr_to_text(generate_arr) with open(FLAGS.save_path, 'w', encoding='utf-8') as f: f.write(generate_text) print(generate_text)
def main(_): tokenizer = Tokenizer(vocab_path=FLAGS.tokenizer_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = \ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(tokenizer.vocab_size, sampling=True, n_neurons=FLAGS.n_neurons, n_layers=FLAGS.n_layers, embedding=FLAGS.embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = tokenizer.texts_to_sequences(FLAGS.start_string) arr = model.sample(FLAGS.max_length, start, tokenizer.vocab_size) print(tokenizer.sequences_to_texts(arr))
def main(_): FLAGS.start_string = FLAGS.start_string.decode('utf-8') converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_arr(FLAGS.start_string) arr = model.sample(FLAGS.max_length, start, converter.vocab_size) print(converter.arr_to_text(arr))
def main(_): model_path = os.path.join('model', FLAGS.name) print(model_path) if os.path.exists(model_path) is False: os.makedirs(model_path) path_exist = False else: path_exist = True with codecs.open(FLAGS.input_file, encoding='utf-8') as f: text = f.read() converter = TextConverter(text, FLAGS.max_vocab) converter.save_to_file(os.path.join(model_path, 'converter.pkl')) arr = converter.text_to_arr(text) g = batch_generator(arr, FLAGS.num_seqs, FLAGS.num_steps) print(converter.vocab_size) model = CharRNN(converter.vocab_size, num_seqs=FLAGS.num_seqs, num_steps=FLAGS.num_steps, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, learning_rate=FLAGS.learning_rate, train_keep_prob=FLAGS.train_keep_prob, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size ) model_file_path = tf.train.latest_checkpoint(model_path) if path_exist: model.load(model_file_path) indexes = [] for dirpath, dirnames, filenames in os.walk(model_path): for name in filenames: filepath = os.path.join(dirpath, name) if filepath.endswith(".index"): indexes.append(int(name[6:-6])) indexes.sort() last_index = indexes[-1] model.step = last_index model.train(g, FLAGS.max_steps, model_path, FLAGS.save_every_n, FLAGS.log_every_n, )
def main(_): FLAGS.start_string = FLAGS.start_string.decode('utf-8') converter = TextConverter(filename=FLAGS.converter_path) #创建文本转化器 if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = tf.train.latest_checkpoint( FLAGS.checkpoint_path) #下载最新模型 model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) #加载模型 start = converter.text_to_arr(FLAGS.start_string) #将input text转为id arr = model.sample(FLAGS.max_length, start, converter.vocab_size) #输出为生成的序列 print(converter.arr_to_text(arr))
def generate(): tf.compat.v1.disable_eager_execution() converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_arr(FLAGS.start_string) arr = model.sample(FLAGS.max_length, start, converter.vocab_size) return converter.arr_to_text(arr)
def main(_): FLAGS.start_string = FLAGS.start_string convert = TextConvert(fname=FLAGS.convert_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = tf.train.latest_checkpoint( FLAGS.checkpoint_path) model = CharRNN(convert.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = convert.text2arr(FLAGS.start_string) arr = model.sample(FLAGS.max_length, start, convert.vocab_size) res = convert.arr2text(arr) print('get result: \n', res)
def main(_): FLAGS.start_string = FLAGS.start_string converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = \ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, state_size=FLAGS.state_size, n_layers=FLAGS.n_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_data(FLAGS.start_string) data = model.sample(FLAGS.max_length, start, converter.vocab_size) # for c in converter.data_to_text(data): # for d in c: # # print(d,end="") # time.sleep(0.5) print(converter.data_to_text(data))
def main(_): converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path = \ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_arr(FLAGS.start_string) # JS/Html/CSS for i in range(0, 1): print('Generating: ' + str(i)) file_path = '../../BrowserFuzzingData/generated/' + FLAGS.file_type + '/' + str( i) + '.' + FLAGS.file_type # f = open(file_path, "x") arr = model.sample(FLAGS.max_length, start, converter.vocab_size) content = converter.arr_to_text(arr) content = content.replace("\\t", "\t") content = content.replace("\\r", "\r") content = content.replace("\\n", "\n") if FLAGS.file_type.__eq__('js'): print(content) # f.write(content) # f.close() elif FLAGS.file_type.__eq__('html'): content = post_process(content) f.write(content) f.close() # TODO: 预留给CSS,暂不作任何处理 else: pass
class Dianpin(Singleton): def __init__(self): self.text = '' self.tfmodel = None self.converter = None def model_built(self):#,vocab_size,sampling,lstm_size,num_layers,use_embedding,embedding_size): FLAGS.start_string = FLAGS.start_string.decode('utf-8') self.converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) self.tfmodel = CharRNN(self.converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) self.tfmodel.load(FLAGS.checkpoint_path) def final_predict(self): start = self.converter.text_to_arr(FLAGS.start_string) arr = self.tfmodel.sample(FLAGS.max_length, start, self.converter.vocab_size) return self.converter.arr_to_text(arr)
def main(_): FLAGS.start_string = FLAGS.start_string converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_arr(FLAGS.start_string) arr = model.predict(FLAGS.max_length, start, converter.vocab_size, 10) for c, p in arr: prediction = converter.arr_to_text(c) prediction = remove_return(prediction) # 如果有中文字生成,请将 {1:^14} 改为 {1:{4}^14} 以修复对齐问题。 # {1:^14}中的 14 随着生成的字符数量而定,一般可以设为字符数+4 print("{0} -> {1:^14} {2} {3}".format(FLAGS.start_string, prediction, "probability:", p, chr(12288)))
def poem_genetate(poem_start=u'君'): #FLAGS.start_string = FLAGS.start_string #FLAGS.start_string = FLAGS.start_string.decode('utf-8') converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =tf.train.latest_checkpoint(FLAGS.checkpoint_path) print FLAGS.checkpoint_path """ model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) """ model = CharRNN(converter.vocab_size, sampling=True, lstm_size=lstm_size, num_layers=num_layers, use_embedding=use_embedding,embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) #start = converter.text_to_arr(start_string) start1 = converter.text_to_arr(poem_start) arr = model.sample(max_length, start1, converter.vocab_size) #pl = model.poemline(max_length, start, converter.vocab_size) #sp=model.sample_hide_poetry( start, converter.vocab_size) poem=converter.arr_to_text(arr) #print (converter.arr_to_text(sp)) print('---------') print(poem) print('---------') #print(converter.arr_to_text(pl)) print('---------') #0:, 1:。 2:\n,每行12个字符。不可以有0,1,2大于1个 lines=poem.split('\n') r_poem=[] for i in range(len(lines)): if len(lines[i])==12: count=0 print lines[i][5] if lines[i][5]==',': print "true" if lines[i][5]==u',': print "u true" if lines[i][5]==u',' and lines[i][11]==u'。': for j in range(len(lines[i])): if lines[i][j]==u',' or lines[i][j]==u'。': count+=1 if count==2: r_poem.append(lines[i]) if len(r_poem)==2: break """ lines=poem.split('\n') r_poem=[] for i in range(len(lines)): if len(lines[i])==12: count=0 if lines[i][5]==0 and lines[i][11]==1: for j in range(len(lines[i])): if lines[i][j]==0 or lines[i][j]==1: count+=1 if count==2: r_poem.append(lines[i]) if len(r_poem)==2: break """ with codecs.open("app/poem.txt","w",'utf-8') as f: words="".join(r_poem) print (lines) print (r_poem) print (words) #words=words.decode('utf-8') f.write(words)