Skip to content

Commit

Permalink
musicgen_app: make subprocess patching a little more elegant
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Oct 13, 2023
1 parent 5d8752d commit 74aa613
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions demos/musicgen_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from concurrent.futures import ProcessPoolExecutor
import os
from pathlib import Path
import subprocess as sp
import subprocess
from tempfile import NamedTemporaryFile
import time
import typing as tp
import warnings
from unittest import mock

import torch
import gradio as gr
Expand All @@ -32,18 +33,7 @@
BATCHED_DURATION = 15
INTERRUPTING = False
MBD = None
# We have to wrap subprocess call to clean a bit the log when using gr.make_waveform
_old_call = sp.call


def _call_nostderr(*args, **kwargs):
# Avoid ffmpeg vomiting on the logs.
kwargs['stderr'] = sp.DEVNULL
kwargs['stdout'] = sp.DEVNULL
_old_call(*args, **kwargs)


sp.call = _call_nostderr
# Preallocating the pool of processes.
pool = ProcessPoolExecutor(4)
pool.__enter__()
Expand Down Expand Up @@ -77,12 +67,20 @@ def _cleanup(self):
file_cleaner = FileCleaner()


def _call_nostderr(*args, **kwargs):
# Avoid ffmpeg vomiting on the logs.
kwargs['stderr'] = subprocess.DEVNULL
kwargs['stdout'] = subprocess.DEVNULL
return subprocess.call(*args, **kwargs)


def make_waveform(*args, **kwargs):
# Further remove some warnings.
be = time.time()
with warnings.catch_warnings():
warnings.simplefilter('ignore')
out = gr.make_waveform(*args, **kwargs)
with mock.patch('subprocess.call', _call_nostderr):
out = gr.make_waveform(*args, **kwargs)
print("Make a video took", time.time() - be)
return out

Expand Down

0 comments on commit 74aa613

Please sign in to comment.