Skip to content

Commit 065f117

Browse files
committed
musicgen_app: make subprocess patching a little more elegant
1 parent 5905d2e commit 065f117

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

demos/musicgen_app.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
import logging
1313
import os
1414
from pathlib import Path
15-
import subprocess as sp
15+
import subprocess
1616
import sys
1717
from tempfile import NamedTemporaryFile
1818
import time
1919
import typing as tp
2020
import warnings
21+
from unittest import mock
2122

2223
from einops import rearrange
2324
import torch
@@ -37,18 +38,7 @@
3738
BATCHED_DURATION = 15
3839
INTERRUPTING = False
3940
MBD = None
40-
# We have to wrap subprocess call to clean a bit the log when using gr.make_waveform
41-
_old_call = sp.call
4241

43-
44-
def _call_nostderr(*args, **kwargs):
45-
# Avoid ffmpeg vomiting on the logs.
46-
kwargs['stderr'] = sp.DEVNULL
47-
kwargs['stdout'] = sp.DEVNULL
48-
_old_call(*args, **kwargs)
49-
50-
51-
sp.call = _call_nostderr
5242
# Preallocating the pool of processes.
5343
pool = ProcessPoolExecutor(4)
5444
pool.__enter__()
@@ -82,12 +72,20 @@ def _cleanup(self):
8272
file_cleaner = FileCleaner()
8373

8474

75+
def _call_nostderr(*args, **kwargs):
76+
# Avoid ffmpeg vomiting on the logs.
77+
kwargs['stderr'] = subprocess.DEVNULL
78+
kwargs['stdout'] = subprocess.DEVNULL
79+
return subprocess.call(*args, **kwargs)
80+
81+
8582
def make_waveform(*args, **kwargs):
8683
# Further remove some warnings.
8784
be = time.time()
8885
with warnings.catch_warnings():
8986
warnings.simplefilter('ignore')
90-
out = gr.make_waveform(*args, **kwargs)
87+
with mock.patch('subprocess.call', _call_nostderr):
88+
out = gr.make_waveform(*args, **kwargs)
9189
print("Make a video took", time.time() - be)
9290
return out
9391

0 commit comments

Comments
 (0)