Datasets:
File size: 5,509 Bytes
fc10d73 |
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 |
import sys
import os
#from tqdm import tqdm
import torch
import torch.multiprocessing as mp
import threading
#import librosa
#import numpy as np
from faster_whisper import WhisperModel
#import whisper
import glob
import fcntl
import argparse
import traceback
from tqdm import tqdm
import numpy as np
import librosa
import soxr
import multiprocessing
def normalize_audio(y, target_dbfs=0):
max_amplitude = np.max(np.abs(y))
if max_amplitude < 0.1:
return y
target_amplitude = 10.0**(target_dbfs / 20.0)
scale_factor = target_amplitude / max_amplitude
normalized_audio = y * scale_factor
return normalized_audio
file_lock = multiprocessing.Lock()
def inference(rank, ckpt_path, text_path, queue: mp.Queue):
device = f"cuda"
model = WhisperModel(ckpt_path, device=device, device_index=rank, compute_type="float16")
puncs = list(",.?!")
buffer = ""
def write_to_file(data):
with file_lock:
with open(text_path, 'a') as f:
f.write(data)
with torch.no_grad():
while True:
#print(texts)
filename = queue.get()
if filename is None:
write_to_file(buffer)
break
filename = filename[0]
try:
audio_path = filename
audio, sr = librosa.load(audio_path, sr=None)
audio = normalize_audio(audio, -6)
audio = soxr.resample(
audio,
sr,
16000
)
segments, info = model.transcribe(audio, beam_size=3, vad_filter=True, condition_on_previous_text=False)
text = ""
for segment in segments:
text_segment = segment.text
text_segment.strip()
if len(text_segment) == 0:
continue
if not text_segment[-1] in puncs:
text_segment += ","
text = text + " " + text_segment
text = text.replace(" ", " ")
text = text.strip()
if len(text) == 0:
continue
if text[-1] == ",":
text = text[:-1] + "."
buffer += f"{filename}|{text}|{info.language}|{info.language_probability}\n"
if len(buffer) > 10000:
write_to_file(buffer)
buffer = ""
except Exception as e:
print(filename)
traceback.print_exc()
def setInterval(interval):
def decorator(function):
def wrapper(*args, **kwargs):
stopped = threading.Event()
def loop(): # executed in another thread
while not stopped.wait(interval): # until stopped
function(*args, **kwargs)
t = threading.Thread(target=loop)
t.daemon = True # stop if the program exits
t.start()
return stopped
return wrapper
return decorator
last_batches = None
@setInterval(5)
def QueueWatcher(queue, bar):
global last_batches
curr_batches = queue.qsize()
bar.update(last_batches-curr_batches)
last_batches = curr_batches
if __name__ == "__main__":
#audio_dir = sys.argv[1]
parser = argparse.ArgumentParser()
parser.add_argument("--filelist_or_dir", type=str, required=True)
parser.add_argument("--text_path", type=str, required=True, help="Dir to save output")
parser.add_argument("--jobs", type=int, required=False, default=2, help="Path to save checkpoints")
parser.add_argument("--ckpt_path", type=str, required=False, default="large-v3")
parser.add_argument("--log_dir", type=str, required=False, default="large-v3", help="For aml compability")
parser.add_argument("--model_dir", type=str, required=False, default="large-v3", help="For aml compability")
args = parser.parse_args()
mp.set_start_method('spawn',force=True)
filelist_or_dir = args.filelist_or_dir
text_path = args.text_path
jobs = args.jobs
ckpt_path = args.ckpt_path
os.makedirs(text_path, exist_ok=True)
model = WhisperModel(ckpt_path, device='cpu') # download model in one thread
del(model)
if os.path.isfile(filelist_or_dir):
filelist_name = filelist_or_dir.split('/')[-1].split('.')[0]
generator = open(filelist_or_dir).read().splitlines()
text_path = os.path.join(text_path, f"{filelist_name}_text.txt")
else:
filelist_name = "single"
generator = glob.glob(f"{filelist_or_dir}/*.wav")
text_path = os.path.join(text_path, "text.txt")
os.system(f"rm {text_path}")
gpu_num = torch.cuda.device_count()
processes = []
queue = mp.Queue()
for thread_num in range(jobs):
rank = thread_num % gpu_num
p = mp.Process(target=inference, args=(rank, ckpt_path, text_path, queue))
p.start()
processes.append(p)
accum = []
tmp_file = []
for filename in generator:
accum.append(filename)
if len(accum) == 1:
queue.put(accum.copy())
accum.clear()
for _ in range(jobs):
queue.put(None)
last_batches = queue.qsize()
bar = tqdm(total=last_batches, desc='whisper')
queue_watcher = QueueWatcher(queue, bar)
for p in processes:
p.join()
queue_watcher.set() |