Update app.py
Browse files
app.py
CHANGED
|
@@ -42,7 +42,7 @@ try:
|
|
| 42 |
sys.exit(1)
|
| 43 |
musicgen_model = MusicGen.get_pretrained(local_model_path, device=device)
|
| 44 |
musicgen_model.set_generation_params(
|
| 45 |
-
duration=
|
| 46 |
two_step_cfg=False # Disable two-step CFG for stability
|
| 47 |
)
|
| 48 |
except Exception as e:
|
|
@@ -58,6 +58,15 @@ def print_resource_usage(stage: str):
|
|
| 58 |
print(f"CPU Memory Used: {psutil.virtual_memory().percent}%")
|
| 59 |
print("---------------")
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# 4) GENRE PROMPT FUNCTIONS
|
| 62 |
def set_red_hot_chili_peppers_prompt(bpm):
|
| 63 |
rhythm = "strong rhythmic steps" if bpm > 120 else "groovy rhythmic flow"
|
|
@@ -158,6 +167,10 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 158 |
output_files = []
|
| 159 |
variation_segments = []
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# Adjust temperature based on BPM for energy
|
| 162 |
adjusted_temperature = temperature + (bpm - 120) / 600.0 # Scale temperature slightly
|
| 163 |
adjusted_temperature = min(max(adjusted_temperature, 0.1), 2.0)
|
|
@@ -170,8 +183,8 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 170 |
|
| 171 |
if use_chunks:
|
| 172 |
# Chunked generation
|
| 173 |
-
num_chunks = max(1, min(chunk_count, total_duration //
|
| 174 |
-
chunk_duration = total_duration / num_chunks
|
| 175 |
overlap_duration = min(1.0, crossfade_duration / 1000.0)
|
| 176 |
generation_duration = chunk_duration + overlap_duration
|
| 177 |
audio_chunks = []
|
|
@@ -218,6 +231,7 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 218 |
|
| 219 |
torch.cuda.empty_cache()
|
| 220 |
gc.collect()
|
|
|
|
| 221 |
time.sleep(0.5)
|
| 222 |
print_resource_usage(f"After Chunk {i+1} Generation (Variation {var+1})")
|
| 223 |
|
|
@@ -273,6 +287,7 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 273 |
|
| 274 |
torch.cuda.empty_cache()
|
| 275 |
gc.collect()
|
|
|
|
| 276 |
time.sleep(0.5)
|
| 277 |
print_resource_usage(f"After Full Track Generation (Variation {var+1})")
|
| 278 |
|
|
@@ -312,6 +327,7 @@ def generate_music(instrumental_prompt: str, cfg_scale: float, top_k: int, top_p
|
|
| 312 |
finally:
|
| 313 |
torch.cuda.empty_cache()
|
| 314 |
gc.collect()
|
|
|
|
| 315 |
|
| 316 |
# Function to toggle interactivity of chunk-related sliders
|
| 317 |
def toggle_chunk_interactivity(use_chunks):
|
|
@@ -322,7 +338,7 @@ def toggle_chunk_interactivity(use_chunks):
|
|
| 322 |
)
|
| 323 |
|
| 324 |
def clear_inputs():
|
| 325 |
-
return "", 3.0, 250, 0.9, 1.0,
|
| 326 |
|
| 327 |
# 7) CUSTOM CSS
|
| 328 |
css = """
|
|
@@ -461,7 +477,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 461 |
label="Total Duration (seconds)",
|
| 462 |
minimum=10,
|
| 463 |
maximum=90,
|
| 464 |
-
value=
|
| 465 |
step=1,
|
| 466 |
info="Total duration of the track (10 to 90 seconds)."
|
| 467 |
)
|
|
@@ -479,7 +495,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 479 |
maximum=2000,
|
| 480 |
value=1000,
|
| 481 |
step=100,
|
| 482 |
-
info="Crossfade duration between variations
|
| 483 |
)
|
| 484 |
num_variations = gr.Slider(
|
| 485 |
label="Number of Variations",
|
|
@@ -495,7 +511,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 495 |
maximum=8,
|
| 496 |
value=2,
|
| 497 |
step=1,
|
| 498 |
-
info="Number of chunks to split the track into (only used if chunking is enabled)."
|
| 499 |
)
|
| 500 |
use_chunks = gr.Checkbox(
|
| 501 |
label="Generate in Chunks",
|
|
|
|
| 42 |
sys.exit(1)
|
| 43 |
musicgen_model = MusicGen.get_pretrained(local_model_path, device=device)
|
| 44 |
musicgen_model.set_generation_params(
|
| 45 |
+
duration=20, # Default chunk duration
|
| 46 |
two_step_cfg=False # Disable two-step CFG for stability
|
| 47 |
)
|
| 48 |
except Exception as e:
|
|
|
|
| 58 |
print(f"CPU Memory Used: {psutil.virtual_memory().percent}%")
|
| 59 |
print("---------------")
|
| 60 |
|
| 61 |
+
# Check available GPU memory
|
| 62 |
+
def check_vram_availability(required_gb=6.0):
|
| 63 |
+
total_vram = torch.cuda.get_device_properties(0).total_memory / (1024**3)
|
| 64 |
+
allocated_vram = torch.cuda.memory_allocated() / (1024**3)
|
| 65 |
+
available_vram = total_vram - allocated_vram
|
| 66 |
+
if available_vram < required_gb:
|
| 67 |
+
print(f"WARNING: Low VRAM available ({available_vram:.2f} GB). Consider reducing total_duration or num_variations.")
|
| 68 |
+
return available_vram >= required_gb
|
| 69 |
+
|
| 70 |
# 4) GENRE PROMPT FUNCTIONS
|
| 71 |
def set_red_hot_chili_peppers_prompt(bpm):
|
| 72 |
rhythm = "strong rhythmic steps" if bpm > 120 else "groovy rhythmic flow"
|
|
|
|
| 167 |
output_files = []
|
| 168 |
variation_segments = []
|
| 169 |
|
| 170 |
+
# Check VRAM availability
|
| 171 |
+
if not check_vram_availability(required_gb=6.0):
|
| 172 |
+
return None, "⚠️ Insufficient VRAM for generation. Reduce total_duration or num_variations."
|
| 173 |
+
|
| 174 |
# Adjust temperature based on BPM for energy
|
| 175 |
adjusted_temperature = temperature + (bpm - 120) / 600.0 # Scale temperature slightly
|
| 176 |
adjusted_temperature = min(max(adjusted_temperature, 0.1), 2.0)
|
|
|
|
| 183 |
|
| 184 |
if use_chunks:
|
| 185 |
# Chunked generation
|
| 186 |
+
num_chunks = max(1, min(chunk_count, total_duration // 20)) # Cap at 20 seconds per chunk
|
| 187 |
+
chunk_duration = min(total_duration / num_chunks, 20) # Max 20 seconds
|
| 188 |
overlap_duration = min(1.0, crossfade_duration / 1000.0)
|
| 189 |
generation_duration = chunk_duration + overlap_duration
|
| 190 |
audio_chunks = []
|
|
|
|
| 231 |
|
| 232 |
torch.cuda.empty_cache()
|
| 233 |
gc.collect()
|
| 234 |
+
torch.cuda.synchronize()
|
| 235 |
time.sleep(0.5)
|
| 236 |
print_resource_usage(f"After Chunk {i+1} Generation (Variation {var+1})")
|
| 237 |
|
|
|
|
| 287 |
|
| 288 |
torch.cuda.empty_cache()
|
| 289 |
gc.collect()
|
| 290 |
+
torch.cuda.synchronize()
|
| 291 |
time.sleep(0.5)
|
| 292 |
print_resource_usage(f"After Full Track Generation (Variation {var+1})")
|
| 293 |
|
|
|
|
| 327 |
finally:
|
| 328 |
torch.cuda.empty_cache()
|
| 329 |
gc.collect()
|
| 330 |
+
torch.cuda.synchronize()
|
| 331 |
|
| 332 |
# Function to toggle interactivity of chunk-related sliders
|
| 333 |
def toggle_chunk_interactivity(use_chunks):
|
|
|
|
| 338 |
)
|
| 339 |
|
| 340 |
def clear_inputs():
|
| 341 |
+
return "", 3.0, 250, 0.9, 1.0, 20, 1000, 1000, 1, 2, True, 120
|
| 342 |
|
| 343 |
# 7) CUSTOM CSS
|
| 344 |
css = """
|
|
|
|
| 477 |
label="Total Duration (seconds)",
|
| 478 |
minimum=10,
|
| 479 |
maximum=90,
|
| 480 |
+
value=20,
|
| 481 |
step=1,
|
| 482 |
info="Total duration of the track (10 to 90 seconds)."
|
| 483 |
)
|
|
|
|
| 495 |
maximum=2000,
|
| 496 |
value=1000,
|
| 497 |
step=100,
|
| 498 |
+
info="Crossfade duration between variations."
|
| 499 |
)
|
| 500 |
num_variations = gr.Slider(
|
| 501 |
label="Number of Variations",
|
|
|
|
| 511 |
maximum=8,
|
| 512 |
value=2,
|
| 513 |
step=1,
|
| 514 |
+
info="Number of chunks to split the track into (only used if chunking is enabled, max 20 seconds per chunk)."
|
| 515 |
)
|
| 516 |
use_chunks = gr.Checkbox(
|
| 517 |
label="Generate in Chunks",
|