if last or len(generated) > args.max_length:
            if len(generated) > 0:
                return generated[:args.max_length]
        else:
            pre_text = generated[-256:]

config = tf.ConfigProto()
if int(args.gpu) >= 0:
    config.gpu_options.allow_growth = True
    config.gpu_options.visible_device_list = args.gpu
with tf.Session(config=config,graph=tf.Graph()) as sess:
    context = tf.placeholder(tf.int32, [1, None])
    output = sample_sequence(
        hparams=hparams, length=length,
        context=context,
        batch_size=1,
        temperature=temperature, top_k=top_k, top_p=top_p
    )

    saver = tf.train.Saver()
    ckpt = tf.train.latest_checkpoint(args.model)
    saver.restore(sess, ckpt)

    if len(args.output_file) > 0:
        with open(args.output_file, 'w') as of:
            for i in range(args.num_generate):
                of.write(generate_one(sess, output)+'\n')
                if i < args.num_generate-1:
                    of.write('========\n')
    else:
        for i in range(args.num_generate):
    n_contexts = hparams.n_ctx - n_prediction
    if n_prediction <= 0:
        raise ValueError(
            'invalid hparams.json or no --max_answer_len argument.')

    config = tf.ConfigProto()
    if int(args.gpu) >= 0:
        config.gpu_options.allow_growth = True
        config.gpu_options.visible_device_list = args.gpu
    with tf.Session(config=config) as sess:
        context = tf.placeholder(tf.int32, [1, None])
        output = sample_sequence(hparams=hparams,
                                 length=hparams.n_ctx,
                                 min_length=min_answer_len,
                                 context=context,
                                 batch_size=1,
                                 temperature=temperature,
                                 top_k=top_k,
                                 top_p=top_p)

        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(args.model)
        saver.restore(sess, ckpt)

        rouge = []
        for curDir, dirs, files in os.walk(args.src_dir):
            for file in tqdm(files):
                if file.endswith(".txt"):
                    input = os.path.join(curDir, file)
                    with open(input, 'r', encoding='utf-8') as fp:
                        raw_text = fp.read()