jacob-c commited on
Commit
1f2ed19
·
1 Parent(s): 30539fa
Files changed (1) hide show
  1. app.py +54 -24
app.py CHANGED
@@ -257,6 +257,42 @@ def format_results(classification_results, lyrics, prompt):
257
  """
258
  return output
259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  def classify_and_generate(audio_file):
261
  """
262
  Classify the audio and generate matching lyrics
@@ -289,7 +325,7 @@ def classify_and_generate(audio_file):
289
  data = f.read()
290
 
291
  print("Sending request to Audio Classification API...")
292
- response = requests.post(AUDIO_API_URL, headers=headers, data=data)
293
 
294
  # Clean up the temporary file
295
  try:
@@ -297,30 +333,24 @@ def classify_and_generate(audio_file):
297
  except:
298
  pass
299
 
300
- if response.status_code == 200:
301
- classification_results = response.json()
302
- # Format classification results
303
- formatted_results = []
304
- for result in classification_results:
305
- formatted_results.append({
306
- 'label': result['label'],
307
- 'score': f"{result['score']*100:.2f}%"
308
- })
309
-
310
- # Generate lyrics based on classification with retry logic
311
- print("Generating lyrics based on classification...")
312
- prompt = create_lyrics_prompt(formatted_results, song_structure)
313
- lyrics = generate_lyrics_with_retry(prompt, song_structure)
314
-
315
- # Format and return results
316
- return format_results(formatted_results, lyrics, prompt)
317
 
318
- elif response.status_code == 401:
319
- return "Error: Invalid or missing API token. Please check your Hugging Face API token."
320
- elif response.status_code == 503:
321
- return "Error: Model is loading. Please try again in a few seconds."
322
- else:
323
- return f"Error: API returned status code {response.status_code}\nResponse: {response.text}"
 
 
 
 
 
 
 
 
 
324
 
325
  except Exception as e:
326
  import traceback
 
257
  """
258
  return output
259
 
260
+ def classify_with_retry(data, max_retries=5, initial_wait=2):
261
+ """Classify audio with retry logic for 503 errors"""
262
+ wait_time = initial_wait
263
+
264
+ for attempt in range(max_retries):
265
+ try:
266
+ print(f"\nAttempt {attempt + 1}: Classifying audio...")
267
+ response = requests.post(AUDIO_API_URL, headers=headers, data=data)
268
+
269
+ if response.status_code == 200:
270
+ return response.json()
271
+ elif response.status_code == 503:
272
+ print(f"Model loading, waiting {wait_time} seconds...")
273
+ time.sleep(wait_time)
274
+ wait_time *= 1.5
275
+ continue
276
+ else:
277
+ print(f"Error response: {response.text}")
278
+ if attempt < max_retries - 1:
279
+ time.sleep(wait_time)
280
+ continue
281
+ return None
282
+
283
+ except Exception as e:
284
+ print(f"Exception during classification: {str(e)}")
285
+ if attempt < max_retries - 1:
286
+ time.sleep(wait_time)
287
+ wait_time *= 1.5
288
+ continue
289
+ return None
290
+
291
+ time.sleep(wait_time)
292
+ wait_time = min(wait_time * 1.5, 10)
293
+
294
+ return None
295
+
296
  def classify_and_generate(audio_file):
297
  """
298
  Classify the audio and generate matching lyrics
 
325
  data = f.read()
326
 
327
  print("Sending request to Audio Classification API...")
328
+ classification_results = classify_with_retry(data)
329
 
330
  # Clean up the temporary file
331
  try:
 
333
  except:
334
  pass
335
 
336
+ if classification_results is None:
337
+ return "Error: Failed to classify audio after multiple retries. Please try again."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
 
339
+ # Format classification results
340
+ formatted_results = []
341
+ for result in classification_results:
342
+ formatted_results.append({
343
+ 'label': result['label'],
344
+ 'score': f"{result['score']*100:.2f}%"
345
+ })
346
+
347
+ # Generate lyrics based on classification with retry logic
348
+ print("Generating lyrics based on classification...")
349
+ prompt = create_lyrics_prompt(formatted_results, song_structure)
350
+ lyrics = generate_lyrics_with_retry(prompt, song_structure)
351
+
352
+ # Format and return results
353
+ return format_results(formatted_results, lyrics, prompt)
354
 
355
  except Exception as e:
356
  import traceback