Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: ja
|
3 |
+
tags:
|
4 |
+
- ja
|
5 |
+
- japanese
|
6 |
+
- gpt2
|
7 |
+
- text-generation
|
8 |
+
- lm
|
9 |
+
- nlp
|
10 |
+
license: mit
|
11 |
+
widget:
|
12 |
+
- text: "桜"
|
13 |
+
---
|
14 |
+
|
15 |
+
# Japanese GPT2 Lyric Model
|
16 |
+
|
17 |
+
## Model description
|
18 |
+
|
19 |
+
The model is used to generate Japanese lyrics.
|
20 |
+
|
21 |
+
## How to use
|
22 |
+
|
23 |
+
```python
|
24 |
+
import torch
|
25 |
+
from transformers import T5Tokenizer, GPT2LMHeadModel
|
26 |
+
|
27 |
+
tokenizer = T5Tokenizer.from_pretrained("skytnt/gpt2-japanese-lyric-medium")
|
28 |
+
model = GPT2LMHeadModel.from_pretrained("skytnt/gpt2-japanese-lyric-medium")
|
29 |
+
|
30 |
+
|
31 |
+
def gen_lyric(title: str, prompt_text: str):
|
32 |
+
if len(title)!= 0 or len(prompt_text)!= 0:
|
33 |
+
prompt_text = "<s>" + title + "[CLS]" + prompt_text
|
34 |
+
prompt_text = prompt_text.replace("\n", "\\n ")
|
35 |
+
prompt_tokens = tokenizer.tokenize(prompt_text)
|
36 |
+
prompt_token_ids = tokenizer.convert_tokens_to_ids(prompt_tokens)
|
37 |
+
prompt_tensor = torch.LongTensor(prompt_token_ids)
|
38 |
+
prompt_tensor = prompt_tensor.view(1, -1).to(device)
|
39 |
+
else:
|
40 |
+
prompt_tensor = None
|
41 |
+
# model forward
|
42 |
+
output_sequences = model.generate(
|
43 |
+
input_ids=prompt_tensor,
|
44 |
+
max_length=512,
|
45 |
+
top_p=0.95,
|
46 |
+
top_k=40,
|
47 |
+
temperature=1.0,
|
48 |
+
do_sample=True,
|
49 |
+
early_stopping=True,
|
50 |
+
bos_token_id=tokenizer.bos_token_id,
|
51 |
+
eos_token_id=tokenizer.eos_token_id,
|
52 |
+
pad_token_id=tokenizer.pad_token_id,
|
53 |
+
num_return_sequences=1
|
54 |
+
)
|
55 |
+
|
56 |
+
# convert model outputs to readable sentence
|
57 |
+
generated_sequence = output_sequences.tolist()[0]
|
58 |
+
generated_tokens = tokenizer.convert_ids_to_tokens(generated_sequence)
|
59 |
+
generated_text = tokenizer.convert_tokens_to_string(generated_tokens)
|
60 |
+
generated_text = "\n".join([s.strip() for s in generated_text.split('\\n')]).replace(' ', '\u3000').replace('<s>', '').replace('</s>', '\n\n---end---')
|
61 |
+
title_and_lyric = generated_text.split("[CLS]",1)
|
62 |
+
if len(title_and_lyric)==1:
|
63 |
+
title,lyric = "" , title_and_lyric[0].strip()
|
64 |
+
else:
|
65 |
+
title,lyric = title_and_lyric[0].strip(), title_and_lyric[1].strip()
|
66 |
+
return f"---{title}---\n\n{lyric}"
|
67 |
+
|
68 |
+
|
69 |
+
print(gen_lyric("桜",""))
|
70 |
+
|
71 |
+
```
|
72 |
+
|
73 |
+
## Training data
|
74 |
+
|
75 |
+
[Training data](https://github.com/SkyTNT/gpt2-japanese-lyric/blob/main/lyric_ids_titled.pkl) contains 143,587 Japanese lyrics which are collected from [uta-net](https://www.uta-net.com/) by [lyric_download](https://github.com/SkyTNT/lyric_downlowd)
|
76 |
+
|