quazim commited on
Commit
81059be
Β·
1 Parent(s): 63eee27
Files changed (1) hide show
  1. app.py +55 -29
app.py CHANGED
@@ -18,7 +18,7 @@ MODEL_CONFIG = {
18
  'savings_dollars': 8.4, # $8.4 saved per 1000 hours
19
  'savings_percent': 74.9, # 74.9% savings
20
  'compressed_cost': 2.8, # $2.8 for compressed
21
- 'original_cost': 11.3, # $11.3 for original
22
  },
23
  'batch_mode': True,
24
  'batch_size': 2 # Number of variants to generate (2, 4, 6, etc.)
@@ -49,13 +49,22 @@ def cleanup_temp_files():
49
  import time
50
  temp_dir = tempfile.gettempdir()
51
  cutoff_time = time.time() - 3600
52
- for temp_file in glob.glob(os.path.join(temp_dir, "tmp*.wav")):
53
- try:
54
- if os.path.getctime(temp_file) < cutoff_time:
55
- os.remove(temp_file)
56
- print(f"[CLEANUP] Removed old temp file: {temp_file}")
57
- except OSError:
58
- pass
 
 
 
 
 
 
 
 
 
59
 
60
 
61
  _generator = None
@@ -317,6 +326,9 @@ def generate_music_batch(text_prompt, duration=10, guidance_scale=3.0, model_mod
317
  audio_variants = []
318
  sample_rate = outputs[0]['sampling_rate']
319
 
 
 
 
320
  for i, output in enumerate(outputs):
321
  audio_data = output['audio']
322
 
@@ -343,7 +355,17 @@ def generate_music_batch(text_prompt, duration=10, guidance_scale=3.0, model_mod
343
  audio_data = audio_data / max_val * 0.95
344
 
345
  audio_data = (audio_data * 32767).astype(np.int16)
346
- audio_variants.append((sample_rate, audio_data))
 
 
 
 
 
 
 
 
 
 
347
 
348
  print(f"[GENERATION] Variant {i + 1} final shape: {audio_data.shape}")
349
 
@@ -355,7 +377,8 @@ def generate_music_batch(text_prompt, duration=10, guidance_scale=3.0, model_mod
355
  variants_text = "audio"
356
  generation_info = f"βœ… Generated {variants_text} in {generation_time:.2f}s\n{savings_message}"
357
 
358
- return audio_variants[0], audio_variants[1], audio_variants[2], audio_variants[3], audio_variants[4], audio_variants[5], generation_info
 
359
 
360
  except Exception as e:
361
  print(f"[ERROR] Batch generation failed: {str(e)}")
@@ -366,11 +389,11 @@ def generate_music_batch(text_prompt, duration=10, guidance_scale=3.0, model_mod
366
 
367
  with gr.Blocks(title="MusicGen Large - Music Generation") as demo:
368
  gr.Markdown("# 🎡 MusicGen Large Music Generator")
369
-
370
  gr.Markdown(
371
  f"Generate music from text descriptions using Facebook's MusicGen "
372
  f"Large model accelerated by TheStage for 2.3x faster performance.")
373
-
374
  with gr.Column():
375
  text_input = gr.Textbox(
376
  label="Music Description",
@@ -378,7 +401,7 @@ with gr.Blocks(title="MusicGen Large - Music Generation") as demo:
378
  lines=3,
379
  value="A groovy funk bassline with a tight drum beat"
380
  )
381
-
382
  with gr.Row():
383
  duration = gr.Slider(
384
  minimum=5,
@@ -395,35 +418,35 @@ with gr.Blocks(title="MusicGen Large - Music Generation") as demo:
395
  label="Guidance Scale",
396
  info="Higher values follow prompt more closely"
397
  )
398
-
399
  generate_btn = gr.Button("🎡 Generate Music", variant="primary", size="lg")
400
-
401
  generation_info = gr.Markdown("Ready to generate music with elastic acceleration")
402
-
403
  audio_section_title = "### Generated Music"
404
  gr.Markdown(audio_section_title)
405
-
406
  actual_outputs = MODEL_CONFIG['batch_size'] if MODEL_CONFIG['batch_mode'] else 1
407
-
408
  audio_outputs = []
409
-
410
  with gr.Row():
411
- audio_output1 = gr.Audio(label="Variant 1", type="numpy", visible=actual_outputs >= 1)
412
- audio_output2 = gr.Audio(label="Variant 2", type="numpy", visible=actual_outputs >= 2)
413
  audio_outputs.extend([audio_output1, audio_output2])
414
 
415
  with gr.Row():
416
- audio_output3 = gr.Audio(label="Variant 3", type="numpy", visible=actual_outputs >= 3)
417
- audio_output4 = gr.Audio(label="Variant 4", type="numpy", visible=actual_outputs >= 4)
418
  audio_outputs.extend([audio_output3, audio_output4])
419
 
420
  with gr.Row():
421
- audio_output5 = gr.Audio(label="Variant 5", type="numpy", visible=actual_outputs >= 5)
422
- audio_output6 = gr.Audio(label="Variant 6", type="numpy", visible=actual_outputs >= 6)
423
  audio_outputs.extend([audio_output5, audio_output6])
424
-
425
  savings_banner = gr.Markdown(get_fixed_savings_message())
426
-
427
  with gr.Accordion("πŸ’‘ Tips & Information", open=False):
428
  gr.Markdown(f"""
429
  **Generation Tips:**
@@ -431,19 +454,22 @@ with gr.Blocks(title="MusicGen Large - Music Generation") as demo:
431
  - Higher guidance scale = follows prompt more closely
432
  - Lower guidance scale = more creative/varied results
433
  - Duration is limited to 30 seconds for faster generation
434
-
435
  **Performance:**
436
  - Accelerated by TheStage elastic compression
437
  - L40S GPU pricing: $1.8/hour
438
  """)
439
 
 
440
  def generate_simple(text_prompt, duration, guidance_scale):
441
  return generate_music_batch(text_prompt, duration, guidance_scale, "compressed")
442
 
 
443
  generate_btn.click(
444
  fn=generate_simple,
445
  inputs=[text_input, duration, guidance_scale],
446
- outputs=[audio_output1, audio_output2, audio_output3, audio_output4, audio_output5, audio_output6, generation_info],
 
447
  show_progress=True
448
  )
449
 
 
18
  'savings_dollars': 8.4, # $8.4 saved per 1000 hours
19
  'savings_percent': 74.9, # 74.9% savings
20
  'compressed_cost': 2.8, # $2.8 for compressed
21
+ 'original_cost': 11.3, # $11.3 for original
22
  },
23
  'batch_mode': True,
24
  'batch_size': 2 # Number of variants to generate (2, 4, 6, etc.)
 
49
  import time
50
  temp_dir = tempfile.gettempdir()
51
  cutoff_time = time.time() - 3600
52
+
53
+ # Clean old generated music files
54
+ patterns = [
55
+ os.path.join(temp_dir, "tmp*.wav"),
56
+ os.path.join(temp_dir, "generated_music_*.wav"),
57
+ os.path.join(temp_dir, "musicgen_variant_*.wav")
58
+ ]
59
+
60
+ for pattern in patterns:
61
+ for temp_file in glob.glob(pattern):
62
+ try:
63
+ if os.path.getctime(temp_file) < cutoff_time:
64
+ os.remove(temp_file)
65
+ print(f"[CLEANUP] Removed old temp file: {temp_file}")
66
+ except OSError:
67
+ pass
68
 
69
 
70
  _generator = None
 
326
  audio_variants = []
327
  sample_rate = outputs[0]['sampling_rate']
328
 
329
+ # Create unique timestamp for this generation batch
330
+ batch_timestamp = int(time.time() * 1000)
331
+
332
  for i, output in enumerate(outputs):
333
  audio_data = output['audio']
334
 
 
355
  audio_data = audio_data / max_val * 0.95
356
 
357
  audio_data = (audio_data * 32767).astype(np.int16)
358
+
359
+ # Save each variant to a unique temporary file
360
+ temp_filename = f"musicgen_variant_{i+1}_{batch_timestamp}.wav"
361
+ temp_path = os.path.join(tempfile.gettempdir(), temp_filename)
362
+
363
+ sf.write(temp_path, audio_data, sample_rate)
364
+
365
+ print(f"[GENERATION] Variant {i + 1} saved to: {temp_path}")
366
+ print(f"[GENERATION] Variant {i + 1} file size: {os.path.getsize(temp_path)} bytes")
367
+
368
+ audio_variants.append(temp_path)
369
 
370
  print(f"[GENERATION] Variant {i + 1} final shape: {audio_data.shape}")
371
 
 
377
  variants_text = "audio"
378
  generation_info = f"βœ… Generated {variants_text} in {generation_time:.2f}s\n{savings_message}"
379
 
380
+ return audio_variants[0], audio_variants[1], audio_variants[2], audio_variants[3], audio_variants[4], \
381
+ audio_variants[5], generation_info
382
 
383
  except Exception as e:
384
  print(f"[ERROR] Batch generation failed: {str(e)}")
 
389
 
390
  with gr.Blocks(title="MusicGen Large - Music Generation") as demo:
391
  gr.Markdown("# 🎡 MusicGen Large Music Generator")
392
+
393
  gr.Markdown(
394
  f"Generate music from text descriptions using Facebook's MusicGen "
395
  f"Large model accelerated by TheStage for 2.3x faster performance.")
396
+
397
  with gr.Column():
398
  text_input = gr.Textbox(
399
  label="Music Description",
 
401
  lines=3,
402
  value="A groovy funk bassline with a tight drum beat"
403
  )
404
+
405
  with gr.Row():
406
  duration = gr.Slider(
407
  minimum=5,
 
418
  label="Guidance Scale",
419
  info="Higher values follow prompt more closely"
420
  )
421
+
422
  generate_btn = gr.Button("🎡 Generate Music", variant="primary", size="lg")
423
+
424
  generation_info = gr.Markdown("Ready to generate music with elastic acceleration")
425
+
426
  audio_section_title = "### Generated Music"
427
  gr.Markdown(audio_section_title)
428
+
429
  actual_outputs = MODEL_CONFIG['batch_size'] if MODEL_CONFIG['batch_mode'] else 1
430
+
431
  audio_outputs = []
432
+
433
  with gr.Row():
434
+ audio_output1 = gr.Audio(label="Variant 1", type="filepath", visible=actual_outputs >= 1)
435
+ audio_output2 = gr.Audio(label="Variant 2", type="filepath", visible=actual_outputs >= 2)
436
  audio_outputs.extend([audio_output1, audio_output2])
437
 
438
  with gr.Row():
439
+ audio_output3 = gr.Audio(label="Variant 3", type="filepath", visible=actual_outputs >= 3)
440
+ audio_output4 = gr.Audio(label="Variant 4", type="filepath", visible=actual_outputs >= 4)
441
  audio_outputs.extend([audio_output3, audio_output4])
442
 
443
  with gr.Row():
444
+ audio_output5 = gr.Audio(label="Variant 5", type="filepath", visible=actual_outputs >= 5)
445
+ audio_output6 = gr.Audio(label="Variant 6", type="filepath", visible=actual_outputs >= 6)
446
  audio_outputs.extend([audio_output5, audio_output6])
447
+
448
  savings_banner = gr.Markdown(get_fixed_savings_message())
449
+
450
  with gr.Accordion("πŸ’‘ Tips & Information", open=False):
451
  gr.Markdown(f"""
452
  **Generation Tips:**
 
454
  - Higher guidance scale = follows prompt more closely
455
  - Lower guidance scale = more creative/varied results
456
  - Duration is limited to 30 seconds for faster generation
457
+
458
  **Performance:**
459
  - Accelerated by TheStage elastic compression
460
  - L40S GPU pricing: $1.8/hour
461
  """)
462
 
463
+
464
  def generate_simple(text_prompt, duration, guidance_scale):
465
  return generate_music_batch(text_prompt, duration, guidance_scale, "compressed")
466
 
467
+
468
  generate_btn.click(
469
  fn=generate_simple,
470
  inputs=[text_input, duration, guidance_scale],
471
+ outputs=[audio_output1, audio_output2, audio_output3, audio_output4, audio_output5, audio_output6,
472
+ generation_info],
473
  show_progress=True
474
  )
475