Spaces:
Running
Running
app.py
CHANGED
|
@@ -13,7 +13,7 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
| 13 |
|
| 14 |
# API URLs and headers
|
| 15 |
AUDIO_API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
| 16 |
-
LYRICS_API_URL = "https://api-inference.huggingface.co/models/
|
| 17 |
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
| 18 |
|
| 19 |
def format_error(message):
|
|
@@ -30,27 +30,15 @@ def create_lyrics_prompt(classification_results):
|
|
| 30 |
# Get additional musical elements
|
| 31 |
additional_elements = [r['label'] for r in classification_results[1:3]]
|
| 32 |
|
| 33 |
-
# Create a
|
| 34 |
-
prompt = f"""Write
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
Secondary Elements: {', '.join(additional_elements)}
|
| 38 |
-
|
| 39 |
-
Requirements:
|
| 40 |
-
1. Create lyrics that strongly reflect the {genre} style
|
| 41 |
-
2. Incorporate elements of {' and '.join(additional_elements)}
|
| 42 |
-
3. Include both verses and a chorus
|
| 43 |
-
4. Match the mood and atmosphere typical of this genre
|
| 44 |
-
5. Use appropriate musical terminology and style
|
| 45 |
-
|
| 46 |
-
Lyrics:
|
| 47 |
-
|
| 48 |
-
[Verse 1]
|
| 49 |
-
"""
|
| 50 |
return prompt
|
| 51 |
|
| 52 |
def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
| 53 |
-
"""Generate lyrics using
|
| 54 |
wait_time = initial_wait
|
| 55 |
|
| 56 |
for attempt in range(max_retries):
|
|
@@ -61,9 +49,9 @@ def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
|
| 61 |
json={
|
| 62 |
"inputs": prompt,
|
| 63 |
"parameters": {
|
| 64 |
-
"max_new_tokens":
|
| 65 |
-
"temperature": 0.
|
| 66 |
-
"top_p": 0.
|
| 67 |
"do_sample": True,
|
| 68 |
"return_full_text": False,
|
| 69 |
"stop": ["[End]", "\n\n\n"]
|
|
@@ -81,10 +69,19 @@ def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
|
| 81 |
# Clean up and format the generated text
|
| 82 |
lines = generated_text.split('\n')
|
| 83 |
cleaned_lines = []
|
|
|
|
|
|
|
| 84 |
for line in lines:
|
| 85 |
line = line.strip()
|
| 86 |
if line and not line.startswith('###') and not line.startswith('```'):
|
|
|
|
|
|
|
| 87 |
cleaned_lines.append(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return "\n".join(cleaned_lines)
|
| 89 |
return "Error: No text generated"
|
| 90 |
elif response.status_code == 503:
|
|
|
|
| 13 |
|
| 14 |
# API URLs and headers
|
| 15 |
AUDIO_API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
| 16 |
+
LYRICS_API_URL = "https://api-inference.huggingface.co/models/gpt2-xl"
|
| 17 |
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
| 18 |
|
| 19 |
def format_error(message):
|
|
|
|
| 30 |
# Get additional musical elements
|
| 31 |
additional_elements = [r['label'] for r in classification_results[1:3]]
|
| 32 |
|
| 33 |
+
# Create a more focused prompt for GPT2-XL
|
| 34 |
+
prompt = f"""Write song lyrics in the style of {genre}.
|
| 35 |
+
Theme: A {genre} song with elements of {' and '.join(additional_elements)}
|
| 36 |
|
| 37 |
+
[Verse 1]"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return prompt
|
| 39 |
|
| 40 |
def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
| 41 |
+
"""Generate lyrics using GPT2-XL with retry logic"""
|
| 42 |
wait_time = initial_wait
|
| 43 |
|
| 44 |
for attempt in range(max_retries):
|
|
|
|
| 49 |
json={
|
| 50 |
"inputs": prompt,
|
| 51 |
"parameters": {
|
| 52 |
+
"max_new_tokens": 200,
|
| 53 |
+
"temperature": 0.9,
|
| 54 |
+
"top_p": 0.95,
|
| 55 |
"do_sample": True,
|
| 56 |
"return_full_text": False,
|
| 57 |
"stop": ["[End]", "\n\n\n"]
|
|
|
|
| 69 |
# Clean up and format the generated text
|
| 70 |
lines = generated_text.split('\n')
|
| 71 |
cleaned_lines = []
|
| 72 |
+
current_section = "[Verse 1]"
|
| 73 |
+
|
| 74 |
for line in lines:
|
| 75 |
line = line.strip()
|
| 76 |
if line and not line.startswith('###') and not line.startswith('```'):
|
| 77 |
+
if line.lower().startswith('[verse') or line.lower().startswith('[chorus'):
|
| 78 |
+
current_section = line
|
| 79 |
cleaned_lines.append(line)
|
| 80 |
+
|
| 81 |
+
# Add chorus after first verse if not present
|
| 82 |
+
if len(cleaned_lines) == 4 and current_section == "[Verse 1]":
|
| 83 |
+
cleaned_lines.append("\n[Chorus]")
|
| 84 |
+
|
| 85 |
return "\n".join(cleaned_lines)
|
| 86 |
return "Error: No text generated"
|
| 87 |
elif response.status_code == 503:
|