Fix StreamVAD audio slicing
Browse files- .gitignore +2 -1
- SileroOrt.py +0 -2
- StreamVAD.py +4 -1
- gradio_app.py +6 -5
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
output
|
| 2 |
-
__pycache__
|
|
|
|
|
|
| 1 |
output
|
| 2 |
+
__pycache__
|
| 3 |
+
.vscode
|
SileroOrt.py
CHANGED
|
@@ -11,8 +11,6 @@ class SileroOrt:
|
|
| 11 |
self.sr = 16000
|
| 12 |
self.hidden_size = 128
|
| 13 |
self.context_size = 64 if self.sr == 16000 else 32
|
| 14 |
-
self.context = np.zeros((self.batch_size, self.context_size), dtype=np.float32)
|
| 15 |
-
self.state = np.zeros((2, self.batch_size, self.hidden_size), dtype=np.float32)
|
| 16 |
self.num_samples = 512 if self.sr == 16000 else 256
|
| 17 |
|
| 18 |
self.model = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
|
|
|
|
| 11 |
self.sr = 16000
|
| 12 |
self.hidden_size = 128
|
| 13 |
self.context_size = 64 if self.sr == 16000 else 32
|
|
|
|
|
|
|
| 14 |
self.num_samples = 512 if self.sr == 16000 else 256
|
| 15 |
|
| 16 |
self.model = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
|
StreamVAD.py
CHANGED
|
@@ -41,11 +41,14 @@ class StreamVAD:
|
|
| 41 |
# record datetime
|
| 42 |
cur_ts = datetime.now()
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
# inference
|
| 45 |
speech_probs = self.model.audio_forward(audio, sr)[0]
|
| 46 |
|
| 47 |
for i, prob in enumerate(speech_probs):
|
| 48 |
-
audio_slice = audio[i * self.model.num_samples : (i + 1) * self.model.num_samples]
|
| 49 |
ts = cur_ts.strftime(self.datetime_format)
|
| 50 |
|
| 51 |
# is speech
|
|
|
|
| 41 |
# record datetime
|
| 42 |
cur_ts = datetime.now()
|
| 43 |
|
| 44 |
+
# freq scale
|
| 45 |
+
freq_scale = int(sr / self.model.sr)
|
| 46 |
+
|
| 47 |
# inference
|
| 48 |
speech_probs = self.model.audio_forward(audio, sr)[0]
|
| 49 |
|
| 50 |
for i, prob in enumerate(speech_probs):
|
| 51 |
+
audio_slice = audio[i * self.model.num_samples * freq_scale : (i + 1) * self.model.num_samples * freq_scale]
|
| 52 |
ts = cur_ts.strftime(self.datetime_format)
|
| 53 |
|
| 54 |
# is speech
|
gradio_app.py
CHANGED
|
@@ -4,7 +4,9 @@ from dataclasses import dataclass, field
|
|
| 4 |
|
| 5 |
|
| 6 |
vad = StreamVAD(
|
| 7 |
-
'silero_vad.onnx'
|
|
|
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
@dataclass
|
|
@@ -12,7 +14,7 @@ class AppState:
|
|
| 12 |
history: list = field(default_factory=list)
|
| 13 |
|
| 14 |
|
| 15 |
-
def process_audio(audio,
|
| 16 |
# print(audio)
|
| 17 |
# audio is a tuple of (sample_rate, numpy int16 array)
|
| 18 |
sr, audio_data = audio
|
|
@@ -21,13 +23,12 @@ def process_audio(audio, chatbot, state):
|
|
| 21 |
state.history.append(
|
| 22 |
gr.ChatMessage(role='user', content=gr.Audio(
|
| 23 |
label=f"{result['start_ts']} - {result['end_ts']}",
|
| 24 |
-
value=(
|
| 25 |
waveform_options=gr.WaveformOptions(show_recording_waveform=False),
|
| 26 |
editable=False
|
| 27 |
)
|
| 28 |
),
|
| 29 |
)
|
| 30 |
-
|
| 31 |
|
| 32 |
return state.history
|
| 33 |
|
|
@@ -42,6 +43,6 @@ with gr.Blocks() as demo:
|
|
| 42 |
input_audio = gr.Audio(sources=['microphone'], type='numpy', streaming=True)
|
| 43 |
|
| 44 |
# streaming process
|
| 45 |
-
input_audio.stream(fn=process_audio, inputs=[input_audio,
|
| 46 |
|
| 47 |
demo.launch(debug=True)
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
vad = StreamVAD(
|
| 7 |
+
'silero_vad.onnx',
|
| 8 |
+
sensitivity=0.5,
|
| 9 |
+
silence_ms=200
|
| 10 |
)
|
| 11 |
|
| 12 |
@dataclass
|
|
|
|
| 14 |
history: list = field(default_factory=list)
|
| 15 |
|
| 16 |
|
| 17 |
+
def process_audio(audio, state):
|
| 18 |
# print(audio)
|
| 19 |
# audio is a tuple of (sample_rate, numpy int16 array)
|
| 20 |
sr, audio_data = audio
|
|
|
|
| 23 |
state.history.append(
|
| 24 |
gr.ChatMessage(role='user', content=gr.Audio(
|
| 25 |
label=f"{result['start_ts']} - {result['end_ts']}",
|
| 26 |
+
value=(sr, result['audio']),
|
| 27 |
waveform_options=gr.WaveformOptions(show_recording_waveform=False),
|
| 28 |
editable=False
|
| 29 |
)
|
| 30 |
),
|
| 31 |
)
|
|
|
|
| 32 |
|
| 33 |
return state.history
|
| 34 |
|
|
|
|
| 43 |
input_audio = gr.Audio(sources=['microphone'], type='numpy', streaming=True)
|
| 44 |
|
| 45 |
# streaming process
|
| 46 |
+
input_audio.stream(fn=process_audio, inputs=[input_audio, state], outputs=[chatbot])
|
| 47 |
|
| 48 |
demo.launch(debug=True)
|