File size: 1,679 Bytes
394bbaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import GPT2LMHeadModel, GPT2Tokenizer, XLNetLMHeadModel, XLNetTokenizer

# Load pre-trained GPT-2 model and tokenizer
gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2")

# Load pre-trained XLNet model and tokenizer
xlnet_tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
xlnet_model = XLNetLMHeadModel.from_pretrained('xlnet-base-cased')

def generate_song_lines_gpt2(style):
    input_text = f"A song in the style of {style}:"
    input_ids = gpt2_tokenizer.encode(input_text, return_tensors='pt')
    # Generate text
    output = gpt2_model.generate(input_ids, do_sample=True, max_length=100, temperature=0.7, num_return_sequences=5)
    # Decode output
    song_lines = [gpt2_tokenizer.decode(ids) for ids in output]

    return song_lines

def generate_song_lines_xlnet(style):
    input_text = f"A song in the style of {style}:"
    input_ids = xlnet_tokenizer.encode(input_text, return_tensors='pt')
    # Generate text
    output = xlnet_model.generate(input_ids, do_sample=True, max_length=100, temperature=0.7, num_return_sequences=5)
    # Decode output
    song_lines = [xlnet_tokenizer.decode(ids) for ids in output]

    return song_lines

def generate_song_gpt2(style):
    song_lines = generate_song_lines_gpt2(style)
    song = "\n".join(song_lines)
    return song

def generate_song_xlnet(style):
    song_lines = generate_song_lines_xlnet(style)
    song = "\n".join(song_lines)
    return song

Artist = "Taylor Swift"

song_gpt2 = generate_song_gpt2(Artist)
song_xlnet = generate_song_xlnet(Artist)

print("GPT-2 Song:\n", song_gpt2)
print("\nXLNet Song:\n", song_xlnet)