Skip to content

Commit 74aa613

Browse files
committed
musicgen_app: make subprocess patching a little more elegant
1 parent 5d8752d commit 74aa613

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
@@ -11,11 +11,12 @@
1111
from concurrent.futures import ProcessPoolExecutor
1212
import os
1313
from pathlib import Path
14-
import subprocess as sp
14+
import subprocess
1515
from tempfile import NamedTemporaryFile
1616
import time
1717
import typing as tp
1818
import warnings
19+
from unittest import mock
1920

2021
import torch
2122
import gradio as gr
@@ -32,18 +33,7 @@
3233
BATCHED_DURATION = 15
3334
INTERRUPTING = False
3435
MBD = None
35-
# We have to wrap subprocess call to clean a bit the log when using gr.make_waveform
36-
_old_call = sp.call
3736

38-
39-
def _call_nostderr(*args, **kwargs):
40-
# Avoid ffmpeg vomiting on the logs.
41-
kwargs['stderr'] = sp.DEVNULL
42-
kwargs['stdout'] = sp.DEVNULL
43-
_old_call(*args, **kwargs)
44-
45-
46-
sp.call = _call_nostderr
4737
# Preallocating the pool of processes.
4838
pool = ProcessPoolExecutor(4)
4939
pool.__enter__()
@@ -77,12 +67,20 @@ def _cleanup(self):
7767
file_cleaner = FileCleaner()
7868

7969

70+
def _call_nostderr(*args, **kwargs):
71+
# Avoid ffmpeg vomiting on the logs.
72+
kwargs['stderr'] = subprocess.DEVNULL
73+
kwargs['stdout'] = subprocess.DEVNULL
74+
return subprocess.call(*args, **kwargs)
75+
76+
8077
def make_waveform(*args, **kwargs):
8178
# Further remove some warnings.
8279
be = time.time()
8380
with warnings.catch_warnings():
8481
warnings.simplefilter('ignore')
85-
out = gr.make_waveform(*args, **kwargs)
82+
with mock.patch('subprocess.call', _call_nostderr):
83+
out = gr.make_waveform(*args, **kwargs)
8684
print("Make a video took", time.time() - be)
8785
return out
8886

0 commit comments

Comments
 (0)