Spaces:
Runtime error
Runtime error
File size: 10,182 Bytes
ca70244 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
import gc
import os
import re
from audio_separator.separator import Separator
os.environ["MODELSCOPE_CACHE"] = "./.cache/funasr"
os.environ["UVR5_CACHE"] = "./.cache/uvr5-models"
import json
import subprocess
from pathlib import Path
import click
import torch
from loguru import logger
from pydub import AudioSegment
from silero_vad import get_speech_timestamps, load_silero_vad, read_audio
from tqdm import tqdm
from tools.file import AUDIO_EXTENSIONS, VIDEO_EXTENSIONS, list_files
from tools.sensevoice.auto_model import AutoModel
def uvr5_cli(
audio_dir: Path,
output_folder: Path,
audio_files: list[Path] | None = None,
output_format: str = "flac",
model: str = "BS-Roformer-Viperx-1297.ckpt",
):
# ["BS-Roformer-Viperx-1297.ckpt", "BS-Roformer-Viperx-1296.ckpt", "BS-Roformer-Viperx-1053.ckpt", "Mel-Roformer-Viperx-1143.ckpt"]
sepr = Separator(
model_file_dir=os.environ["UVR5_CACHE"],
output_dir=output_folder,
output_format=output_format,
)
dictmodel = {
"BS-Roformer-Viperx-1297.ckpt": "model_bs_roformer_ep_317_sdr_12.9755.ckpt",
"BS-Roformer-Viperx-1296.ckpt": "model_bs_roformer_ep_368_sdr_12.9628.ckpt",
"BS-Roformer-Viperx-1053.ckpt": "model_bs_roformer_ep_937_sdr_10.5309.ckpt",
"Mel-Roformer-Viperx-1143.ckpt": "model_mel_band_roformer_ep_3005_sdr_11.4360.ckpt",
}
roformer_model = dictmodel[model]
sepr.load_model(roformer_model)
if audio_files is None:
audio_files = list_files(
path=audio_dir, extensions=AUDIO_EXTENSIONS, recursive=True
)
total_files = len(audio_files)
print(f"{total_files} audio files found")
res = []
for audio in tqdm(audio_files, desc="Denoising: "):
file_path = str(audio_dir / audio)
sep_out = sepr.separate(file_path)
if isinstance(sep_out, str):
res.append(sep_out)
elif isinstance(sep_out, list):
res.extend(sep_out)
del sepr
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
return res, roformer_model
def get_sample_rate(media_path: Path):
result = subprocess.run(
[
"ffprobe",
"-v",
"quiet",
"-print_format",
"json",
"-show_streams",
str(media_path),
],
capture_output=True,
text=True,
check=True,
)
media_info = json.loads(result.stdout)
for stream in media_info.get("streams", []):
if stream.get("codec_type") == "audio":
return stream.get("sample_rate")
return "44100" # Default sample rate if not found
def convert_to_mono(src_path: Path, out_path: Path, out_fmt: str = "wav"):
sr = get_sample_rate(src_path)
out_path.parent.mkdir(parents=True, exist_ok=True)
if src_path.resolve() == out_path.resolve():
output = str(out_path.with_stem(out_path.stem + f"_{sr}"))
else:
output = str(out_path)
subprocess.run(
[
"ffmpeg",
"-loglevel",
"error",
"-i",
str(src_path),
"-acodec",
"pcm_s16le" if out_fmt == "wav" else "flac",
"-ar",
sr,
"-ac",
"1",
"-y",
output,
],
check=True,
)
return out_path
def convert_video_to_audio(video_path: Path, audio_dir: Path):
cur_dir = audio_dir / video_path.relative_to(audio_dir).parent
vocals = [
p
for p in cur_dir.glob(f"{video_path.stem}_(Vocals)*.*")
if p.suffix in AUDIO_EXTENSIONS
]
if len(vocals) > 0:
return vocals[0]
audio_path = cur_dir / f"{video_path.stem}.wav"
convert_to_mono(video_path, audio_path)
return audio_path
@click.command()
@click.option("--audio-dir", required=True, help="Directory containing audio files")
@click.option(
"--save-dir", required=True, help="Directory to save processed audio files"
)
@click.option("--device", default="cuda", help="Device to use [cuda / cpu]")
@click.option("--language", default="auto", help="Language of the transcription")
@click.option(
"--max_single_segment_time",
default=20000,
type=int,
help="Maximum of Output single audio duration(ms)",
)
@click.option("--fsmn-vad/--silero-vad", default=False)
@click.option("--punc/--no-punc", default=False)
@click.option("--denoise/--no-denoise", default=False)
@click.option("--save_emo/--no_save_emo", default=False)
def main(
audio_dir: str,
save_dir: str,
device: str,
language: str,
max_single_segment_time: int,
fsmn_vad: bool,
punc: bool,
denoise: bool,
save_emo: bool,
):
audios_path = Path(audio_dir)
save_path = Path(save_dir)
save_path.mkdir(parents=True, exist_ok=True)
video_files = list_files(
path=audio_dir, extensions=VIDEO_EXTENSIONS, recursive=True
)
v2a_files = [convert_video_to_audio(p, audio_dir) for p in video_files]
if denoise:
VOCAL = "_(Vocals)"
original_files = [
p
for p in audios_path.glob("**/*")
if p.suffix in AUDIO_EXTENSIONS and VOCAL not in p.stem
]
_, cur_model = uvr5_cli(
audio_dir=audio_dir, output_folder=audio_dir, audio_files=original_files
)
need_remove = [p for p in audios_path.glob("**/*(Instrumental)*")]
need_remove.extend(original_files)
for _ in need_remove:
_.unlink()
vocal_files = [
p
for p in audios_path.glob("**/*")
if p.suffix in AUDIO_EXTENSIONS and VOCAL in p.stem
]
for f in vocal_files:
fn, ext = f.stem, f.suffix
v_pos = fn.find(VOCAL + "_" + cur_model.split(".")[0])
if v_pos != -1:
new_fn = fn[: v_pos + len(VOCAL)]
new_f = f.with_name(new_fn + ext)
f = f.rename(new_f)
convert_to_mono(f, f, "flac")
f.unlink()
audio_files = list_files(
path=audio_dir, extensions=AUDIO_EXTENSIONS, recursive=True
)
logger.info("Loading / Downloading Funasr model...")
model_dir = "iic/SenseVoiceSmall"
vad_model = "fsmn-vad" if fsmn_vad else None
vad_kwargs = {"max_single_segment_time": max_single_segment_time}
punc_model = "ct-punc" if punc else None
manager = AutoModel(
model=model_dir,
trust_remote_code=False,
vad_model=vad_model,
vad_kwargs=vad_kwargs,
punc_model=punc_model,
device=device,
)
if not fsmn_vad and vad_model is None:
vad_model = load_silero_vad()
logger.info("Model loaded.")
pattern = re.compile(r"_\d{3}\.")
for file_path in tqdm(audio_files, desc="Processing audio file"):
if pattern.search(file_path.name):
# logger.info(f"Skipping {file_path} as it has already been processed.")
continue
file_stem = file_path.stem
file_suffix = file_path.suffix
rel_path = Path(file_path).relative_to(audio_dir)
(save_path / rel_path.parent).mkdir(parents=True, exist_ok=True)
audio = AudioSegment.from_file(file_path)
cfg = dict(
cache={},
language=language, # "zh", "en", "yue", "ja", "ko", "nospeech"
use_itn=False,
batch_size_s=60,
)
if fsmn_vad:
elapsed, vad_res = manager.vad(input=str(file_path), **cfg)
else:
wav = read_audio(
str(file_path)
) # backend (sox, soundfile, or ffmpeg) required!
audio_key = file_path.stem
audio_val = []
speech_timestamps = get_speech_timestamps(
wav,
vad_model,
max_speech_duration_s=max_single_segment_time // 1000,
return_seconds=True,
)
audio_val = [
[int(timestamp["start"] * 1000), int(timestamp["end"] * 1000)]
for timestamp in speech_timestamps
]
vad_res = []
vad_res.append(dict(key=audio_key, value=audio_val))
res = manager.inference_with_vadres(
input=str(file_path), vad_res=vad_res, **cfg
)
for i, info in enumerate(res):
[start_ms, end_ms] = info["interval"]
text = info["text"]
emo = info["emo"]
sliced_audio = audio[start_ms:end_ms]
audio_save_path = (
save_path / rel_path.parent / f"{file_stem}_{i:03d}{file_suffix}"
)
sliced_audio.export(audio_save_path, format=file_suffix[1:])
print(f"Exported {audio_save_path}: {text}")
transcript_save_path = (
save_path / rel_path.parent / f"{file_stem}_{i:03d}.lab"
)
with open(
transcript_save_path,
"w",
encoding="utf-8",
) as f:
f.write(text)
if save_emo:
emo_save_path = save_path / rel_path.parent / f"{file_stem}_{i:03d}.emo"
with open(
emo_save_path,
"w",
encoding="utf-8",
) as f:
f.write(emo)
if audios_path.resolve() == save_path.resolve():
file_path.unlink()
if __name__ == "__main__":
main()
exit(0)
from funasr.utils.postprocess_utils import rich_transcription_postprocess
# Load the audio file
audio_path = Path(r"D:\PythonProject\ok\1_output_(Vocals).wav")
model_dir = "iic/SenseVoiceSmall"
m, kwargs = SenseVoiceSmall.from_pretrained(model=model_dir, device="cuda:0")
m.eval()
res = m.inference(
data_in=f"{kwargs['model_path']}/example/zh.mp3",
language="auto", # "zh", "en", "yue", "ja", "ko", "nospeech"
use_itn=False,
ban_emo_unk=False,
**kwargs,
)
print(res)
text = rich_transcription_postprocess(res[0][0]["text"])
print(text)
|