Spaces:
Running
Running
File size: 5,107 Bytes
ed7741a a19410a ed7741a 8599ceb ed7741a |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
from transformers import pipeline
import torch
from typing import Dict, List, Optional
class LyricGenerator:
def __init__(self, model_name: str = "gpt2-medium"):
"""
Initialize the lyric generator with a specified language model.
Args:
model_name: The name of the pre-trained model to use
"""
try:
# Try to use CUDA if available
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
self.generator = pipeline(
"text-generation",
model=model_name,
device_map="auto" # Let transformers handle device mapping
)
except Exception as e:
print(f"Warning: GPU initialization failed, falling back to CPU. Error: {str(e)}")
self.generator = pipeline(
"text-generation",
model=model_name,
device="cpu"
)
# Genre-specific prompts to guide generation
self.genre_prompts = {
"rock": "Write energetic rock lyrics about",
"pop": "Create catchy pop lyrics about",
"hip hop": "Write hip hop verses about",
"country": "Write country music lyrics about",
"jazz": "Compose smooth jazz lyrics about",
"classical": "Write classical music lyrics about",
"electronic": "Create electronic dance music lyrics about",
"blues": "Write soulful blues lyrics about",
"reggae": "Write laid-back reggae lyrics about",
"metal": "Write intense metal lyrics about"
}
def generate_lyrics(
self,
genre: str,
theme: str,
max_length: int = 200,
num_return_sequences: int = 1,
temperature: float = 0.9,
top_p: float = 0.9,
top_k: int = 50
) -> List[str]:
"""
Generate lyrics based on genre and theme.
Args:
genre: The music genre to generate lyrics for
theme: The theme or topic for the lyrics
max_length: Maximum length of generated text
num_return_sequences: Number of different lyrics to generate
temperature: Controls randomness (higher = more random)
top_p: Nucleus sampling parameter
top_k: Top-k sampling parameter
Returns:
List of generated lyrics
"""
try:
# Get genre-specific prompt or use default
genre = genre.lower()
base_prompt = self.genre_prompts.get(
genre,
"Write song lyrics about"
)
# Construct full prompt
prompt = f"{base_prompt} {theme}:\n\n"
# Generate lyrics
outputs = self.generator(
prompt,
max_length=max_length,
num_return_sequences=num_return_sequences,
temperature=temperature,
top_p=top_p,
top_k=top_k,
do_sample=True,
pad_token_id=50256 # GPT-2's pad token ID
)
# Process and clean up the generated texts
generated_lyrics = []
for output in outputs:
# Remove the prompt from the generated text
lyrics = output['generated_text'][len(prompt):].strip()
# Basic cleanup
lyrics = lyrics.replace('<|endoftext|>', '').strip()
generated_lyrics.append(lyrics)
return generated_lyrics
except Exception as e:
raise ValueError(f"Lyric generation failed: {str(e)}")
def style_transfer(
self,
original_lyrics: str,
target_genre: str,
temperature: float = 0.9
) -> str:
"""
Attempt to transfer the style of existing lyrics to a target genre.
Args:
original_lyrics: The original lyrics to restyle
target_genre: The target genre for the style transfer
temperature: Controls randomness of generation
Returns:
Restyled lyrics in the target genre
"""
try:
prompt = f"Rewrite these lyrics in {target_genre} style:\n\n{original_lyrics}\n\nNew version:\n"
output = self.generator(
prompt,
max_length=len(prompt) + 200,
temperature=temperature,
top_p=0.9,
do_sample=True,
num_return_sequences=1
)[0]
# Extract the new version only
generated_text = output['generated_text']
new_lyrics = generated_text.split("New version:\n")[-1].strip()
return new_lyrics
except Exception as e:
raise ValueError(f"Style transfer failed: {str(e)}") |