Spaces:
Running
Running
app.py
CHANGED
@@ -58,18 +58,25 @@ def calculate_song_structure(duration):
|
|
58 |
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 |
-
|
|
|
|
|
62 |
|
63 |
-
# Create a
|
64 |
-
prompt = f"""Write a
|
65 |
-
Main style: {genres[0]}
|
66 |
-
Additional elements: {', '.join(genres[1:])}
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
|
|
73 |
return prompt
|
74 |
|
75 |
def format_lyrics(generated_text, song_structure):
|
@@ -142,11 +149,13 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
|
|
142 |
"inputs": prompt,
|
143 |
"parameters": {
|
144 |
"max_new_tokens": song_structure['tokens'],
|
145 |
-
"temperature": 0.
|
146 |
-
"top_p": 0.
|
147 |
"do_sample": True,
|
148 |
"return_full_text": False,
|
149 |
-
"repetition_penalty": 1.2
|
|
|
|
|
150 |
}
|
151 |
}
|
152 |
)
|
@@ -160,12 +169,14 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
|
|
160 |
|
161 |
formatted_lyrics = format_lyrics(generated_text, song_structure)
|
162 |
|
163 |
-
# Verify we have actual content
|
164 |
content_lines = [l for l in formatted_lyrics.split('\n')
|
165 |
if l.strip() and not l.strip().startswith('[') and l.strip() != '...']
|
166 |
-
|
|
|
|
|
167 |
if attempt < max_retries - 1:
|
168 |
-
print("
|
169 |
continue
|
170 |
|
171 |
return formatted_lyrics
|
|
|
58 |
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 more specific songwriting prompt
|
66 |
+
prompt = f"""Write a song in the style of {main_style} music.
|
|
|
|
|
67 |
|
68 |
+
Style Guide:
|
69 |
+
- Primary style: {main_style} ({main_confidence:.1f}% confidence)
|
70 |
+
- Incorporate elements of: {', '.join(secondary_elements)}
|
71 |
+
- Structure: {song_structure['verses']} verses and {song_structure['choruses']} choruses
|
72 |
+
- Each verse and chorus should be exactly 4 lines
|
73 |
+
- Write in proper song lyric format
|
74 |
+
- Focus on musical rhythm and flow
|
75 |
+
- Include rhyming where appropriate
|
76 |
|
77 |
+
Start with the first verse:
|
78 |
+
|
79 |
+
[Verse 1]"""
|
80 |
return prompt
|
81 |
|
82 |
def format_lyrics(generated_text, song_structure):
|
|
|
149 |
"inputs": prompt,
|
150 |
"parameters": {
|
151 |
"max_new_tokens": song_structure['tokens'],
|
152 |
+
"temperature": 0.85, # Balanced between creativity and coherence
|
153 |
+
"top_p": 0.92,
|
154 |
"do_sample": True,
|
155 |
"return_full_text": False,
|
156 |
+
"repetition_penalty": 1.2,
|
157 |
+
"presence_penalty": 0.5, # Encourage diverse vocabulary
|
158 |
+
"frequency_penalty": 0.5 # Discourage repetitive phrases
|
159 |
}
|
160 |
}
|
161 |
)
|
|
|
169 |
|
170 |
formatted_lyrics = format_lyrics(generated_text, song_structure)
|
171 |
|
172 |
+
# Verify we have actual content and it looks like lyrics
|
173 |
content_lines = [l for l in formatted_lyrics.split('\n')
|
174 |
if l.strip() and not l.strip().startswith('[') and l.strip() != '...']
|
175 |
+
|
176 |
+
# Check if the content looks like lyrics (short lines, not prose)
|
177 |
+
if len(content_lines) < 4 or any(len(line.split()) > 15 for line in content_lines):
|
178 |
if attempt < max_retries - 1:
|
179 |
+
print("Generated text doesn't look like lyrics, retrying...")
|
180 |
continue
|
181 |
|
182 |
return formatted_lyrics
|