Can you write the code for k2, assuming the user has both espnet's and k2's dependencies…
- User
Can you write the code for k2, assuming the user has both espnet's and k2's dependencies installed?
Assistant
Here's a modified version of the k2-asr
transcribe.pythat implements long audio inference using thefind_blankfunction from espnet-asr. This code assumes that the user has both espnet's and k2's dependencies installed.pythonimport os import warnings import sherpa_onnx from .interface import TranscribeConfig, TranscribeResult, Subword from .audio import audio_to_file, pad_audio, norm_audio from espnet_asr.src.transcribe import find_blank PAD_SECONDS = 0.9 TOO_LONG_SECONDS = 30.0 WINDOW_SECONDS = 20 PADDING = (16000, 8000) def transcribe(model, audio, config=None): """Inference audio data using K2 model Args: model (sherpa_onnx.OfflineRecognizer): ReazonSpeech model audio (AudioData): Audio data to transcribe config (TranscribeConfig): Additional settings Returns: TranscribeResult """ if config is None: config = TranscribeConfig() audio = norm_audio(audio) pos = 0 fulltext = "" subwords = [] window = int(WINDOW_SECONDS * audio.samplerate) while pos < len(audio.waveform): samples = audio.waveform[pos:] # If the audio data is very long, find out the longest # non-speech region and perform decoding up to that point. if len(samples) > window: blank = find_blank(model, samples[:window]) mid = int((blank.start + blank.end) / 2) samples = samples[:mid] samples = pad_audio(samples, PAD_SECONDS) stream = model.create_stream() stream.accept_waveform(audio.samplerate, samples) model.decode_stream(stream) for t, s in zip(stream.result.tokens, stream.result.timestamps): subwords.append(Subword(token=t, seconds=s + pos / audio.samplerate)) fulltext += stream.result.text pos += len(samples) return TranscribeResult(fulltext, subwords)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.