Update barks.py
Browse files
barks.py
CHANGED
|
@@ -48,13 +48,13 @@ memory_cleanup()
|
|
| 48 |
|
| 49 |
# 2) LOAD MODELS
|
| 50 |
try:
|
| 51 |
-
print("Loading MusicGen medium model into
|
| 52 |
local_model_path = "./models/musicgen-medium"
|
| 53 |
if not os.path.exists(local_model_path):
|
| 54 |
print(f"ERROR: Local model path {local_model_path} does not exist.")
|
| 55 |
print("Please download the MusicGen medium model weights and place them in the correct directory.")
|
| 56 |
sys.exit(1)
|
| 57 |
-
musicgen_model = MusicGen.get_pretrained(local_model_path, device="
|
| 58 |
musicgen_model.set_generation_params(
|
| 59 |
duration=5, # Lower default chunk duration
|
| 60 |
two_step_cfg=False # Disable two-step CFG for stability
|
|
@@ -67,7 +67,7 @@ except Exception as e:
|
|
| 67 |
try:
|
| 68 |
print("Loading Bark small model into system RAM with 4-bit quantization...")
|
| 69 |
bark_processor = AutoProcessor.from_pretrained("suno/bark-small")
|
| 70 |
-
bark_model = BarkModel.from_pretrained("suno/bark-small", load_in_4bit=True, device_map="cpu") # Quantize and offload
|
| 71 |
except Exception as e:
|
| 72 |
print(f"ERROR: Failed to load Bark model: {e}")
|
| 73 |
print("Ensure Bark model weights and bitsandbytes are installed.")
|
|
@@ -326,9 +326,6 @@ def generate_music(instrumental_prompt: str, vocal_prompt: str, cfg_scale: float
|
|
| 326 |
torch.manual_seed(seed)
|
| 327 |
np.random.seed(seed)
|
| 328 |
|
| 329 |
-
# Move MusicGen to GPU
|
| 330 |
-
musicgen_model = musicgen_model.to("cuda")
|
| 331 |
-
|
| 332 |
for i in range(num_chunks):
|
| 333 |
chunk_prompt = instrumental_prompt
|
| 334 |
print(f"Generating chunk {i+1}/{num_chunks} on GPU (prompt: {chunk_prompt})...")
|
|
@@ -370,10 +367,6 @@ def generate_music(instrumental_prompt: str, vocal_prompt: str, cfg_scale: float
|
|
| 370 |
memory_cleanup()
|
| 371 |
print_resource_usage(f"After Chunk {i+1} Generation")
|
| 372 |
|
| 373 |
-
# Move MusicGen back to CPU
|
| 374 |
-
musicgen_model = musicgen_model.to("cpu")
|
| 375 |
-
memory_cleanup()
|
| 376 |
-
|
| 377 |
print("Combining instrumental chunks...")
|
| 378 |
final_segment = audio_segments[0]
|
| 379 |
for i in range(1, len(audio_segments)):
|
|
|
|
| 48 |
|
| 49 |
# 2) LOAD MODELS
|
| 50 |
try:
|
| 51 |
+
print("Loading MusicGen medium model into GPU VRAM...")
|
| 52 |
local_model_path = "./models/musicgen-medium"
|
| 53 |
if not os.path.exists(local_model_path):
|
| 54 |
print(f"ERROR: Local model path {local_model_path} does not exist.")
|
| 55 |
print("Please download the MusicGen medium model weights and place them in the correct directory.")
|
| 56 |
sys.exit(1)
|
| 57 |
+
musicgen_model = MusicGen.get_pretrained(local_model_path, device="cuda") # Load directly to GPU
|
| 58 |
musicgen_model.set_generation_params(
|
| 59 |
duration=5, # Lower default chunk duration
|
| 60 |
two_step_cfg=False # Disable two-step CFG for stability
|
|
|
|
| 67 |
try:
|
| 68 |
print("Loading Bark small model into system RAM with 4-bit quantization...")
|
| 69 |
bark_processor = AutoProcessor.from_pretrained("suno/bark-small")
|
| 70 |
+
bark_model = BarkModel.from_pretrained("suno/bark-small", load_in_4bit=True, device_map="cpu") # Quantize and offload to CPU
|
| 71 |
except Exception as e:
|
| 72 |
print(f"ERROR: Failed to load Bark model: {e}")
|
| 73 |
print("Ensure Bark model weights and bitsandbytes are installed.")
|
|
|
|
| 326 |
torch.manual_seed(seed)
|
| 327 |
np.random.seed(seed)
|
| 328 |
|
|
|
|
|
|
|
|
|
|
| 329 |
for i in range(num_chunks):
|
| 330 |
chunk_prompt = instrumental_prompt
|
| 331 |
print(f"Generating chunk {i+1}/{num_chunks} on GPU (prompt: {chunk_prompt})...")
|
|
|
|
| 367 |
memory_cleanup()
|
| 368 |
print_resource_usage(f"After Chunk {i+1} Generation")
|
| 369 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
print("Combining instrumental chunks...")
|
| 371 |
final_segment = audio_segments[0]
|
| 372 |
for i in range(1, len(audio_segments)):
|