jacob-c commited on
Commit
b893150
·
1 Parent(s): 0c45734
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -61,16 +61,20 @@ def create_lyrics_prompt(classification_results, song_structure):
61
  main_style = classification_results[0]['label']
62
  secondary_elements = [result['label'] for result in classification_results[1:3]]
63
 
64
- # Create a more specific prompt for song lyrics
65
- prompt = f"""Write a short song with musical elements.
66
 
67
- Style: {main_style}
68
- Elements: {', '.join(secondary_elements)}
69
-
70
- Start with the first verse:
71
 
72
  [Verse 1]
73
- """
 
 
 
 
 
 
 
74
  return prompt
75
 
76
  def format_lyrics(generated_text, song_structure):
@@ -137,50 +141,44 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
137
  for attempt in range(max_retries):
138
  try:
139
  print(f"\nAttempt {attempt + 1}: Generating lyrics...")
140
- print(f"Using prompt:\n{prompt}")
141
 
142
  response = requests.post(
143
- "https://api-inference.huggingface.co/models/distilgpt2", # Using DistilGPT2 for more reliable generation
144
  headers=headers,
145
  json={
146
  "inputs": prompt,
147
  "parameters": {
148
- "max_new_tokens": 100, # Shorter output for more focused generation
149
- "temperature": 0.8,
150
- "top_p": 0.9,
151
  "do_sample": True,
152
  "return_full_text": False,
153
- "num_return_sequences": 1, # Single sequence for now
154
- "stop": ["\n\n", "[End]"]
155
  }
156
  }
157
  )
158
 
159
- print(f"Response status: {response.status_code}")
160
-
161
  if response.status_code == 200:
162
  try:
163
  result = response.json()
164
- print(f"Raw response: {result}")
165
-
166
  if isinstance(result, list) and len(result) > 0:
167
  generated_text = result[0].get("generated_text", "")
168
  if not generated_text:
169
- print("No text generated, retrying...")
170
  continue
171
 
172
- # Clean up the generated text
173
  lines = []
174
  current_lines = []
175
 
176
  for line in generated_text.split('\n'):
177
  line = line.strip()
178
- if not line or line.startswith(('```', '###', '[')):
 
179
  continue
180
- if len(line.split()) <= 15: # Only include reasonably-sized lines
 
181
  current_lines.append(line)
182
- if len(current_lines) >= 4:
183
- break
184
 
185
  if len(current_lines) >= 4:
186
  # Format into song structure
@@ -188,19 +186,23 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
188
  lines.extend(current_lines[:4])
189
 
190
  if song_structure['choruses'] > 0:
 
 
 
 
 
 
191
  lines.append("\n[Chorus]")
192
- lines.extend([
193
- "Let the music play",
194
- "In our special way",
195
- "Feel the rhythm flow",
196
- "As the melodies grow"
197
- ])
198
 
199
  return "\n".join(lines)
200
  else:
201
  print(f"Not enough valid lines generated (got {len(current_lines)}), retrying...")
202
- else:
203
- print("Invalid response format, retrying...")
204
  except Exception as e:
205
  print(f"Error processing response: {str(e)}")
206
  if attempt < max_retries - 1:
 
61
  main_style = classification_results[0]['label']
62
  secondary_elements = [result['label'] for result in classification_results[1:3]]
63
 
64
+ # Create a more specific prompt with example lyrics
65
+ prompt = f"""Create song lyrics in {main_style} style with {', '.join(secondary_elements)} elements.
66
 
67
+ Here's an example of the lyric style:
 
 
 
68
 
69
  [Verse 1]
70
+ Gentle bells ring in the night
71
+ Stars are shining pure and bright
72
+ Music fills the evening air
73
+ Magic moments we can share
74
+
75
+ Write new original lyrics following this style:
76
+
77
+ [Verse 1]"""
78
  return prompt
79
 
80
  def format_lyrics(generated_text, song_structure):
 
141
  for attempt in range(max_retries):
142
  try:
143
  print(f"\nAttempt {attempt + 1}: Generating lyrics...")
 
144
 
145
  response = requests.post(
146
+ "https://api-inference.huggingface.co/models/distilgpt2",
147
  headers=headers,
148
  json={
149
  "inputs": prompt,
150
  "parameters": {
151
+ "max_new_tokens": 200,
152
+ "temperature": 0.7, # Lower temperature for more coherent output
153
+ "top_p": 0.85,
154
  "do_sample": True,
155
  "return_full_text": False,
156
+ "num_return_sequences": 1,
157
+ "repetition_penalty": 1.2
158
  }
159
  }
160
  )
161
 
 
 
162
  if response.status_code == 200:
163
  try:
164
  result = response.json()
 
 
165
  if isinstance(result, list) and len(result) > 0:
166
  generated_text = result[0].get("generated_text", "")
167
  if not generated_text:
 
168
  continue
169
 
170
+ # Clean up and format the text
171
  lines = []
172
  current_lines = []
173
 
174
  for line in generated_text.split('\n'):
175
  line = line.strip()
176
+ # Skip empty lines, section markers, and non-lyric content
177
+ if not line or line.startswith(('```', '###', '[', 'Start', 'Write')):
178
  continue
179
+ # Only include lines that look like lyrics (not too long, no punctuation at start)
180
+ if len(line.split()) <= 12 and not line[0] in '.,!?':
181
  current_lines.append(line)
 
 
182
 
183
  if len(current_lines) >= 4:
184
  # Format into song structure
 
186
  lines.extend(current_lines[:4])
187
 
188
  if song_structure['choruses'] > 0:
189
+ chorus_lines = [
190
+ "Hear the music in the air",
191
+ "Feel the rhythm everywhere",
192
+ "Let the melody take flight",
193
+ "As we sing into the night"
194
+ ]
195
  lines.append("\n[Chorus]")
196
+ lines.extend(chorus_lines)
197
+
198
+ if song_structure['verses'] > 1 and len(current_lines) >= 8:
199
+ lines.append("\n[Verse 2]")
200
+ lines.extend(current_lines[4:8])
 
201
 
202
  return "\n".join(lines)
203
  else:
204
  print(f"Not enough valid lines generated (got {len(current_lines)}), retrying...")
205
+
 
206
  except Exception as e:
207
  print(f"Error processing response: {str(e)}")
208
  if attempt < max_retries - 1: