Spaces:
Running
Running
app.py
CHANGED
@@ -11,7 +11,7 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
11 |
|
12 |
# API URLs and headers
|
13 |
AUDIO_API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
14 |
-
|
15 |
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
16 |
|
17 |
def format_error(message):
|
@@ -24,37 +24,47 @@ def create_lyrics_prompt(classification_results):
|
|
24 |
top_result = classification_results[0]
|
25 |
genre = top_result['label']
|
26 |
confidence = float(top_result['score'].strip('%')) / 100
|
27 |
-
|
28 |
-
# Create a detailed prompt
|
29 |
-
prompt = f"""Write song lyrics in the style of {genre} music. The song should capture the essence of this genre.
|
30 |
-
Additional musical elements detected: {', '.join(r['label'] for r in classification_results[1:3])}
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
2. Have a clear structure (verse, chorus)
|
35 |
-
3. Reflect the mood and themes common in this genre
|
36 |
|
37 |
-
|
38 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
return prompt
|
40 |
|
41 |
def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
42 |
-
"""Generate lyrics using
|
43 |
wait_time = initial_wait
|
44 |
|
45 |
for attempt in range(max_retries):
|
46 |
try:
|
47 |
response = requests.post(
|
48 |
-
|
49 |
headers=headers,
|
50 |
json={
|
51 |
"inputs": prompt,
|
52 |
"parameters": {
|
53 |
-
"max_new_tokens":
|
54 |
-
"temperature": 0.
|
55 |
-
"top_p": 0.
|
56 |
"do_sample": True,
|
57 |
-
"return_full_text": False
|
|
|
58 |
}
|
59 |
}
|
60 |
)
|
@@ -65,8 +75,16 @@ def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
|
65 |
if response.status_code == 200:
|
66 |
result = response.json()
|
67 |
if isinstance(result, list) and len(result) > 0:
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
elif response.status_code == 503:
|
71 |
print(f"Model loading, attempt {attempt + 1}/{max_retries}. Waiting {wait_time} seconds...")
|
72 |
time.sleep(wait_time)
|
|
|
11 |
|
12 |
# API URLs and headers
|
13 |
AUDIO_API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
14 |
+
LYRICS_API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-j-6B"
|
15 |
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
16 |
|
17 |
def format_error(message):
|
|
|
24 |
top_result = classification_results[0]
|
25 |
genre = top_result['label']
|
26 |
confidence = float(top_result['score'].strip('%')) / 100
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Get additional musical elements
|
29 |
+
additional_elements = [r['label'] for r in classification_results[1:3]]
|
|
|
|
|
30 |
|
31 |
+
# Create a detailed creative prompt
|
32 |
+
prompt = f"""Write creative and original song lyrics that capture the following musical elements:
|
33 |
+
|
34 |
+
Primary Style: {genre} ({confidence*100:.1f}% confidence)
|
35 |
+
Secondary Elements: {', '.join(additional_elements)}
|
36 |
+
|
37 |
+
Requirements:
|
38 |
+
1. Create lyrics that strongly reflect the {genre} style
|
39 |
+
2. Incorporate elements of {' and '.join(additional_elements)}
|
40 |
+
3. Include both verses and a chorus
|
41 |
+
4. Match the mood and atmosphere typical of this genre
|
42 |
+
5. Use appropriate musical terminology and style
|
43 |
+
|
44 |
+
Lyrics:
|
45 |
+
|
46 |
+
[Verse 1]
|
47 |
+
"""
|
48 |
return prompt
|
49 |
|
50 |
def generate_lyrics_with_retry(prompt, max_retries=5, initial_wait=2):
|
51 |
+
"""Generate lyrics using GPT-J-6B with retry logic"""
|
52 |
wait_time = initial_wait
|
53 |
|
54 |
for attempt in range(max_retries):
|
55 |
try:
|
56 |
response = requests.post(
|
57 |
+
LYRICS_API_URL,
|
58 |
headers=headers,
|
59 |
json={
|
60 |
"inputs": prompt,
|
61 |
"parameters": {
|
62 |
+
"max_new_tokens": 250,
|
63 |
+
"temperature": 0.8,
|
64 |
+
"top_p": 0.92,
|
65 |
"do_sample": True,
|
66 |
+
"return_full_text": False,
|
67 |
+
"stop": ["[End]", "\n\n\n"]
|
68 |
}
|
69 |
}
|
70 |
)
|
|
|
75 |
if response.status_code == 200:
|
76 |
result = response.json()
|
77 |
if isinstance(result, list) and len(result) > 0:
|
78 |
+
generated_text = result[0].get("generated_text", "")
|
79 |
+
# Clean up and format the generated text
|
80 |
+
lines = generated_text.split('\n')
|
81 |
+
cleaned_lines = []
|
82 |
+
for line in lines:
|
83 |
+
line = line.strip()
|
84 |
+
if line and not line.startswith('###') and not line.startswith('```'):
|
85 |
+
cleaned_lines.append(line)
|
86 |
+
return "\n".join(cleaned_lines)
|
87 |
+
return "Error: No text generated"
|
88 |
elif response.status_code == 503:
|
89 |
print(f"Model loading, attempt {attempt + 1}/{max_retries}. Waiting {wait_time} seconds...")
|
90 |
time.sleep(wait_time)
|