forked from albarji/neurowriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
48 lines (38 loc) · 1.89 KB
/
generate.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
# coding: utf-8
# Text generation using a pre-trained model
# Generates a infinite amount of text using a pre-trained model, using the style learned in such model.
import argparse
from keras.models import load_model
from neurowriter.encoding import loadencoding
from neurowriter.writer import Writer
from neurowriter.encoding import END
def generate(modelname, encodername, seed, creativity, maxtokens=None):
"""Generates infinite text using a pre-trained model and a seed text"""
# Load pre-trained encoder
encoder = loadencoding(encodername)
# Load pre-trained model
model = load_model(modelname)
# Text generation
print("Seed:", seed)
writer = Writer(model, encoder, creativity=creativity, batchsize=1, beamsize=1)
print("Generated:")
print(seed, end='')
for i, token in enumerate(writer.generate(seed)):
if maxtokens is not None and i >= maxtokens:
break
print(token, end='')
if token == END:
print('\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generates text following a pre-trained style model.')
parser.add_argument('model', type=str, help='name of the pre-trained model')
parser.add_argument('encoder', type=str, help='name of the pre-trained encoder')
parser.add_argument('--seed', type=str, help='seed to use to initialize the generator. Default: empty string',
default='')
parser.add_argument('--creativity', type=float, help='amount of creativity in the generation. Default: 0.5',
default=0.5)
parser.add_argument('--maxtokens', type=int, help='maximum number of tokens to generate. Default: never stop',
default=None)
args = parser.parse_args()
generate(modelname=args.model, encodername=args.encoder, seed=args.seed, creativity=args.creativity,
maxtokens=args.maxtokens)