Spaces:
Running
Running
app.py
CHANGED
|
@@ -59,25 +59,12 @@ def create_lyrics_prompt(classification_results, song_structure):
|
|
| 59 |
"""Create a prompt for lyrics generation based on classification results and desired structure"""
|
| 60 |
# Get the top genres and characteristics
|
| 61 |
main_style = classification_results[0]['label']
|
| 62 |
-
main_confidence = float(classification_results[0]['score'].strip('%'))
|
| 63 |
secondary_elements = [result['label'] for result in classification_results[1:3]]
|
| 64 |
|
| 65 |
-
# Create a
|
| 66 |
-
prompt = f"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
The melody rings through the air tonight
|
| 70 |
-
Like a gentle whisper in the light
|
| 71 |
-
Every note tells a story so clear
|
| 72 |
-
Creating magic for all to hear
|
| 73 |
-
|
| 74 |
-
[Chorus]
|
| 75 |
-
Let the rhythm flow and shine
|
| 76 |
-
Feel the music so divine
|
| 77 |
-
Every moment, every sound
|
| 78 |
-
Brings the joy that we have found
|
| 79 |
-
|
| 80 |
-
Now continue with your own lyrics in this style:
|
| 81 |
|
| 82 |
[Verse 1]"""
|
| 83 |
return prompt
|
|
@@ -140,49 +127,62 @@ def format_lyrics(generated_text, song_structure):
|
|
| 140 |
return "\n".join(lines)
|
| 141 |
|
| 142 |
def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wait=2):
|
| 143 |
-
"""Generate lyrics using GPT2
|
| 144 |
wait_time = initial_wait
|
| 145 |
|
| 146 |
for attempt in range(max_retries):
|
| 147 |
try:
|
| 148 |
response = requests.post(
|
| 149 |
-
|
| 150 |
headers=headers,
|
| 151 |
json={
|
| 152 |
"inputs": prompt,
|
| 153 |
"parameters": {
|
| 154 |
-
"max_new_tokens":
|
| 155 |
-
"temperature": 0.
|
| 156 |
-
"
|
| 157 |
"do_sample": True,
|
| 158 |
"return_full_text": False,
|
| 159 |
-
"
|
| 160 |
-
"presence_penalty": 0.3,
|
| 161 |
-
"frequency_penalty": 0.3
|
| 162 |
}
|
| 163 |
}
|
| 164 |
)
|
| 165 |
|
| 166 |
if response.status_code == 200:
|
| 167 |
-
|
| 168 |
-
if isinstance(
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
if not generated_text:
|
| 171 |
continue
|
| 172 |
-
|
| 173 |
-
formatted_lyrics = format_lyrics(generated_text, song_structure)
|
| 174 |
-
|
| 175 |
-
# Verify we have actual content and it looks like lyrics
|
| 176 |
-
content_lines = [l for l in formatted_lyrics.split('\n')
|
| 177 |
-
if l.strip() and not l.strip().startswith('[') and l.strip() != '...']
|
| 178 |
|
| 179 |
-
#
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
print("Generated text doesn't look like lyrics, retrying...")
|
| 183 |
-
continue
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
elif response.status_code == 503:
|
| 188 |
print(f"Model loading, attempt {attempt + 1}/{max_retries}. Waiting {wait_time} seconds...")
|
|
|
|
| 59 |
"""Create a prompt for lyrics generation based on classification results and desired structure"""
|
| 60 |
# Get the top genres and characteristics
|
| 61 |
main_style = classification_results[0]['label']
|
|
|
|
| 62 |
secondary_elements = [result['label'] for result in classification_results[1:3]]
|
| 63 |
|
| 64 |
+
# Create a focused prompt for lyrics generation
|
| 65 |
+
prompt = f"""Write lyrics for a song.
|
| 66 |
+
Genre: {main_style}
|
| 67 |
+
Musical elements: {', '.join(secondary_elements)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
[Verse 1]"""
|
| 70 |
return prompt
|
|
|
|
| 127 |
return "\n".join(lines)
|
| 128 |
|
| 129 |
def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wait=2):
|
| 130 |
+
"""Generate lyrics using GPT2 with retry logic"""
|
| 131 |
wait_time = initial_wait
|
| 132 |
|
| 133 |
for attempt in range(max_retries):
|
| 134 |
try:
|
| 135 |
response = requests.post(
|
| 136 |
+
"https://api-inference.huggingface.co/models/gpt2", # Using base GPT-2
|
| 137 |
headers=headers,
|
| 138 |
json={
|
| 139 |
"inputs": prompt,
|
| 140 |
"parameters": {
|
| 141 |
+
"max_new_tokens": 150, # Shorter output
|
| 142 |
+
"temperature": 0.9, # More creative
|
| 143 |
+
"top_k": 50, # Standard top-k sampling
|
| 144 |
"do_sample": True,
|
| 145 |
"return_full_text": False,
|
| 146 |
+
"num_return_sequences": 3 # Generate multiple sequences
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
}
|
| 149 |
)
|
| 150 |
|
| 151 |
if response.status_code == 200:
|
| 152 |
+
results = response.json()
|
| 153 |
+
if not isinstance(results, list) or not results:
|
| 154 |
+
continue
|
| 155 |
+
|
| 156 |
+
# Try each generated sequence
|
| 157 |
+
for result in results:
|
| 158 |
+
generated_text = result.get("generated_text", "")
|
| 159 |
if not generated_text:
|
| 160 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
# Basic cleaning
|
| 163 |
+
lines = [line.strip() for line in generated_text.split('\n')
|
| 164 |
+
if line.strip() and not line.strip().startswith(('[', '#', '`'))]
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
# Check if we have enough content
|
| 167 |
+
if len(lines) >= 4:
|
| 168 |
+
# Format into verses and chorus
|
| 169 |
+
formatted_lyrics = []
|
| 170 |
+
formatted_lyrics.append("[Verse 1]")
|
| 171 |
+
formatted_lyrics.extend(lines[:4])
|
| 172 |
+
|
| 173 |
+
if song_structure['choruses'] > 0:
|
| 174 |
+
formatted_lyrics.append("\n[Chorus 1]")
|
| 175 |
+
formatted_lyrics.extend(lines[4:8] if len(lines) >= 8 else ["..." for _ in range(4)])
|
| 176 |
+
|
| 177 |
+
if song_structure['verses'] > 1:
|
| 178 |
+
formatted_lyrics.append("\n[Verse 2]")
|
| 179 |
+
formatted_lyrics.extend(lines[8:12] if len(lines) >= 12 else ["..." for _ in range(4)])
|
| 180 |
+
|
| 181 |
+
return "\n".join(formatted_lyrics)
|
| 182 |
+
|
| 183 |
+
# If we get here, none of the sequences were good enough
|
| 184 |
+
if attempt < max_retries - 1:
|
| 185 |
+
continue
|
| 186 |
|
| 187 |
elif response.status_code == 503:
|
| 188 |
print(f"Model loading, attempt {attempt + 1}/{max_retries}. Waiting {wait_time} seconds...")
|