chomayouni
The v1 commit
394bbaa
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)