Spaces:
Running
Running
app.py
CHANGED
@@ -14,7 +14,7 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
14 |
|
15 |
# API URLs and headers
|
16 |
AUDIO_API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
17 |
-
LYRICS_API_URL = "https://api-inference.huggingface.co/models/gpt2-
|
18 |
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
19 |
|
20 |
def get_audio_duration(audio_path):
|
@@ -62,19 +62,22 @@ def create_lyrics_prompt(classification_results, song_structure):
|
|
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 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
- Write in proper song lyric format
|
74 |
-
- Focus on musical rhythm and flow
|
75 |
-
- Include rhyming where appropriate
|
76 |
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
[Verse 1]"""
|
80 |
return prompt
|
@@ -137,7 +140,7 @@ def format_lyrics(generated_text, song_structure):
|
|
137 |
return "\n".join(lines)
|
138 |
|
139 |
def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wait=2):
|
140 |
-
"""Generate lyrics using GPT2-
|
141 |
wait_time = initial_wait
|
142 |
|
143 |
for attempt in range(max_retries):
|
@@ -149,13 +152,13 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
|
|
149 |
"inputs": prompt,
|
150 |
"parameters": {
|
151 |
"max_new_tokens": song_structure['tokens'],
|
152 |
-
"temperature": 0.
|
153 |
-
"top_p": 0.
|
154 |
"do_sample": True,
|
155 |
"return_full_text": False,
|
156 |
-
"repetition_penalty": 1.
|
157 |
-
"presence_penalty": 0.
|
158 |
-
"frequency_penalty": 0.
|
159 |
}
|
160 |
}
|
161 |
)
|
@@ -173,8 +176,8 @@ def generate_lyrics_with_retry(prompt, song_structure, max_retries=5, initial_wa
|
|
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 |
-
#
|
177 |
-
if len(content_lines) < 4 or any(len(line.split()) >
|
178 |
if attempt < max_retries - 1:
|
179 |
print("Generated text doesn't look like lyrics, retrying...")
|
180 |
continue
|
|
|
14 |
|
15 |
# API URLs and headers
|
16 |
AUDIO_API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
17 |
+
LYRICS_API_URL = "https://api-inference.huggingface.co/models/gpt2-medium"
|
18 |
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
19 |
|
20 |
def get_audio_duration(audio_path):
|
|
|
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 simpler prompt with example structure
|
66 |
+
prompt = f"""Here's a {main_style} song with {', '.join(secondary_elements)} elements:
|
67 |
|
68 |
+
[Verse 1]
|
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 |
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-Medium with retry logic"""
|
144 |
wait_time = initial_wait
|
145 |
|
146 |
for attempt in range(max_retries):
|
|
|
152 |
"inputs": prompt,
|
153 |
"parameters": {
|
154 |
"max_new_tokens": song_structure['tokens'],
|
155 |
+
"temperature": 0.7, # Lower temperature for more focused output
|
156 |
+
"top_p": 0.85,
|
157 |
"do_sample": True,
|
158 |
"return_full_text": False,
|
159 |
+
"repetition_penalty": 1.1, # Reduced repetition penalty
|
160 |
+
"presence_penalty": 0.3,
|
161 |
+
"frequency_penalty": 0.3
|
162 |
}
|
163 |
}
|
164 |
)
|
|
|
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 |
+
# More lenient line length check
|
180 |
+
if len(content_lines) < 4 or any(len(line.split()) > 20 for line in content_lines):
|
181 |
if attempt < max_retries - 1:
|
182 |
print("Generated text doesn't look like lyrics, retrying...")
|
183 |
continue
|