Update app.py
Browse filesfull product updates
app.py
CHANGED
|
@@ -32,6 +32,12 @@ if device != "cuda":
|
|
| 32 |
sys.exit(1)
|
| 33 |
print(f"CUDA is available. Using GPU: {torch.cuda.get_device_name(0)}")
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# 2) LOAD MUSICGEN INTO VRAM
|
| 36 |
try:
|
| 37 |
print("Loading MusicGen medium model into VRAM...")
|
|
@@ -42,7 +48,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:
|
|
@@ -59,286 +65,265 @@ def print_resource_usage(stage: str):
|
|
| 59 |
print("---------------")
|
| 60 |
|
| 61 |
# Check available GPU memory
|
| 62 |
-
def check_vram_availability(required_gb=
|
| 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).
|
| 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"
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
styles = ["anthemic", "gritty", "melodic", "fast-paced", "driving"]
|
| 89 |
tempos = ["upbeat", "mid-tempo", "high-energy"]
|
| 90 |
moods = ["energetic", "introspective", "rebellious", "uplifting"]
|
| 91 |
style = random.choice(styles)
|
| 92 |
tempo = random.choice(tempos)
|
| 93 |
mood = random.choice(moods)
|
| 94 |
-
rhythm = "powerful rhythmic steps" if bpm > 120 else "catchy rhythmic groove"
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
return f"
|
| 128 |
-
|
| 129 |
-
def
|
| 130 |
-
rhythm = "
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# 5) AUDIO PROCESSING FUNCTIONS
|
| 134 |
-
def apply_chorus(segment):
|
| 135 |
-
delayed = segment - 6
|
| 136 |
-
delayed = delayed.set_frame_rate(segment.frame_rate)
|
| 137 |
-
return segment.overlay(delayed, position=20)
|
| 138 |
-
|
| 139 |
def apply_eq(segment):
|
| 140 |
segment = segment.low_pass_filter(8000)
|
| 141 |
segment = segment.high_pass_filter(80)
|
| 142 |
return segment
|
| 143 |
|
| 144 |
-
def
|
| 145 |
-
if segment.dBFS > max_db:
|
| 146 |
-
segment = segment - (segment.dBFS - max_db)
|
| 147 |
-
return segment
|
| 148 |
-
|
| 149 |
-
def apply_final_gain(segment, target_db=-18.0):
|
| 150 |
-
gain_adjustment = target_db - segment.dBFS
|
| 151 |
-
return segment + gain_adjustment
|
| 152 |
-
|
| 153 |
-
def apply_fade(segment, fade_in_duration=2000, fade_out_duration=2000):
|
| 154 |
segment = segment.fade_in(fade_in_duration)
|
| 155 |
segment = segment.fade_out(fade_out_duration)
|
| 156 |
return segment
|
| 157 |
|
| 158 |
# 6) GENERATION & I/O FUNCTIONS
|
| 159 |
-
def generate_music(instrumental_prompt: str, cfg_scale: float,
|
| 160 |
global musicgen_model
|
| 161 |
if not instrumental_prompt.strip():
|
| 162 |
return None, "⚠️ Please enter a valid instrumental prompt!"
|
| 163 |
try:
|
| 164 |
start_time = time.time()
|
| 165 |
-
total_duration = min(max(total_duration,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
sample_rate = musicgen_model.sample_rate
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
print(f"Generating
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
torchaudio.save(temp_wav_path, audio_chunk, sample_rate, bits_per_sample=24)
|
| 227 |
-
segment = AudioSegment.from_wav(temp_wav_path)
|
| 228 |
-
segment.export(chunk_path, format="mp3", bitrate="320k")
|
| 229 |
-
os.remove(temp_wav_path)
|
| 230 |
-
audio_chunks.append(chunk_path)
|
| 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 |
-
|
| 238 |
-
print(f"Combining audio chunks for variation {var+1}...")
|
| 239 |
-
final_segment = AudioSegment.from_mp3(audio_chunks[0])
|
| 240 |
-
for i in range(1, len(audio_chunks)):
|
| 241 |
-
next_segment = AudioSegment.from_mp3(audio_chunks[i])
|
| 242 |
-
next_segment = next_segment + 1
|
| 243 |
-
final_segment = final_segment.append(next_segment, crossfade=crossfade_duration)
|
| 244 |
-
|
| 245 |
-
final_segment = final_segment[:total_duration * 1000]
|
| 246 |
-
variation_segments.append(final_segment)
|
| 247 |
-
|
| 248 |
-
for chunk_path in audio_chunks:
|
| 249 |
-
os.remove(chunk_path)
|
| 250 |
-
else:
|
| 251 |
-
# Single-shot generation
|
| 252 |
-
print(f"Generating full track for variation {var+1} on GPU (prompt: {instrumental_prompt})...")
|
| 253 |
-
musicgen_model.set_generation_params(
|
| 254 |
-
duration=total_duration,
|
| 255 |
-
use_sampling=True,
|
| 256 |
-
top_k=top_k,
|
| 257 |
-
top_p=top_p,
|
| 258 |
-
temperature=adjusted_temperature,
|
| 259 |
-
cfg_coef=cfg_scale
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
-
print_resource_usage(f"Before Full Track Generation (Variation {var+1})")
|
| 263 |
-
|
| 264 |
-
with torch.no_grad():
|
| 265 |
-
with autocast():
|
| 266 |
-
audio_chunk = musicgen_model.generate([f"{instrumental_prompt}, at {bpm} BPM"], progress=True)[0]
|
| 267 |
-
|
| 268 |
-
audio_chunk = audio_chunk.cpu().to(dtype=torch.float32)
|
| 269 |
-
if audio_chunk.dim() == 1:
|
| 270 |
-
audio_chunk = torch.stack([audio_chunk, audio_chunk], dim=0)
|
| 271 |
-
elif audio_chunk.dim() == 2 and audio_chunk.shape[0] == 1:
|
| 272 |
-
audio_chunk = torch.cat([audio_chunk, audio_chunk], dim=0)
|
| 273 |
-
elif audio_chunk.dim() == 2 and audio_chunk.shape[0] != 2:
|
| 274 |
-
audio_chunk = audio_chunk[:1, :]
|
| 275 |
-
audio_chunk = torch.cat([audio_chunk, audio_chunk], dim=0)
|
| 276 |
-
elif audio_chunk.dim() > 2:
|
| 277 |
-
audio_chunk = audio_chunk.view(2, -1)
|
| 278 |
-
|
| 279 |
-
if audio_chunk.shape[0] != 2:
|
| 280 |
-
raise ValueError(f"Expected stereo audio with shape (2, samples), got shape {audio_chunk.shape}")
|
| 281 |
-
|
| 282 |
-
temp_wav_path = f"temp_full_{var}.wav"
|
| 283 |
-
torchaudio.save(temp_wav_path, audio_chunk, sample_rate, bits_per_sample=24)
|
| 284 |
-
final_segment = AudioSegment.from_wav(temp_wav_path)
|
| 285 |
-
os.remove(temp_wav_path)
|
| 286 |
-
variation_segments.append(final_segment)
|
| 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 |
-
|
| 294 |
-
# Combine variations with crossfade
|
| 295 |
-
print("Combining variations with crossfade...")
|
| 296 |
-
combined_segment = variation_segments[0]
|
| 297 |
-
for i in range(1, len(variation_segments)):
|
| 298 |
-
next_segment = variation_segments[i]
|
| 299 |
next_segment = next_segment + 1
|
| 300 |
-
|
|
|
|
|
|
|
| 301 |
|
| 302 |
-
# Post-process combined track
|
| 303 |
print("Post-processing final track...")
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
combined_segment = combined_segment.normalize(headroom=-9.0)
|
| 308 |
-
combined_segment = apply_final_gain(combined_segment, target_db=-18.0)
|
| 309 |
|
| 310 |
mp3_path = "output_cleaned.mp3"
|
| 311 |
-
|
| 312 |
mp3_path,
|
| 313 |
format="mp3",
|
| 314 |
-
bitrate="
|
| 315 |
tags={"title": "GhostAI Instrumental", "artist": "GhostAI"}
|
| 316 |
)
|
| 317 |
print(f"Saved final audio to {mp3_path}")
|
| 318 |
-
output_files.append(mp3_path)
|
| 319 |
|
| 320 |
print_resource_usage("After Final Generation")
|
| 321 |
print(f"Total Generation Time: {time.time() - start_time:.2f} seconds")
|
| 322 |
|
| 323 |
-
|
| 324 |
-
return mp3_path, f"✅ Done! Generated {num_variations} variations."
|
| 325 |
except Exception as e:
|
| 326 |
return None, f"❌ Generation failed: {e}"
|
| 327 |
finally:
|
| 328 |
torch.cuda.empty_cache()
|
| 329 |
gc.collect()
|
|
|
|
| 330 |
torch.cuda.synchronize()
|
| 331 |
|
| 332 |
-
# Function to
|
| 333 |
-
def toggle_chunk_interactivity(use_chunks):
|
| 334 |
-
return (
|
| 335 |
-
gr.update(interactive=use_chunks), # crossfade_duration
|
| 336 |
-
gr.update(interactive=use_chunks), # variation_crossfade_duration
|
| 337 |
-
gr.update(interactive=use_chunks) # chunk_count
|
| 338 |
-
)
|
| 339 |
-
|
| 340 |
def clear_inputs():
|
| 341 |
-
return "", 3.0,
|
| 342 |
|
| 343 |
# 7) CUSTOM CSS
|
| 344 |
css = """
|
|
@@ -449,74 +434,29 @@ with gr.Blocks(css=css) as demo:
|
|
| 449 |
step=0.1,
|
| 450 |
info="Higher values make the instrumental more closely follow the prompt."
|
| 451 |
)
|
| 452 |
-
top_k = gr.Slider(
|
| 453 |
-
label="Top-K Sampling",
|
| 454 |
-
minimum=10,
|
| 455 |
-
maximum=500,
|
| 456 |
-
value=250,
|
| 457 |
-
step=10,
|
| 458 |
-
info="Limits sampling to the top k most likely tokens."
|
| 459 |
-
)
|
| 460 |
-
top_p = gr.Slider(
|
| 461 |
-
label="Top-P Sampling",
|
| 462 |
-
minimum=0.0,
|
| 463 |
-
maximum=1.0,
|
| 464 |
-
value=0.9,
|
| 465 |
-
step=0.05,
|
| 466 |
-
info="Keeps tokens with cumulative probability above p."
|
| 467 |
-
)
|
| 468 |
-
temperature = gr.Slider(
|
| 469 |
-
label="Temperature",
|
| 470 |
-
minimum=0.1,
|
| 471 |
-
maximum=2.0,
|
| 472 |
-
value=1.0,
|
| 473 |
-
step=0.1,
|
| 474 |
-
info="Controls randomness. Higher values make output more diverse."
|
| 475 |
-
)
|
| 476 |
total_duration = gr.Slider(
|
| 477 |
label="Total Duration (seconds)",
|
| 478 |
-
minimum=
|
| 479 |
-
maximum=
|
| 480 |
-
value=
|
| 481 |
step=1,
|
| 482 |
-
info="Total duration of the track (
|
| 483 |
)
|
| 484 |
-
|
| 485 |
-
label="Chunk
|
| 486 |
-
minimum=
|
| 487 |
-
maximum=
|
| 488 |
-
value=
|
| 489 |
-
step=
|
| 490 |
-
info="
|
| 491 |
)
|
| 492 |
-
|
| 493 |
-
label="
|
| 494 |
minimum=100,
|
| 495 |
maximum=2000,
|
| 496 |
value=1000,
|
| 497 |
step=100,
|
| 498 |
-
info="Crossfade duration between
|
| 499 |
-
)
|
| 500 |
-
num_variations = gr.Slider(
|
| 501 |
-
label="Number of Variations",
|
| 502 |
-
minimum=1,
|
| 503 |
-
maximum=4,
|
| 504 |
-
value=1,
|
| 505 |
-
step=1,
|
| 506 |
-
info="Number of different versions to generate with varying random seeds."
|
| 507 |
-
)
|
| 508 |
-
chunk_count = gr.Slider(
|
| 509 |
-
label="Chunk Count",
|
| 510 |
-
minimum=1,
|
| 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",
|
| 518 |
-
value=True,
|
| 519 |
-
info="Enable to generate in chunks (safer for GPU memory). Disable for single-shot generation (higher VRAM usage)."
|
| 520 |
)
|
| 521 |
bpm = gr.Slider(
|
| 522 |
label="Tempo (BPM)",
|
|
@@ -526,6 +466,42 @@ with gr.Blocks(css=css) as demo:
|
|
| 526 |
step=1,
|
| 527 |
info="Beats per minute to influence the track's tempo."
|
| 528 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
with gr.Row(elem_classes="action-buttons"):
|
| 530 |
gen_btn = gr.Button("Generate Music")
|
| 531 |
clr_btn = gr.Button("Clear Inputs")
|
|
@@ -534,37 +510,29 @@ with gr.Blocks(css=css) as demo:
|
|
| 534 |
out_audio = gr.Audio(label="Generated Stereo Instrumental Track", type="filepath")
|
| 535 |
status = gr.Textbox(label="Status", interactive=False)
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
)
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
radiohead_btn.click(set_radiohead_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 552 |
-
classic_rock_btn.click(set_classic_rock_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 553 |
-
alternative_rock_btn.click(set_alternative_rock_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 554 |
-
post_punk_btn.click(set_post_punk_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 555 |
-
indie_rock_btn.click(set_indie_rock_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 556 |
-
funk_rock_btn.click(set_funk_rock_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 557 |
-
detroit_techno_btn.click(set_detroit_techno_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 558 |
-
deep_house_btn.click(set_deep_house_prompt, inputs=bpm, outputs=instrumental_prompt)
|
| 559 |
gen_btn.click(
|
| 560 |
generate_music,
|
| 561 |
-
inputs=[instrumental_prompt, cfg_scale,
|
| 562 |
outputs=[out_audio, status]
|
| 563 |
)
|
| 564 |
clr_btn.click(
|
| 565 |
clear_inputs,
|
| 566 |
inputs=None,
|
| 567 |
-
outputs=[instrumental_prompt, cfg_scale,
|
| 568 |
)
|
| 569 |
|
| 570 |
# 9) TURN OFF OPENAPI/DOCS
|
|
|
|
| 32 |
sys.exit(1)
|
| 33 |
print(f"CUDA is available. Using GPU: {torch.cuda.get_device_name(0)}")
|
| 34 |
|
| 35 |
+
# Pre-run memory cleanup
|
| 36 |
+
torch.cuda.empty_cache()
|
| 37 |
+
gc.collect()
|
| 38 |
+
torch.cuda.ipc_collect()
|
| 39 |
+
torch.cuda.synchronize()
|
| 40 |
+
|
| 41 |
# 2) LOAD MUSICGEN INTO VRAM
|
| 42 |
try:
|
| 43 |
print("Loading MusicGen medium model into VRAM...")
|
|
|
|
| 48 |
sys.exit(1)
|
| 49 |
musicgen_model = MusicGen.get_pretrained(local_model_path, device=device)
|
| 50 |
musicgen_model.set_generation_params(
|
| 51 |
+
duration=10, # Default chunk duration
|
| 52 |
two_step_cfg=False # Disable two-step CFG for stability
|
| 53 |
)
|
| 54 |
except Exception as e:
|
|
|
|
| 65 |
print("---------------")
|
| 66 |
|
| 67 |
# Check available GPU memory
|
| 68 |
+
def check_vram_availability(required_gb=3.5):
|
| 69 |
total_vram = torch.cuda.get_device_properties(0).total_memory / (1024**3)
|
| 70 |
allocated_vram = torch.cuda.memory_allocated() / (1024**3)
|
| 71 |
available_vram = total_vram - allocated_vram
|
| 72 |
if available_vram < required_gb:
|
| 73 |
+
print(f"WARNING: Low VRAM available ({available_vram:.2f} GB). Reduce total_duration or chunk_duration.")
|
| 74 |
return available_vram >= required_gb
|
| 75 |
|
| 76 |
# 4) GENRE PROMPT FUNCTIONS
|
| 77 |
+
def set_red_hot_chili_peppers_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 78 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("strong rhythmic steps" if bpm > 120 else "groovy rhythmic flow")
|
| 79 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 80 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 81 |
+
bass = f", {bass_style}" if bass_style != "none" else ", groovy basslines"
|
| 82 |
+
guitar = f", {guitar_style} guitar riffs" if guitar_style != "none" else ", syncopated guitar riffs"
|
| 83 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 84 |
+
return f"Funk rock{bass}{guitar}{drum}{synth}{vocal}, Red Hot Chili Peppers-inspired vibe with dynamic energy and funky breakdowns, {rhythm} at {bpm} BPM."
|
| 85 |
+
|
| 86 |
+
def set_nirvana_grunge_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 87 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("intense rhythmic steps" if bpm > 120 else "grungy rhythmic pulse")
|
| 88 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 89 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 90 |
+
bass = f", {bass_style}" if bass_style != "none" else ", melodic basslines"
|
| 91 |
+
guitar = f", {guitar_style} guitar riffs" if guitar_style != "none" else ", raw distorted guitar riffs"
|
| 92 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 93 |
+
return f"Grunge{bass}{guitar}{drum}{synth}{vocal}, Nirvana-inspired angst-filled sound with quiet-loud dynamics, {rhythm} at {bpm} BPM."
|
| 94 |
+
|
| 95 |
+
def set_pearl_jam_grunge_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 96 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("soulful rhythmic steps" if bpm > 120 else "driving rhythmic flow")
|
| 97 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 98 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 99 |
+
bass = f", {bass_style}" if bass_style != "none" else ", deep bass"
|
| 100 |
+
guitar = f", {guitar_style} guitar leads" if guitar_style != "none" else ", soulful guitar leads"
|
| 101 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 102 |
+
return f"Grunge{bass}{guitar}{drum}{synth}{vocal}, Pearl Jam-inspired emotional intensity with soaring choruses, {rhythm} at {bpm} BPM."
|
| 103 |
+
|
| 104 |
+
def set_soundgarden_grunge_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 105 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("heavy rhythmic steps" if bpm > 120 else "sludgy rhythmic groove")
|
| 106 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 107 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 108 |
+
bass = f", {bass_style}" if bass_style != "none" else ""
|
| 109 |
+
guitar = f", {guitar_style} guitar riffs" if guitar_style != "none" else ", heavy sludgy guitar riffs"
|
| 110 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 111 |
+
return f"Grunge{bass}{guitar}{drum}{synth}{vocal}, Soundgarden-inspired dark, psychedelic edge with powerful vocals, {rhythm} at {bpm} BPM."
|
| 112 |
+
|
| 113 |
+
def set_foo_fighters_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 114 |
styles = ["anthemic", "gritty", "melodic", "fast-paced", "driving"]
|
| 115 |
tempos = ["upbeat", "mid-tempo", "high-energy"]
|
| 116 |
moods = ["energetic", "introspective", "rebellious", "uplifting"]
|
| 117 |
style = random.choice(styles)
|
| 118 |
tempo = random.choice(tempos)
|
| 119 |
mood = random.choice(moods)
|
| 120 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("powerful rhythmic steps" if bpm > 120 else "catchy rhythmic groove")
|
| 121 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 122 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 123 |
+
bass = f", {bass_style}" if bass_style != "none" else ""
|
| 124 |
+
guitar = f", {guitar_style} guitar riffs" if guitar_style != "none" else ", {style} guitar riffs"
|
| 125 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 126 |
+
return f"Alternative rock{bass}{guitar}{drum}{synth}{vocal}, Foo Fighters-inspired {mood} vibe with powerful choruses, {rhythm} at {bpm} BPM."
|
| 127 |
+
|
| 128 |
+
def set_smashing_pumpkins_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 129 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("dynamic rhythmic steps" if bpm > 120 else "dreamy rhythmic flow")
|
| 130 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 131 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 132 |
+
bass = f", {bass_style}" if bass_style != "none" else ""
|
| 133 |
+
guitar = f", {guitar_style} guitar textures" if guitar_style != "none" else ", dreamy guitar textures"
|
| 134 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 135 |
+
return f"Alternative rock{bass}{guitar}{drum}{synth}{vocal}, Smashing Pumpkins-inspired blend of melancholy and aggression, {rhythm} at {bpm} BPM."
|
| 136 |
+
|
| 137 |
+
def set_radiohead_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 138 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("complex rhythmic steps" if bpm > 120 else "intricate rhythmic pulse")
|
| 139 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 140 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ", atmospheric synths"
|
| 141 |
+
bass = f", {bass_style}" if bass_style != "none" else ""
|
| 142 |
+
guitar = f", {guitar_style} guitar layers" if guitar_style != "none" else ", intricate guitar layers"
|
| 143 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 144 |
+
return f"Experimental rock{bass}{guitar}{drum}{synth}{vocal}, Radiohead-inspired blend of introspective and innovative soundscapes, {rhythm} at {bpm} BPM."
|
| 145 |
+
|
| 146 |
+
def set_classic_rock_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 147 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("bluesy rhythmic steps" if bpm > 120 else "steady rhythmic groove")
|
| 148 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 149 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 150 |
+
bass = f", {bass_style}" if bass_style != "none" else ", groovy bass"
|
| 151 |
+
guitar = f", {guitar_style} electric guitars" if guitar_style != "none" else ", bluesy electric guitars"
|
| 152 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 153 |
+
return f"Classic rock{bass}{guitar}{drum}{synth}{vocal}, Led Zeppelin-inspired raw energy with dynamic solos, {rhythm} at {bpm} BPM."
|
| 154 |
+
|
| 155 |
+
def set_alternative_rock_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 156 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("quirky rhythmic steps" if bpm > 120 else "energetic rhythmic flow")
|
| 157 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 158 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 159 |
+
bass = f", {bass_style}" if bass_style != "none" else ", melodic basslines"
|
| 160 |
+
guitar = f", {guitar_style} guitar riffs" if guitar_style != "none" else ", distorted guitar riffs"
|
| 161 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 162 |
+
return f"Alternative rock{bass}{guitar}{drum}{synth}{vocal}, Pixies-inspired quirky, energetic vibe, {rhythm} at {bpm} BPM."
|
| 163 |
+
|
| 164 |
+
def set_post_punk_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 165 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("sharp rhythmic steps" if bpm > 120 else "moody rhythmic pulse")
|
| 166 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 167 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 168 |
+
bass = f", {bass_style}" if bass_style != "none" else ", driving basslines"
|
| 169 |
+
guitar = f", {guitar_style} guitars" if guitar_style != "none" else ", jangly guitars"
|
| 170 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 171 |
+
return f"Post-punk{bass}{guitar}{drum}{synth}{vocal}, Joy Division-inspired moody, atmospheric sound, {rhythm} at {bpm} BPM."
|
| 172 |
+
|
| 173 |
+
def set_indie_rock_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 174 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("catchy rhythmic steps" if bpm > 120 else "jangly rhythmic flow")
|
| 175 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 176 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 177 |
+
bass = f", {bass_style}" if bass_style != "none" else ""
|
| 178 |
+
guitar = f", {guitar_style} guitars" if guitar_style != "none" else ", jangly guitars"
|
| 179 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ", heartfelt vocals"
|
| 180 |
+
return f"Indie rock{bass}{guitar}{drum}{synth}{vocal}, Arctic Monkeys-inspired blend of witty lyrics and catchy riffs, {rhythm} at {bpm} BPM."
|
| 181 |
+
|
| 182 |
+
def set_funk_rock_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 183 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("aggressive rhythmic steps" if bpm > 120 else "funky rhythmic groove")
|
| 184 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 185 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ""
|
| 186 |
+
bass = f", {bass_style}" if bass_style != "none" else ", slap bass"
|
| 187 |
+
guitar = f", {guitar_style} guitar chords" if guitar_style != "none" else ", funky guitar chords"
|
| 188 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 189 |
+
return f"Funk rock{bass}{guitar}{drum}{synth}{vocal}, Rage Against the Machine-inspired mix of groove and aggression, {rhythm} at {bpm} BPM."
|
| 190 |
+
|
| 191 |
+
def set_detroit_techno_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 192 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("pulsing rhythmic steps" if bpm > 120 else "deep rhythmic groove")
|
| 193 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ", crisp hi-hats"
|
| 194 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ", deep pulsing synths"
|
| 195 |
+
bass = f", {bass_style}" if bass_style != "none" else ", driving basslines"
|
| 196 |
+
guitar = f", {guitar_style} guitars" if guitar_style != "none" else ""
|
| 197 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ""
|
| 198 |
+
return f"Detroit techno{bass}{guitar}{drum}{synth}{vocal}, Juan Atkins-inspired rhythmic groove, {rhythm} at {bpm} BPM."
|
| 199 |
+
|
| 200 |
+
def set_deep_house_prompt(bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence):
|
| 201 |
+
rhythm = f" with {rhythmic_steps}" if rhythmic_steps != "none" else ("soulful rhythmic steps" if bpm > 120 else "laid-back rhythmic flow")
|
| 202 |
+
drum = f", {drum_beat} drums" if drum_beat != "none" else ""
|
| 203 |
+
synth = f", {synthesizer} accents" if synthesizer != "none" else ", warm analog synth chords"
|
| 204 |
+
bass = f", {bass_style}" if bass_style != "none" else ", deep basslines"
|
| 205 |
+
guitar = f", {guitar_style} guitars" if guitar_style != "none" else ""
|
| 206 |
+
vocal = f", {vocal_presence}" if vocal_presence != "none" else ", soulful vocal chops"
|
| 207 |
+
return f"Deep house{bass}{guitar}{drum}{synth}{vocal}, Larry Heard-inspired laid-back groove, {rhythm} at {bpm} BPM."
|
| 208 |
|
| 209 |
# 5) AUDIO PROCESSING FUNCTIONS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
def apply_eq(segment):
|
| 211 |
segment = segment.low_pass_filter(8000)
|
| 212 |
segment = segment.high_pass_filter(80)
|
| 213 |
return segment
|
| 214 |
|
| 215 |
+
def apply_fade(segment, fade_in_duration=1000, fade_out_duration=1000):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
segment = segment.fade_in(fade_in_duration)
|
| 217 |
segment = segment.fade_out(fade_out_duration)
|
| 218 |
return segment
|
| 219 |
|
| 220 |
# 6) GENERATION & I/O FUNCTIONS
|
| 221 |
+
def generate_music(instrumental_prompt: str, cfg_scale: float, total_duration: int, chunk_duration: int, crossfade_duration: int, bpm: int, drum_beat: str, synthesizer: str, rhythmic_steps: str, bass_style: str, guitar_style: str, vocal_presence: str):
|
| 222 |
global musicgen_model
|
| 223 |
if not instrumental_prompt.strip():
|
| 224 |
return None, "⚠️ Please enter a valid instrumental prompt!"
|
| 225 |
try:
|
| 226 |
start_time = time.time()
|
| 227 |
+
total_duration = min(max(total_duration, 5), 30)
|
| 228 |
+
chunk_duration = min(max(chunk_duration, 5), 15)
|
| 229 |
+
num_chunks = max(1, total_duration // chunk_duration)
|
| 230 |
+
chunk_duration = total_duration / num_chunks
|
| 231 |
+
overlap_duration = min(1.0, crossfade_duration / 1000.0)
|
| 232 |
+
generation_duration = chunk_duration + overlap_duration
|
| 233 |
sample_rate = musicgen_model.sample_rate
|
| 234 |
+
audio_segments = []
|
| 235 |
+
|
| 236 |
+
if not check_vram_availability(required_gb=3.5):
|
| 237 |
+
return None, "⚠️ Insufficient VRAM for generation. Reduce total_duration or chunk_duration."
|
| 238 |
+
|
| 239 |
+
print("Generating audio...")
|
| 240 |
+
seed = 42
|
| 241 |
+
torch.manual_seed(seed)
|
| 242 |
+
np.random.seed(seed)
|
| 243 |
+
|
| 244 |
+
for i in range(num_chunks):
|
| 245 |
+
chunk_prompt = instrumental_prompt
|
| 246 |
+
print(f"Generating chunk {i+1}/{num_chunks} on GPU (prompt: {chunk_prompt})...")
|
| 247 |
+
musicgen_model.set_generation_params(
|
| 248 |
+
duration=generation_duration,
|
| 249 |
+
use_sampling=True,
|
| 250 |
+
top_k=250,
|
| 251 |
+
top_p=0.9,
|
| 252 |
+
temperature=1.0,
|
| 253 |
+
cfg_coef=cfg_scale
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
print_resource_usage(f"Before Chunk {i+1} Generation")
|
| 257 |
+
|
| 258 |
+
with torch.no_grad():
|
| 259 |
+
with autocast():
|
| 260 |
+
audio_chunk = musicgen_model.generate([chunk_prompt], progress=True)[0]
|
| 261 |
+
|
| 262 |
+
audio_chunk = audio_chunk.cpu().to(dtype=torch.float32)
|
| 263 |
+
if audio_chunk.dim() == 1:
|
| 264 |
+
audio_chunk = torch.stack([audio_chunk, audio_chunk], dim=0)
|
| 265 |
+
elif audio_chunk.dim() == 2 and audio_chunk.shape[0] == 1:
|
| 266 |
+
audio_chunk = torch.cat([audio_chunk, audio_chunk], dim=0)
|
| 267 |
+
elif audio_chunk.dim() == 2 and audio_chunk.shape[0] != 2:
|
| 268 |
+
audio_chunk = audio_chunk[:1, :]
|
| 269 |
+
audio_chunk = torch.cat([audio_chunk, audio_chunk], dim=0)
|
| 270 |
+
elif audio_chunk.dim() > 2:
|
| 271 |
+
audio_chunk = audio_chunk.view(2, -1)
|
| 272 |
+
|
| 273 |
+
if audio_chunk.shape[0] != 2:
|
| 274 |
+
raise ValueError(f"Expected stereo audio with shape (2, samples), got shape {audio_chunk.shape}")
|
| 275 |
+
|
| 276 |
+
temp_wav_path = f"temp_chunk_{i}.wav"
|
| 277 |
+
torchaudio.save(temp_wav_path, audio_chunk, sample_rate, bits_per_sample=24)
|
| 278 |
+
segment = AudioSegment.from_wav(temp_wav_path)
|
| 279 |
+
os.remove(temp_wav_path)
|
| 280 |
+
audio_segments.append(segment)
|
| 281 |
+
|
| 282 |
+
torch.cuda.empty_cache()
|
| 283 |
+
gc.collect()
|
| 284 |
+
torch.cuda.ipc_collect()
|
| 285 |
+
torch.cuda.synchronize()
|
| 286 |
+
time.sleep(0.5)
|
| 287 |
+
print_resource_usage(f"After Chunk {i+1} Generation")
|
| 288 |
+
|
| 289 |
+
print("Combining audio chunks...")
|
| 290 |
+
final_segment = audio_segments[0]
|
| 291 |
+
for i in range(1, len(audio_segments)):
|
| 292 |
+
next_segment = audio_segments[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
next_segment = next_segment + 1
|
| 294 |
+
final_segment = final_segment.append(next_segment, crossfade=crossfade_duration)
|
| 295 |
+
|
| 296 |
+
final_segment = final_segment[:total_duration * 1000]
|
| 297 |
|
|
|
|
| 298 |
print("Post-processing final track...")
|
| 299 |
+
final_segment = apply_eq(final_segment)
|
| 300 |
+
final_segment = final_segment.normalize(headroom=-9.0)
|
| 301 |
+
final_segment = apply_fade(final_segment)
|
|
|
|
|
|
|
| 302 |
|
| 303 |
mp3_path = "output_cleaned.mp3"
|
| 304 |
+
final_segment.export(
|
| 305 |
mp3_path,
|
| 306 |
format="mp3",
|
| 307 |
+
bitrate="128k",
|
| 308 |
tags={"title": "GhostAI Instrumental", "artist": "GhostAI"}
|
| 309 |
)
|
| 310 |
print(f"Saved final audio to {mp3_path}")
|
|
|
|
| 311 |
|
| 312 |
print_resource_usage("After Final Generation")
|
| 313 |
print(f"Total Generation Time: {time.time() - start_time:.2f} seconds")
|
| 314 |
|
| 315 |
+
return mp3_path, "✅ Done! Generated audio."
|
|
|
|
| 316 |
except Exception as e:
|
| 317 |
return None, f"❌ Generation failed: {e}"
|
| 318 |
finally:
|
| 319 |
torch.cuda.empty_cache()
|
| 320 |
gc.collect()
|
| 321 |
+
torch.cuda.ipc_collect()
|
| 322 |
torch.cuda.synchronize()
|
| 323 |
|
| 324 |
+
# Function to clear inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
def clear_inputs():
|
| 326 |
+
return "", 3.0, 10, 10, 1000, 120, "none", "none", "none", "none", "none", "none"
|
| 327 |
|
| 328 |
# 7) CUSTOM CSS
|
| 329 |
css = """
|
|
|
|
| 434 |
step=0.1,
|
| 435 |
info="Higher values make the instrumental more closely follow the prompt."
|
| 436 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
total_duration = gr.Slider(
|
| 438 |
label="Total Duration (seconds)",
|
| 439 |
+
minimum=5,
|
| 440 |
+
maximum=30,
|
| 441 |
+
value=10,
|
| 442 |
step=1,
|
| 443 |
+
info="Total duration of the track (5 to 30 seconds)."
|
| 444 |
)
|
| 445 |
+
chunk_duration = gr.Slider(
|
| 446 |
+
label="Chunk Duration (seconds)",
|
| 447 |
+
minimum=5,
|
| 448 |
+
maximum=15,
|
| 449 |
+
value=10,
|
| 450 |
+
step=1,
|
| 451 |
+
info="Duration of each chunk to render (5 to 15 seconds)."
|
| 452 |
)
|
| 453 |
+
crossfade_duration = gr.Slider(
|
| 454 |
+
label="Crossfade Duration (ms)",
|
| 455 |
minimum=100,
|
| 456 |
maximum=2000,
|
| 457 |
value=1000,
|
| 458 |
step=100,
|
| 459 |
+
info="Crossfade duration between chunks."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
)
|
| 461 |
bpm = gr.Slider(
|
| 462 |
label="Tempo (BPM)",
|
|
|
|
| 466 |
step=1,
|
| 467 |
info="Beats per minute to influence the track's tempo."
|
| 468 |
)
|
| 469 |
+
drum_beat = gr.Dropdown(
|
| 470 |
+
label="Drum Beat",
|
| 471 |
+
choices=["none", "standard rock", "funk groove", "techno kick", "jazz swing"],
|
| 472 |
+
value="none",
|
| 473 |
+
info="Select a drum beat style to influence the rhythm."
|
| 474 |
+
)
|
| 475 |
+
synthesizer = gr.Dropdown(
|
| 476 |
+
label="Synthesizer",
|
| 477 |
+
choices=["none", "analog synth", "digital pad", "arpeggiated synth"],
|
| 478 |
+
value="none",
|
| 479 |
+
info="Select a synthesizer style to add electronic accents."
|
| 480 |
+
)
|
| 481 |
+
rhythmic_steps = gr.Dropdown(
|
| 482 |
+
label="Rhythmic Steps",
|
| 483 |
+
choices=["none", "syncopated steps", "steady steps", "complex steps"],
|
| 484 |
+
value="none",
|
| 485 |
+
info="Select a rhythmic step style to enhance the beat."
|
| 486 |
+
)
|
| 487 |
+
bass_style = gr.Dropdown(
|
| 488 |
+
label="Bass Style",
|
| 489 |
+
choices=["none", "slap bass", "deep bass", "melodic bass"],
|
| 490 |
+
value="none",
|
| 491 |
+
info="Select a bass style to shape the low end."
|
| 492 |
+
)
|
| 493 |
+
guitar_style = gr.Dropdown(
|
| 494 |
+
label="Guitar Style",
|
| 495 |
+
choices=["none", "distorted", "clean", "jangle"],
|
| 496 |
+
value="none",
|
| 497 |
+
info="Select a guitar style to define the riffs."
|
| 498 |
+
)
|
| 499 |
+
vocal_presence = gr.Dropdown(
|
| 500 |
+
label="Vocal Presence",
|
| 501 |
+
choices=["none", "background vocals", "vocal chops"],
|
| 502 |
+
value="none",
|
| 503 |
+
info="Select a vocal style to add vocal elements."
|
| 504 |
+
)
|
| 505 |
with gr.Row(elem_classes="action-buttons"):
|
| 506 |
gen_btn = gr.Button("Generate Music")
|
| 507 |
clr_btn = gr.Button("Clear Inputs")
|
|
|
|
| 510 |
out_audio = gr.Audio(label="Generated Stereo Instrumental Track", type="filepath")
|
| 511 |
status = gr.Textbox(label="Status", interactive=False)
|
| 512 |
|
| 513 |
+
rhcp_btn.click(set_red_hot_chili_peppers_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 514 |
+
nirvana_btn.click(set_nirvana_grunge_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 515 |
+
pearl_jam_btn.click(set_pearl_jam_grunge_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 516 |
+
soundgarden_btn.click(set_soundgarden_grunge_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 517 |
+
foo_fighters_btn.click(set_foo_fighters_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 518 |
+
smashing_pumpkins_btn.click(set_smashing_pumpkins_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 519 |
+
radiohead_btn.click(set_radiohead_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 520 |
+
classic_rock_btn.click(set_classic_rock_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 521 |
+
alternative_rock_btn.click(set_alternative_rock_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 522 |
+
post_punk_btn.click(set_post_punk_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 523 |
+
indie_rock_btn.click(set_indie_rock_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 524 |
+
funk_rock_btn.click(set_funk_rock_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 525 |
+
detroit_techno_btn.click(set_detroit_techno_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
| 526 |
+
deep_house_btn.click(set_deep_house_prompt, inputs=[bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence], outputs=instrumental_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
gen_btn.click(
|
| 528 |
generate_music,
|
| 529 |
+
inputs=[instrumental_prompt, cfg_scale, total_duration, chunk_duration, crossfade_duration, bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence],
|
| 530 |
outputs=[out_audio, status]
|
| 531 |
)
|
| 532 |
clr_btn.click(
|
| 533 |
clear_inputs,
|
| 534 |
inputs=None,
|
| 535 |
+
outputs=[instrumental_prompt, cfg_scale, total_duration, chunk_duration, crossfade_duration, bpm, drum_beat, synthesizer, rhythmic_steps, bass_style, guitar_style, vocal_presence]
|
| 536 |
)
|
| 537 |
|
| 538 |
# 9) TURN OFF OPENAPI/DOCS
|