-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.py
32 lines (26 loc) · 894 Bytes
/
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
import torch
from utils import sample_sequence
from transformers import GPT2LMHeadModel, GPT2Tokenizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_class, tokenizer_class = (GPT2LMHeadModel, GPT2Tokenizer)
tokenizer = tokenizer_class.from_pretrained('output')
model = model_class.from_pretrained('output')
model.to(device)
model.eval()
def generateHoro(raw_text):
context_tokens = tokenizer.encode(raw_text, add_special_tokens=False)
out = sample_sequence(
model=model,
context=context_tokens,
num_samples=1,
length=50,
device=device)
out = out[:, len(context_tokens):].tolist()[0]
text = raw_text + tokenizer.decode(out, clean_up_tokenization_spaces=True)
text = text.replace('\n', '').replace('\xa0', '')
return text
def main():
text = 'today '
print(generateHoro(text))
if __name__ == '__main__':
main()