Repository navigation
Expand file tree
/
Copy pathservice_asr.py
More file actions
172 lines (138 loc) · 5.81 KB
/
Copy pathservice_asr.py
File metadata and controls
172 lines (138 loc) · 5.81 KB
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
# Service that uses an ASR model to recognize the language and transcribe the audio
import numpy as np
import soundfile as sf
from pydub import AudioSegment
from pydub.silence import split_on_silence
import samplerate
from multiprocessing import Process, Queue
import asyncio
# Model
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
import torch
# Logging
from utils import logger
class ASRService:
def __init__(self, command_queue: Queue, response_queue: Queue):
self.command_queue = command_queue
self.response_queue = response_queue
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "openai/whisper-large-v3"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model = model.to_bettertransformer()
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=30,
batch_size=16,
return_timestamps=True,
torch_dtype=torch_dtype,
device=device,
)
# ASR Engine
self.pipe = pipe
self.whisper_sample_rate = 16000
self.fast_resampler = samplerate.Resampler('sinc_fastest', channels=1)
self.best_resampler = samplerate.Resampler('sinc_best', channels=1)
def normalize_audio(self, pcm_data_array, channels, input_sample_rate):
normalized_array = []
for pcm_data in pcm_data_array:
# Squeeze out extra dimensions:
if pcm_data.ndim > 1 and (pcm_data.shape[0] == 1 or pcm_data.shape[1] == 1):
pcm_data = np.squeeze(pcm_data)
# Check if audio is stereo (2 channels). If so, convert it to mono.
if pcm_data.ndim > 1 and pcm_data.shape[1] == 2:
pcm_data = np.mean(pcm_data, axis=1)
elif pcm_data.ndim == 1 and channels == 2:
# Left channel
pcm_data = pcm_data[::2]
if np.issubdtype(pcm_data.dtype, np.floating):
# Convert to int16, PyDub doesn't work with float
pcm_data = (pcm_data * np.iinfo(np.int16).max).astype(np.int16)
normalized_array.append(pcm_data)
pcm_data = np.concatenate(normalized_array)
# Create an audio segment from the data
sound = AudioSegment(
pcm_data.tobytes(),
frame_rate=input_sample_rate,
sample_width=pcm_data.dtype.itemsize,
channels=1
)
# Remove silence from audio clip
chunks = split_on_silence(sound,
min_silence_len=500, # milliseconds
silence_thresh=-30, # dB
keep_silence = 300, # milliseconds
seek_step=20 # Speed up by seeking this many ms at a time
)
# Ignore empty audio
if len(chunks) <= 0:
return None
non_silent_audio = sum(chunks)
byte_string = non_silent_audio.raw_data
int_audio = np.frombuffer(byte_string, dtype=np.int16)
float_audio = int_audio.astype(np.float32) / np.iinfo(np.int16).max
# Use a faster resampler for longer audio
if len(float_audio) > 100000:
resampler = self.fast_resampler
else:
resampler = self.best_resampler
# Resample audio to the Whisper model's sample rate
pcm_data = resampler.process(
np.array(float_audio),
self.whisper_sample_rate / input_sample_rate)
# Ignore very short audio clips
if len(pcm_data) / self.whisper_sample_rate < 0.5:
return None
return pcm_data
def transcribe(self, pcm_data_array, channels, sample_rate):
pcm_data = self.normalize_audio(pcm_data_array, channels, sample_rate)
if pcm_data is None:
logger.info("remove_silence returned no data")
return None
result = self.pipe(pcm_data)
return result["text"]
def run(self):
while True:
command, *args = self.command_queue.get()
if command == 'stop':
# Exit!
break
elif command == 'transcribe':
result = self.transcribe(*args)
self.response_queue.put(result)
# This is run from a background process
def run_loop(command_queue: Queue, response_queue: Queue):
service = ASRService(command_queue, response_queue)
service.run()
logger.info("ASR background run loop process exiting")
# Runner for the service
# Also provides an API wrapper around the queues
# There should just be one instance of this class in the main process
class ASRServiceRunner:
def __init__(self):
self.lock = asyncio.Lock()
self.command_queue = Queue()
self.response_queue = Queue()
self.service_process = Process(
target=run_loop,
args=(self.command_queue, self.response_queue))
self.service_process.start()
def close(self):
logger.info("Stopping ASR service...")
self.command_queue.put(('stop',))
self.service_process.join()
self.command_queue.close()
self.response_queue.close()
logger.info("ASR service stopped.")
async def Transcribe(self, pcm_data_array, channels, sample_rate):
async with self.lock:
self.command_queue.put(('transcribe', pcm_data_array, channels, sample_rate))
return await asyncio.get_running_loop().run_in_executor(None, self.response_queue.get)