-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
51 lines (33 loc) · 1.58 KB
/
generator.py
File metadata and controls
51 lines (33 loc) · 1.58 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
import argparse
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# #######################
# # GENERATION
# #######################
parser = argparse.ArgumentParser()
def sentence_generation(args):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_dir = args.model_dir
# Load a trained model and vocabulary that you have fine-tuned
model = GPT2LMHeadModel.from_pretrained(model_dir)
tokenizer = GPT2Tokenizer.from_pretrained(model_dir)
model.to(device)
model.eval()
prompt = args.prompt
generated = torch.tensor(tokenizer.encode(prompt)).unsqueeze(0)
generated = generated.to(device)
print(generated)
sample_outputs = model.generate(generated,
# bos_token_id=random.randint(1,30000),
do_sample=True,
top_k=50,
max_length=300,
top_p=0.95,
num_return_sequences=30
)
for i, sample_output in enumerate(sample_outputs):
print("{}: {}\n\n".format(i, tokenizer.decode(sample_output, skip_special_tokens=True)))
parser.add_argument("--model_dir", type=str, default="models/", help="Specify the directory where the model is saved")
parser.add_argument("--prompt", type=str, default="<|startoftext|>", help="Add contextual data for the generator")
args = parser.parse_args()
sentence_generation(args)