-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[Feature] Add chunk-based streaming ASR for Qwen3-ASR #22089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
918e08d
[Feature] Add chunk-based streaming ASR with adapter integration
SammLSH b012e19
fix: import prompt template from processor for single source of truth
SammLSH 5d44261
fix: rename _DEFAULT_ASR_PROMPT to DEFAULT_ASR_PROMPT (public API)
SammLSH 7f42ca4
fix: add leading space between chunk boundaries in streaming output
SammLSH f3dd88d
Merge branch 'main' into feat/qwen3-asr-streaming
mickqian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import io | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| import soundfile as sf | ||
|
|
||
|
|
||
| @dataclass | ||
| class StreamingASRState: | ||
| """State for chunk-based streaming ASR with prefix rollback. | ||
|
|
||
| Parameters are model-specific and should be provided via the | ||
| adapter's ``chunked_streaming_config``. | ||
|
|
||
| Known limitation: rollback uses str.split() which is ineffective | ||
| for CJK languages (no whitespace between words). | ||
| TODO: implement token-level rollback to handle all languages | ||
| correctly. | ||
| """ | ||
|
|
||
| chunk_size_sec: float | ||
| unfixed_chunk_num: int | ||
| unfixed_token_num: int | ||
| confirmed_text: str = "" | ||
| full_transcript: str = "" | ||
| chunk_index: int = 0 | ||
|
|
||
| def get_prefix_text(self) -> str: | ||
| if self.chunk_index < self.unfixed_chunk_num or not self.confirmed_text: | ||
| return "" | ||
| return self.confirmed_text | ||
|
|
||
| def update(self, new_transcript: str) -> str: | ||
| old_confirmed = self.confirmed_text | ||
| words = new_transcript.split() | ||
| if len(words) > self.unfixed_token_num: | ||
| self.confirmed_text = " ".join(words[: -self.unfixed_token_num]) | ||
| else: | ||
| self.confirmed_text = "" | ||
| self.full_transcript = new_transcript | ||
| self.chunk_index += 1 | ||
| if self.confirmed_text.startswith(old_confirmed): | ||
| return self.confirmed_text[len(old_confirmed) :].strip() | ||
| # Model revised earlier text, use word level common prefix to avoid | ||
| # re-emitting already-sent content and cutting mid-word. | ||
| old_words = old_confirmed.split() | ||
| new_words = self.confirmed_text.split() | ||
| common_count = 0 | ||
| for ow, nw in zip(old_words, new_words): | ||
| if ow != nw: | ||
| break | ||
| common_count += 1 | ||
| return " ".join(new_words[common_count:]) | ||
|
|
||
| def finalize(self) -> str: | ||
| confirmed_words = self.confirmed_text.split() | ||
| all_words = self.full_transcript.split() | ||
| # Use word level common prefix to handle punctuation differences | ||
| # between intermediate chunks and the final full transcription. | ||
| common_count = 0 | ||
| for cw, aw in zip(confirmed_words, all_words): | ||
| if cw != aw: | ||
| break | ||
| common_count += 1 | ||
| self.confirmed_text = self.full_transcript | ||
| if common_count == 0 and confirmed_words and all_words: | ||
| return self.full_transcript | ||
| return " ".join(all_words[common_count:]) | ||
|
|
||
|
|
||
| def split_audio_chunks(audio_data: bytes, chunk_size_sec: float) -> List[bytes]: | ||
| if not audio_data: | ||
| raise ValueError("audio_data is empty") | ||
| if chunk_size_sec <= 0: | ||
| raise ValueError(f"chunk_size_sec must be positive, got {chunk_size_sec}") | ||
| audio_file = io.BytesIO(audio_data) | ||
| try: | ||
| data, sample_rate = sf.read(audio_file, dtype="float32") | ||
| except sf.LibsndfileError as e: | ||
| raise ValueError(f"failed to decode audio: {e}") from e | ||
| if len(data.shape) > 1: | ||
| data = data.mean(axis=1) | ||
| chunk_size_samples = int(chunk_size_sec * sample_rate) | ||
| total_samples = len(data) | ||
| chunks = [] | ||
| for end in range( | ||
| chunk_size_samples, total_samples + chunk_size_samples, chunk_size_samples | ||
| ): | ||
| end = min(end, total_samples) | ||
| buf = io.BytesIO() | ||
| sf.write(buf, data[:end], sample_rate, format="WAV") | ||
| chunks.append(buf.getvalue()) | ||
| return chunks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.