Repository navigation
fix(punctuation): return an empty result for empty text in CTTransformerStreaming - #3824
Open
tanishqbaweja wants to merge 1 commit into
Open
tanishqbaweja wants to merge 1 commit into
tanishqbaweja wants to merge 1 commit into
Conversation
…aming CTTransformerStreaming overrides CTTransformer.inference without the empty-input check that CTTransformer has had since 4ad2a7b. With a fresh cache, empty or whitespace-only text yields float64 ids and the embedding raises "Expected tensor for argument modelscope#1 'indices' ... but got torch.DoubleTensor". With words pending in cache["pre_text"], the output string can be empty and sentence_out[-1] raises IndexError. AutoModel.generate(input="", cache=cache), which the streaming punctuation demo calls once per segment, passes the text straight to inference. Return the same empty result as CTTransformer before the cache is read, so pending words are kept for the next segment, and run the new test in the NumPy compatibility workflow.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #3823
Since 4ad2a7b,
CTTransformer.inferencereturns[{"key": ..., "text": "", "punc_array": None}]for empty or whitespace-only text.CTTransformerStreaming(funasr/models/ct_transformer_streaming/model.py, the class behind the realtime modeliic/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727inexamples/industrial_data_pretraining/ct_transformer_streaming/demo.py) overridesinferencewithout that check, so empty text fails in one of two ways:With a fresh cache,
""or" "gives no words.tokenizer.encode([])returns[], andnp.concatenateof the empty int32cache_sent_idwith that empty list produces float64 ids, so the embedding raises the error users reported for the offline model in runtime error for audio file in which there is actually no speech (blank voice) #1386, 无人声音频时指定punc_model报错 #1625 and auto_model 中punc模型入参为空触发的bug #1660:When earlier calls left words in
cache["pre_text"], an empty call runs the model on those words again and skips them in the output. If no punctuation is predicted after the last of them,sentence_outis empty andif sentence_out[-1] in self.punc_list:raisesIndexError: string index out of range.AutoModel.generate(input="", cache=cache)passes the empty string straight toinference, and the demo callsgenerateonce per VAD segment, so a segment with empty ASR text (silence or noise) takes one of these paths.This adds the three-line check from
CTTransformer.inferenceright afterassert len(data_in) == 1, before the cache is read or written. Empty or whitespace-only text now returnstext: ""andpunc_array: None, and words waiting incache["pre_text"]stay there for the next segment.Type of change
Validation
python -m compileall funasr examples testsWindows, Python 3.11.17, torch 2.10.0+cpu, numpy 2.4.6, pytest 9.2.0.
tests/test_ct_transformer_streaming_empty_input.pybuilds the realCTTransformerStreamingclass (embedding,SANMVadEncoderwith 2 blocks so the VAD mask runs, decoder) with tiny random weights and a decoder that always predicts_, plus the repo'sCharTokenizer. Nothing is downloaded.test_empty_text_returns_empty_result:""and" "with a fresh cache return[{"key": "utt", "text": "", "punc_array": None}].test_empty_segment_keeps_pending_words: callsAutoModel.generatethe way the demo does, with the segments跨境河流,""and是and one cache. The texts are跨境河流,""and是, andcache["pre_text"]is still["跨", "境", "河", "流"]after the empty segment.Commands:
python -m pytest -q tests/test_ct_transformer_streaming_empty_input.py: onmain(8710e1c), both subtests of the first test fail with theRuntimeErrorabove, and the second test fails at the empty segment withIndexError: string index out of range(model.py:207). With this change: 2 passed, 2 subtests passed. I got the same results with numpy 1.26.4, the older of the two versions in the workflow matrix.python -m pytest -q tests/test_punc_model_none.py tests/test_paraformer_empty_token_batch.py tests/test_ct_transformer_docs.py: 29 passed, 14 subtests passed, both onmainand with this change (with this change also under numpy 1.26.4).test-numpy-compatibility.yml, and the test to its pytest command next to the other CPU-only punctuation and Paraformer regressions. CI runs that list on both NumPy versions. Here on Windows the rest of the list does not run cleanly (nopytorch_wpeor ffmpeg, and some subprocess imports hit their 120 s timeout); none of those failures involve this change.black --line-length=100(24.4.0, the pre-commit pin) leaves the new test unchanged and does not touch the added lines inmodel.py.git diff --checkis clean.User impact
Users of the realtime punctuation model through
AutoModel, as in the streaming punctuation demo, get an empty result for a segment whose ASR text is empty or whitespace-only instead of aRuntimeErrororIndexError, and the next segment is punctuated with the same pending context. Non-empty text takes the unchanged code path.Notes for reviewers
CTTransformer.inferenceuses, so both punctuation models return the same structure for empty input.AutoModel.inference_with_vadalready handlespunc_array: Nonefrom the offline model.cache["pre_text"]is read or initialised, so an empty call leaves the cache as it was. Onmain, such a call either raised or recomputedpre_textfrom the same words.runtime/python/websocket/funasr_wss_server.pyalready skips empty text before calling this model. This change is for directAutoModelcallers such as the demo.