Spaces:
Running
Running
app.py
CHANGED
@@ -65,13 +65,20 @@ def create_lyrics_prompt(classification_results, song_structure):
|
|
65 |
# Get additional musical elements
|
66 |
additional_elements = [r['label'] for r in classification_results[1:3]]
|
67 |
|
68 |
-
# Create a
|
69 |
-
prompt = f"""Write song
|
|
|
|
|
70 |
Theme: A {genre} song with elements of {' and '.join(additional_elements)}
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
Make each verse 4-6 lines and chorus 4 lines.
|
75 |
|
76 |
[Verse 1]"""
|
77 |
return prompt
|
@@ -80,40 +87,71 @@ def format_lyrics(generated_text, song_structure):
|
|
80 |
"""Format the generated lyrics according to desired structure"""
|
81 |
lines = generated_text.split('\n')
|
82 |
cleaned_lines = []
|
83 |
-
current_section =
|
84 |
verse_count = 0
|
85 |
chorus_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
for line in lines:
|
88 |
line = line.strip()
|
89 |
if not line or line.startswith('###') or line.startswith('```'):
|
90 |
continue
|
91 |
|
92 |
-
#
|
93 |
-
if line.lower().startswith('[
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
continue
|
99 |
-
elif line.lower().startswith('[chorus'):
|
100 |
-
if chorus_count < song_structure['choruses']:
|
101 |
-
chorus_count += 1
|
102 |
-
current_section = f"[Chorus {chorus_count}]"
|
103 |
-
cleaned_lines.append(f"\n{current_section}")
|
104 |
-
continue
|
105 |
-
|
106 |
-
# Add the line if we haven't exceeded our structure limits
|
107 |
-
if (current_section.startswith('[Verse') and verse_count <= song_structure['verses']) or \
|
108 |
-
(current_section.startswith('[Chorus') and chorus_count <= song_structure['choruses']):
|
109 |
cleaned_lines.append(line)
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
return "\n".join(
|
117 |
|
118 |
def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wait=2):
|
119 |
"""Generate lyrics using GPT2-XL with retry logic"""
|
@@ -138,18 +176,25 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
|
|
138 |
)
|
139 |
|
140 |
print(f"Response status: {response.status_code}")
|
141 |
-
print(f"Response content: {response.content.decode('utf-8', errors='ignore')}")
|
142 |
|
143 |
if response.status_code == 200:
|
144 |
result = response.json()
|
145 |
if isinstance(result, list) and len(result) > 0:
|
146 |
generated_text = result[0].get("generated_text", "")
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
return "Error: No text generated"
|
149 |
elif response.status_code == 503:
|
150 |
print(f"Model loading, attempt {attempt + 1}/{max_retries}. Waiting {wait_time} seconds...")
|
151 |
time.sleep(wait_time)
|
152 |
-
wait_time *= 1.5
|
153 |
continue
|
154 |
else:
|
155 |
return f"Error generating lyrics: {response.text}"
|
|
|
65 |
# Get additional musical elements
|
66 |
additional_elements = [r['label'] for r in classification_results[1:3]]
|
67 |
|
68 |
+
# Create a more specific and structured prompt
|
69 |
+
prompt = f"""Write a song with the following structure:
|
70 |
+
|
71 |
+
Style: {genre} music
|
72 |
Theme: A {genre} song with elements of {' and '.join(additional_elements)}
|
73 |
+
Length: {song_structure['verses']} verses and {song_structure['choruses']} choruses
|
74 |
+
|
75 |
+
Guidelines:
|
76 |
+
- Each verse should be exactly 4 lines
|
77 |
+
- Each chorus should be exactly 4 lines
|
78 |
+
- Keep the lyrics matching the {genre} style
|
79 |
+
- Use appropriate musical themes and imagery
|
80 |
|
81 |
+
Start with Verse 1:
|
|
|
82 |
|
83 |
[Verse 1]"""
|
84 |
return prompt
|
|
|
87 |
"""Format the generated lyrics according to desired structure"""
|
88 |
lines = generated_text.split('\n')
|
89 |
cleaned_lines = []
|
90 |
+
current_section = None
|
91 |
verse_count = 0
|
92 |
chorus_count = 0
|
93 |
+
lines_in_section = 0
|
94 |
+
|
95 |
+
# Add first verse marker
|
96 |
+
cleaned_lines.append("[Verse 1]")
|
97 |
+
current_section = "verse"
|
98 |
+
verse_count = 1
|
99 |
|
100 |
for line in lines:
|
101 |
line = line.strip()
|
102 |
if not line or line.startswith('###') or line.startswith('```'):
|
103 |
continue
|
104 |
|
105 |
+
# Skip section markers in the generated text
|
106 |
+
if line.lower().startswith('['):
|
107 |
+
continue
|
108 |
+
|
109 |
+
# Add the line if it's not a marker
|
110 |
+
if len(line) > 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
cleaned_lines.append(line)
|
112 |
+
lines_in_section += 1
|
113 |
+
|
114 |
+
# Check if we need to start a new section
|
115 |
+
if lines_in_section >= 4: # After 4 lines in current section
|
116 |
+
lines_in_section = 0
|
117 |
+
|
118 |
+
# Determine next section
|
119 |
+
if current_section == "verse" and chorus_count < song_structure['choruses']:
|
120 |
+
# Add a chorus after verse
|
121 |
+
chorus_count += 1
|
122 |
+
cleaned_lines.append(f"\n[Chorus {chorus_count}]")
|
123 |
+
current_section = "chorus"
|
124 |
+
elif current_section == "chorus" and verse_count < song_structure['verses']:
|
125 |
+
# Add next verse after chorus
|
126 |
+
verse_count += 1
|
127 |
+
cleaned_lines.append(f"\n[Verse {verse_count}]")
|
128 |
+
current_section = "verse"
|
129 |
+
|
130 |
+
# Ensure we have complete sections
|
131 |
+
result = []
|
132 |
+
current_section = None
|
133 |
+
section_lines = []
|
134 |
+
|
135 |
+
for line in cleaned_lines:
|
136 |
+
if line.startswith('['):
|
137 |
+
if current_section and section_lines:
|
138 |
+
# Pad section to 4 lines if needed
|
139 |
+
while len(section_lines) < 4:
|
140 |
+
section_lines.append("...")
|
141 |
+
result.extend(section_lines)
|
142 |
+
current_section = line
|
143 |
+
result.append(f"\n{line}")
|
144 |
+
section_lines = []
|
145 |
+
else:
|
146 |
+
section_lines.append(line)
|
147 |
+
|
148 |
+
# Add the last section
|
149 |
+
if section_lines:
|
150 |
+
while len(section_lines) < 4:
|
151 |
+
section_lines.append("...")
|
152 |
+
result.extend(section_lines)
|
153 |
|
154 |
+
return "\n".join(result)
|
155 |
|
156 |
def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wait=2):
|
157 |
"""Generate lyrics using GPT2-XL with retry logic"""
|
|
|
176 |
)
|
177 |
|
178 |
print(f"Response status: {response.status_code}")
|
|
|
179 |
|
180 |
if response.status_code == 200:
|
181 |
result = response.json()
|
182 |
if isinstance(result, list) and len(result) > 0:
|
183 |
generated_text = result[0].get("generated_text", "")
|
184 |
+
formatted_lyrics = format_lyrics(generated_text, song_structure)
|
185 |
+
|
186 |
+
# Verify the formatting worked correctly
|
187 |
+
if formatted_lyrics.count('[Verse') < 1 or '>' in formatted_lyrics:
|
188 |
+
# If formatting failed, try again
|
189 |
+
if attempt < max_retries - 1:
|
190 |
+
print("Malformed lyrics, retrying...")
|
191 |
+
continue
|
192 |
+
return formatted_lyrics
|
193 |
return "Error: No text generated"
|
194 |
elif response.status_code == 503:
|
195 |
print(f"Model loading, attempt {attempt + 1}/{max_retries}. Waiting {wait_time} seconds...")
|
196 |
time.sleep(wait_time)
|
197 |
+
wait_time *= 1.5
|
198 |
continue
|
199 |
else:
|
200 |
return f"Error generating lyrics: {response.text}"
|