Update demos/musicgen_app.py
Browse files- demos/musicgen_app.py +44 -13
demos/musicgen_app.py
CHANGED
|
@@ -7,7 +7,7 @@ import subprocess as sp
|
|
| 7 |
import sys
|
| 8 |
import time
|
| 9 |
import typing as tp
|
| 10 |
-
from tempfile import NamedTemporaryFile
|
| 11 |
|
| 12 |
from einops import rearrange
|
| 13 |
import torch
|
|
@@ -18,11 +18,11 @@ from audiocraft.data.audio import audio_write
|
|
| 18 |
from audiocraft.models.encodec import InterleaveStereoCompressionModel
|
| 19 |
from audiocraft.models import MusicGen, MultiBandDiffusion
|
| 20 |
import multiprocessing as mp
|
|
|
|
| 21 |
|
| 22 |
# --- Utility Functions and Classes ---
|
| 23 |
-
import warnings # <--- Import the warnings module!
|
| 24 |
|
| 25 |
-
class FileCleaner:
|
| 26 |
def __init__(self, file_lifetime: float = 3600):
|
| 27 |
self.file_lifetime = file_lifetime
|
| 28 |
self.files = []
|
|
@@ -42,15 +42,38 @@ class FileCleaner: # Unchanged
|
|
| 42 |
break
|
| 43 |
file_cleaner = FileCleaner()
|
| 44 |
|
| 45 |
-
def make_waveform(*args, **kwargs): # Unchanged
|
| 46 |
-
be = time.time()
|
| 47 |
-
with warnings.catch_warnings():
|
| 48 |
-
warnings.simplefilter('ignore')
|
| 49 |
-
out = gr.make_waveform(*args, **kwargs)
|
| 50 |
-
print("Make a video took", time.time() - be)
|
| 51 |
-
return out
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def model_worker(model_name: str, task_queue: mp.Queue, result_queue: mp.Queue):
|
| 56 |
"""
|
|
@@ -257,8 +280,12 @@ def predict_full(model, model_path, use_mbd, text, melody, duration, topk, topp,
|
|
| 257 |
loudness_headroom_db=16, loudness_compressor=True, add_suffix=False
|
| 258 |
)
|
| 259 |
wav_paths.append(file.name)
|
| 260 |
-
|
|
|
|
|
|
|
| 261 |
file_cleaner.add(file.name)
|
|
|
|
|
|
|
| 262 |
|
| 263 |
# Save MBD output if used
|
| 264 |
if diffusion_wav is not None:
|
|
@@ -268,8 +295,12 @@ def predict_full(model, model_path, use_mbd, text, melody, duration, topk, topp,
|
|
| 268 |
loudness_headroom_db=16, loudness_compressor=True, add_suffix=False
|
| 269 |
)
|
| 270 |
wav_paths.append(file.name)
|
| 271 |
-
|
|
|
|
|
|
|
| 272 |
file_cleaner.add(file.name)
|
|
|
|
|
|
|
| 273 |
# Shutdown predictor to prevent hanging processes!
|
| 274 |
|
| 275 |
if not predictor.is_daemon: # Important!
|
|
|
|
| 7 |
import sys
|
| 8 |
import time
|
| 9 |
import typing as tp
|
| 10 |
+
from tempfile import NamedTemporaryFile, gettempdir
|
| 11 |
|
| 12 |
from einops import rearrange
|
| 13 |
import torch
|
|
|
|
| 18 |
from audiocraft.models.encodec import InterleaveStereoCompressionModel
|
| 19 |
from audiocraft.models import MusicGen, MultiBandDiffusion
|
| 20 |
import multiprocessing as mp
|
| 21 |
+
import warnings
|
| 22 |
|
| 23 |
# --- Utility Functions and Classes ---
|
|
|
|
| 24 |
|
| 25 |
+
class FileCleaner:
|
| 26 |
def __init__(self, file_lifetime: float = 3600):
|
| 27 |
self.file_lifetime = file_lifetime
|
| 28 |
self.files = []
|
|
|
|
| 42 |
break
|
| 43 |
file_cleaner = FileCleaner()
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
def convert_wav_to_mp4(wav_path, output_path=None):
|
| 47 |
+
"""Converts a WAV file to a waveform MP4 video using ffmpeg."""
|
| 48 |
+
if output_path is None:
|
| 49 |
+
# Create output path in the same directory as the input
|
| 50 |
+
output_path = Path(wav_path).with_suffix(".mp4")
|
| 51 |
+
try:
|
| 52 |
+
command = [
|
| 53 |
+
"ffmpeg",
|
| 54 |
+
"-y", # Overwrite output file if it exists
|
| 55 |
+
"-i", str(wav_path),
|
| 56 |
+
"-filter_complex",
|
| 57 |
+
"[0:a]showwaves=s=1280x202:mode=line,format=yuv420p[v]", # Waveform filter
|
| 58 |
+
"-map", "[v]",
|
| 59 |
+
"-map", "0:a",
|
| 60 |
+
"-c:v", "libx264", # Video codec
|
| 61 |
+
"-c:a", "aac", # Audio codec
|
| 62 |
+
"-preset", "fast", # Important, don't do veryslow.
|
| 63 |
+
str(output_path),
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
process = sp.run(command, capture_output=True, text=True, check=True)
|
| 67 |
+
return str(output_path)
|
| 68 |
+
|
| 69 |
+
except sp.CalledProcessError as e:
|
| 70 |
+
print(f"Error in ffmpeg conversion: {e}")
|
| 71 |
+
print(f"ffmpeg stdout: {e.stdout}")
|
| 72 |
+
print(f"ffmpeg stderr: {e.stderr}")
|
| 73 |
+
raise # Re-raise the exception to be caught by Gradio
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# --- Worker Process ---
|
| 77 |
|
| 78 |
def model_worker(model_name: str, task_queue: mp.Queue, result_queue: mp.Queue):
|
| 79 |
"""
|
|
|
|
| 280 |
loudness_headroom_db=16, loudness_compressor=True, add_suffix=False
|
| 281 |
)
|
| 282 |
wav_paths.append(file.name)
|
| 283 |
+
# Make and clean up video:
|
| 284 |
+
video_path = convert_wav_to_mp4(file.name)
|
| 285 |
+
video_paths.append(video_path)
|
| 286 |
file_cleaner.add(file.name)
|
| 287 |
+
file_cleaner.add(video_path)
|
| 288 |
+
|
| 289 |
|
| 290 |
# Save MBD output if used
|
| 291 |
if diffusion_wav is not None:
|
|
|
|
| 295 |
loudness_headroom_db=16, loudness_compressor=True, add_suffix=False
|
| 296 |
)
|
| 297 |
wav_paths.append(file.name)
|
| 298 |
+
# Make and clean up video:
|
| 299 |
+
video_path = convert_wav_to_mp4(file.name)
|
| 300 |
+
video_paths.append(video_path)
|
| 301 |
file_cleaner.add(file.name)
|
| 302 |
+
file_cleaner.add(video_path)
|
| 303 |
+
|
| 304 |
# Shutdown predictor to prevent hanging processes!
|
| 305 |
|
| 306 |
if not predictor.is_daemon: # Important!
|