Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import os, sys, re, json
|
| 2 |
import argparse
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
from pdb import set_trace as b
|
| 5 |
from pprint import pprint as pp
|
| 6 |
from profanity_check import predict, predict_prob
|
|
@@ -8,7 +10,6 @@ from pydub import AudioSegment
|
|
| 8 |
from pydub.playback import play
|
| 9 |
from subprocess import Popen, PIPE
|
| 10 |
|
| 11 |
-
|
| 12 |
def parse_args():
|
| 13 |
"""
|
| 14 |
"""
|
|
@@ -30,7 +31,7 @@ def parse_args():
|
|
| 30 |
'--model',
|
| 31 |
default='small',
|
| 32 |
nargs='?',
|
| 33 |
-
help=("model used by whisper for speech recognition: tiny, small (default)
|
| 34 |
)
|
| 35 |
parser.add_argument(
|
| 36 |
'-p',
|
|
@@ -42,15 +43,14 @@ def parse_args():
|
|
| 42 |
parser.add_argument(
|
| 43 |
'-v',
|
| 44 |
'--verbose',
|
| 45 |
-
default=
|
| 46 |
action='store_true',
|
| 47 |
help=("print transcribed text and detected profanities to screen")
|
| 48 |
)
|
| 49 |
return parser.parse_args()
|
| 50 |
|
| 51 |
|
| 52 |
-
|
| 53 |
-
def main(args, input_file=None, model_size=None, verbose=False, play_output=False):
|
| 54 |
"""
|
| 55 |
"""
|
| 56 |
if not input_file:
|
|
@@ -66,26 +66,24 @@ def main(args, input_file=None, model_size=None, verbose=False, play_output=Fals
|
|
| 66 |
play_output = args.play
|
| 67 |
|
| 68 |
# exit if input file not found
|
| 69 |
-
if not os.path.isfile(input_file):
|
| 70 |
print('Error: --input file not found')
|
| 71 |
-
|
| 72 |
|
| 73 |
print(f'\nProcessing input file: {input_file}')
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
#text = json.loads('\n'.join(stdout.decode('utf8').split('\n')[1:]))
|
| 87 |
|
| 88 |
-
import whisper_timestamped as wt
|
| 89 |
audio = wt.load_audio(vocal_stem)
|
| 90 |
model = wt.load_model(model_size, device='cpu')
|
| 91 |
text = wt.transcribe(model, audio, language='en')
|
|
@@ -99,7 +97,8 @@ def main(args, input_file=None, model_size=None, verbose=False, play_output=Fals
|
|
| 99 |
profanities = profanity_detection(text)
|
| 100 |
if not profanities:
|
| 101 |
print(f'No profanities found in {input_file} - exiting')
|
| 102 |
-
|
|
|
|
| 103 |
if verbose:
|
| 104 |
print('profanities found in text:')
|
| 105 |
pp(profanities)
|
|
@@ -110,7 +109,10 @@ def main(args, input_file=None, model_size=None, verbose=False, play_output=Fals
|
|
| 110 |
|
| 111 |
# re-mixing
|
| 112 |
print('Merge instrumentals stem and masked vocals stem')
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# write mix to file
|
| 116 |
outpath = input_file.replace('.mp3', '_masked.mp3').replace('.wav', '_masked.wav')
|
|
@@ -125,29 +127,37 @@ def main(args, input_file=None, model_size=None, verbose=False, play_output=Fals
|
|
| 125 |
print('\nPlaying output...')
|
| 126 |
play(mix)
|
| 127 |
|
| 128 |
-
return outpath
|
| 129 |
|
| 130 |
|
| 131 |
-
def source_separation(inpath):
|
| 132 |
"""
|
| 133 |
Execute shell command to run demucs and pipe stdout/stderr back to python
|
| 134 |
"""
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
stdout, stderr = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True, executable='/bin/bash').communicate()
|
| 137 |
stdout = stdout.decode('utf8')
|
| 138 |
|
| 139 |
-
# exit if
|
| 140 |
if stderr:
|
| 141 |
stderr = stderr.decode('utf-8').lower()
|
| 142 |
if 'error' in stderr or 'not exist' in stderr:
|
| 143 |
print(stderr.decode('utf8').split('\n')[0])
|
| 144 |
-
|
| 145 |
|
| 146 |
# parse stems directory path from stdout and return it if successful
|
| 147 |
-
stems_dir = ''.join(re.findall('/.*', stdout)).replace('.mp3','').replace('.wav','').replace('samples/','')
|
| 148 |
if not os.path.isdir(stems_dir):
|
| 149 |
print(f'Error: output stem directory "{stems_dir}" not found')
|
| 150 |
-
|
| 151 |
|
| 152 |
return stems_dir
|
| 153 |
|
|
@@ -164,7 +174,7 @@ def profanity_detection(text):
|
|
| 164 |
text = word['text'].replace('.','').replace(',','').lower()
|
| 165 |
|
| 166 |
# skip false positives
|
| 167 |
-
if text in ['cancer',
|
| 168 |
continue
|
| 169 |
|
| 170 |
# assume anything returned by whisper with more than 1 * is profanity e.g n***a
|
|
@@ -202,35 +212,72 @@ def mask_profanities(vocal_stem, profanities):
|
|
| 202 |
return vocals
|
| 203 |
|
| 204 |
|
| 205 |
-
|
| 206 |
if __name__ == "__main__":
|
| 207 |
args = parse_args()
|
| 208 |
|
| 209 |
if len(sys.argv)>1:
|
| 210 |
-
main(args)
|
| 211 |
else:
|
| 212 |
import streamlit as st
|
| 213 |
st.title('Saylss')
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
if uploaded_file is not None:
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
# run code
|
| 225 |
with st.spinner('Processing input audio...'):
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# display output audio
|
| 229 |
#st.text('Play output Track:')
|
| 230 |
st.text('\nOutput:')
|
| 231 |
audio_file = open(outpath, 'rb')
|
| 232 |
audio_bytes = audio_file.read()
|
| 233 |
-
st.audio(audio_bytes, format=
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, sys, re, json
|
| 2 |
import argparse
|
| 3 |
+
import shutil
|
| 4 |
+
import warnings
|
| 5 |
+
import whisper_timestamped as wt
|
| 6 |
from pdb import set_trace as b
|
| 7 |
from pprint import pprint as pp
|
| 8 |
from profanity_check import predict, predict_prob
|
|
|
|
| 10 |
from pydub.playback import play
|
| 11 |
from subprocess import Popen, PIPE
|
| 12 |
|
|
|
|
| 13 |
def parse_args():
|
| 14 |
"""
|
| 15 |
"""
|
|
|
|
| 31 |
'--model',
|
| 32 |
default='small',
|
| 33 |
nargs='?',
|
| 34 |
+
help=("model used by whisper for speech recognition: tiny, small (default) or medium")
|
| 35 |
)
|
| 36 |
parser.add_argument(
|
| 37 |
'-p',
|
|
|
|
| 43 |
parser.add_argument(
|
| 44 |
'-v',
|
| 45 |
'--verbose',
|
| 46 |
+
default=True,
|
| 47 |
action='store_true',
|
| 48 |
help=("print transcribed text and detected profanities to screen")
|
| 49 |
)
|
| 50 |
return parser.parse_args()
|
| 51 |
|
| 52 |
|
| 53 |
+
def main(args, input_file=None, model_size=None, verbose=False, play_output=False, skip_ss=False):
|
|
|
|
| 54 |
"""
|
| 55 |
"""
|
| 56 |
if not input_file:
|
|
|
|
| 66 |
play_output = args.play
|
| 67 |
|
| 68 |
# exit if input file not found
|
| 69 |
+
if len(sys.argv)>1 and not os.path.isfile(input_file):
|
| 70 |
print('Error: --input file not found')
|
| 71 |
+
raise Exception
|
| 72 |
|
| 73 |
print(f'\nProcessing input file: {input_file}')
|
| 74 |
|
| 75 |
+
if not skip_ss:
|
| 76 |
+
# split audio into vocals + accompaniment
|
| 77 |
+
print('Running source separation')
|
| 78 |
+
stems_dir = source_separation(input_file, use_demucs=False, use_spleeter=True)
|
| 79 |
+
vocal_stem = os.path.join(stems_dir, 'vocals.wav')
|
| 80 |
+
#instr_stem = os.path.join(stems_dir, 'no_vocals.wav') # demucs
|
| 81 |
+
instr_stem = os.path.join(stems_dir, 'accompaniment.wav') # spleeter
|
| 82 |
+
print(f'Vocal stem written to: {vocal_stem}')
|
| 83 |
+
else:
|
| 84 |
+
vocal_stem = input_file
|
| 85 |
+
instr_stem = None
|
|
|
|
| 86 |
|
|
|
|
| 87 |
audio = wt.load_audio(vocal_stem)
|
| 88 |
model = wt.load_model(model_size, device='cpu')
|
| 89 |
text = wt.transcribe(model, audio, language='en')
|
|
|
|
| 97 |
profanities = profanity_detection(text)
|
| 98 |
if not profanities:
|
| 99 |
print(f'No profanities found in {input_file} - exiting')
|
| 100 |
+
return 'No profanities found', None, None
|
| 101 |
+
|
| 102 |
if verbose:
|
| 103 |
print('profanities found in text:')
|
| 104 |
pp(profanities)
|
|
|
|
| 109 |
|
| 110 |
# re-mixing
|
| 111 |
print('Merge instrumentals stem and masked vocals stem')
|
| 112 |
+
if not skip_ss:
|
| 113 |
+
mix = AudioSegment.from_wav(instr_stem).overlay(vocals)
|
| 114 |
+
else:
|
| 115 |
+
mix = vocals
|
| 116 |
|
| 117 |
# write mix to file
|
| 118 |
outpath = input_file.replace('.mp3', '_masked.mp3').replace('.wav', '_masked.wav')
|
|
|
|
| 127 |
print('\nPlaying output...')
|
| 128 |
play(mix)
|
| 129 |
|
| 130 |
+
return outpath, vocal_stem, instr_stem
|
| 131 |
|
| 132 |
|
| 133 |
+
def source_separation(inpath, use_demucs=False, use_spleeter=True):
|
| 134 |
"""
|
| 135 |
Execute shell command to run demucs and pipe stdout/stderr back to python
|
| 136 |
"""
|
| 137 |
+
infile = os.path.basename(inpath)
|
| 138 |
+
|
| 139 |
+
if use_demucs:
|
| 140 |
+
cmd = f'demucs --two-stems=vocals --jobs 8 "{inpath}"'
|
| 141 |
+
#stems_dir = os.path.join(re.findall('/.*', stdout)[0], infile.replace('.mp3','').replace('.wav',''))
|
| 142 |
+
elif use_spleeter:
|
| 143 |
+
outdir = 'audio/separated'
|
| 144 |
+
cmd = f'spleeter separate {inpath} -p spleeter:2stems -o {outdir}'
|
| 145 |
+
stems_dir = os.path.join(outdir, os.path.splitext(infile)[0])
|
| 146 |
+
|
| 147 |
stdout, stderr = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True, executable='/bin/bash').communicate()
|
| 148 |
stdout = stdout.decode('utf8')
|
| 149 |
|
| 150 |
+
# exit if lib error'd out
|
| 151 |
if stderr:
|
| 152 |
stderr = stderr.decode('utf-8').lower()
|
| 153 |
if 'error' in stderr or 'not exist' in stderr:
|
| 154 |
print(stderr.decode('utf8').split('\n')[0])
|
| 155 |
+
raise Exception
|
| 156 |
|
| 157 |
# parse stems directory path from stdout and return it if successful
|
|
|
|
| 158 |
if not os.path.isdir(stems_dir):
|
| 159 |
print(f'Error: output stem directory "{stems_dir}" not found')
|
| 160 |
+
raise Exception
|
| 161 |
|
| 162 |
return stems_dir
|
| 163 |
|
|
|
|
| 174 |
text = word['text'].replace('.','').replace(',','').lower()
|
| 175 |
|
| 176 |
# skip false positives
|
| 177 |
+
if text in ['cancer','hell','junk','die','lame','freak','freaky','white','stink','shut','spit','mouth','orders','eat','clouds','ugly','dirty','wet']:
|
| 178 |
continue
|
| 179 |
|
| 180 |
# assume anything returned by whisper with more than 1 * is profanity e.g n***a
|
|
|
|
| 212 |
return vocals
|
| 213 |
|
| 214 |
|
|
|
|
| 215 |
if __name__ == "__main__":
|
| 216 |
args = parse_args()
|
| 217 |
|
| 218 |
if len(sys.argv)>1:
|
| 219 |
+
main(args, skip_ss=False)
|
| 220 |
else:
|
| 221 |
import streamlit as st
|
| 222 |
st.title('Saylss')
|
| 223 |
+
with st.expander("About", expanded=False):
|
| 224 |
+
st.markdown('''
|
| 225 |
+
This app processes an input audio track (.mp3 or .wav) with the purpose of identifying and muting profanities in the song.
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
A larger model takes longer to run and is more accurate, and vice-versa.
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
Simply select the model size and upload your file!
|
| 232 |
+
''')
|
| 233 |
+
model = st.selectbox('Choose model size:', ('tiny','small','medium'), index=1)
|
| 234 |
+
|
| 235 |
+
uploaded_file = st.file_uploader(
|
| 236 |
+
"Choose input track:",
|
| 237 |
+
type=[".mp3",".wav"],
|
| 238 |
+
accept_multiple_files=False,
|
| 239 |
+
)
|
| 240 |
|
| 241 |
if uploaded_file is not None:
|
| 242 |
+
uploaded_file.name = uploaded_file.name.replace(' ','_')
|
| 243 |
+
ext = os.path.splitext(uploaded_file.name)[1]
|
| 244 |
+
if ext == '.wav':
|
| 245 |
+
st_format = 'audio/wav'
|
| 246 |
+
elif ext == '.mp3':
|
| 247 |
+
st_format = 'audio/mp3'
|
| 248 |
+
|
| 249 |
+
uploaded_file_content = uploaded_file.getvalue()
|
| 250 |
+
with open(uploaded_file.name, 'wb') as f:
|
| 251 |
+
f.write(uploaded_file_content)
|
| 252 |
+
|
| 253 |
+
audio_bytes_input = uploaded_file_content
|
| 254 |
+
st.audio(audio_bytes_input, format=st_format)
|
| 255 |
|
| 256 |
# run code
|
| 257 |
with st.spinner('Processing input audio...'):
|
| 258 |
+
inpath = os.path.abspath(uploaded_file.name)
|
| 259 |
+
outpath, vocal_stem, instr_stem = main(args, input_file=inpath, model_size=model)
|
| 260 |
+
|
| 261 |
+
if outpath == 'No profanities found':
|
| 262 |
+
st.text(outpath + ' - Refresh the page and try a different song or model size')
|
| 263 |
+
sys.exit()
|
| 264 |
|
| 265 |
# display output audio
|
| 266 |
#st.text('Play output Track:')
|
| 267 |
st.text('\nOutput:')
|
| 268 |
audio_file = open(outpath, 'rb')
|
| 269 |
audio_bytes = audio_file.read()
|
| 270 |
+
st.audio(audio_bytes, format=st_format)
|
| 271 |
+
|
| 272 |
+
# flush all media
|
| 273 |
+
if os.path.isfile(inpath):
|
| 274 |
+
os.remove(inpath)
|
| 275 |
+
if os.path.isfile(outpath):
|
| 276 |
+
os.remove(outpath)
|
| 277 |
+
if os.path.isfile(vocal_stem):
|
| 278 |
+
os.remove(vocal_stem)
|
| 279 |
+
if os.path.isfile(instr_stem):
|
| 280 |
+
os.remove(instr_stem)
|
| 281 |
+
sep_dir = os.path.split(instr_stem)[0]
|
| 282 |
+
if os.path.isdir(sep_dir):
|
| 283 |
+
os.rmdir(sep_dir)
|